Skip to content
This repository was archived by the owner on Nov 14, 2022. It is now read-only.

Commit c4d621f

Browse files
author
Dimitar Tasev
authored
Merge pull request #1252 from ISISScientificComputing/threadsafe_experiment_creation
2 parents 1ae33e2 + 3c51b5c commit c4d621f

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 3.1.2 on 2021-03-26 16:28
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('reduction_viewer', '0004_instrument_is_flat_output'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='experiment',
15+
name='reference_number',
16+
field=models.IntegerField(unique=True),
17+
),
18+
]

WebApp/autoreduce_webapp/reduction_viewer/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Experiment(models.Model):
4848
"""
4949
Holds data about an Experiment
5050
"""
51-
reference_number = models.IntegerField()
51+
reference_number = models.IntegerField(unique=True)
5252

5353
def __str__(self):
5454
"""

model/database/access.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Common functions for accessing and creating records in the database
99
"""
1010
from typing import List
11+
from django.db import transaction
1112

1213
from utils.clients.django_database_client import DatabaseClient
1314

@@ -22,6 +23,7 @@ def start_database():
2223
return database
2324

2425

26+
@transaction.atomic
2527
def get_instrument(instrument_name):
2628
"""
2729
Find the instrument record associated with the name provided in the database
@@ -53,6 +55,7 @@ def get_all_instrument_names() -> List[str]:
5355
return list(database.data_model.Instrument.objects.values_list("name", flat=True))
5456

5557

58+
@transaction.atomic
5659
def get_status(status_value: str):
5760
"""
5861
Find the status record associated with the value provided in the database
@@ -68,6 +71,7 @@ def get_status(status_value: str):
6871
return database.data_model.Status.objects.get_or_create(value=status_value)[0]
6972

7073

74+
@transaction.atomic
7175
def get_experiment(rb_number):
7276
"""
7377
Find the Experiment record associated with the rb_number provided in the database
@@ -78,6 +82,7 @@ def get_experiment(rb_number):
7882
return database.data_model.Experiment.objects.get_or_create(reference_number=rb_number)[0]
7983

8084

85+
@transaction.atomic
8186
def get_software(name, version):
8287
"""
8388
Find the Software record associated with the name and version provided
@@ -126,3 +131,7 @@ def save_record(record):
126131
Note: This is mostly a wrapper to aid unit testing
127132
"""
128133
record.save()
134+
135+
136+
# Initialises the DjangoORM immediately on importing this module
137+
start_database()

queue_processors/queue_processor/handle_message.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,31 @@ def _handle_error(self, reduction_run, message, err):
8080
message.reduction_log = str(err)
8181
self.reduction_error(reduction_run, message)
8282

83-
@transaction.atomic
8483
def create_run_records(self, message: Message):
8584
"""
86-
Creates or gets the necessary records to construct a ReductionRun
85+
Creates or gets the necessary records to construct a ReductionRun.
8786
"""
8887
# This must be done before looking up the run version to make sure the experiment record exists!
8988
rb_number = self.normalise_rb_number(message.rb_number)
9089
experiment = db_access.get_experiment(rb_number)
9190
run_version = db_access.find_highest_run_version(experiment, run_number=str(message.run_number))
9291
message.run_version = run_version
93-
9492
instrument = db_access.get_instrument(str(message.instrument))
93+
return self.do_create_reduction_record(message, experiment, instrument)
94+
95+
@transaction.atomic
96+
def do_create_reduction_record(self, message: Message, experiment, instrument):
97+
"""
98+
Creates the reduction record
99+
"""
95100
script = ReductionScript(instrument.name)
96101
script_text = script.text()
102+
97103
# Make the new reduction run with the information collected so far
98104
reduction_run = db_records.create_reduction_run_record(experiment=experiment,
99105
instrument=instrument,
100106
message=message,
101-
run_version=run_version,
107+
run_version=message.run_version,
102108
script_text=script_text,
103109
status=self.status.get_queued())
104110
reduction_run.save()

0 commit comments

Comments
 (0)