0

I'm having a strange problem that is difficult to reproduce (everything worked 2 days ago but some time between then and now no longer does--with no changes in the interim!)

I have a django server program which we are running via gunicorn with multiple worker subprocesses and a separate small REST webservice which shares the settings.py of the server program and acts on the same DB objects. The code for this server program is roughly as follows:

# my app's models.py
class TestConfig(models.Model):
    ## various attributes
class Test(models.Model):
    ## some attributes
    def startTest(self):
        return TestExecution.objects.create(test=self)
class TestExecution(models.Model):
    test = models.ForeignKey(
    Test,
    on_delete=models.CASCADE
    )
    config = models.ForeignKey(
    TestConfig,
    on_delete=models.CASCADE,
    null=True
    )

# excerpt from a post() method in my app's views.py
    test = Test.objects.get(test_id)
    if config_form.is_valid():
        config = config_form.save()
        config_id = config.id
        test_exe = test.startTest()
        test_exe.config = config
        test_exe.save()

        webservice_response = requests.get(
            'http://{}:{}/rest/add_to_queue/{}'.format(
                webservice_ip, webservice_port, test_exe.id))

The other program (small REST webservice) sharing the same settings.py as the django server program looks as follows:

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django
django.setup()

# the REST endpoint referenced from the django server program
@app.route('/rest/add_to_queue/<test_exe_object_id>/')
@app.route('/rest/add_to_queue/<test_exe_object_id>')
def add_to_queue(test_exe_object_id):
from myapp.models import TestExecution
try:
    exe_object = TestExecution.objects.get(pk=int(test_exe_object_id))

# for completeness the database section of my settings.py:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test_db',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
    }
}

As I mentioned, this was all working fine a few days ago, then when I tried again today I was getting a DoesNotExist in the second program when trying to "get()" the TestExecution object using its 'id'.

OL83
  • 3
  • 3
  • Sounds like a transaction that hasn't finished committing. Maybe a `with transaction.atomic():...` around the form and object saves and before the rest call? – thebjorn Jun 03 '18 at 19:03
  • I will try wrapping the test_exe creation, modification, and save with the atomic ContextManager, but my understanding was that the atomicity features in django have to do with ensuring sole access and not necessarily whether the transactions are committed or not (for that, I should be covered by the default django behavior of autocommit being enabled: https://docs.djangoproject.com/en/1.10/topics/db/transactions/ . But thank you for your suggestion, I will give it a try! – OL83 Jun 03 '18 at 19:23
  • no, atomic is Django's misspelling of transaction/commit ;-) Autocommit should be enough to save you, but maybe django has added some performance enhancements that holds external visibility until the end of the method (I haven't checked lately). – thebjorn Jun 03 '18 at 19:26

0 Answers0