Skip to content

Commit bb7909e

Browse files
committed
[FIX] queue job pass stored context
1 parent a16f587 commit bb7909e

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

base_import_async/models/base_import_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def _split_file(self, model_name, translated_model_name,
169169
fields, data[row_from:row_to + 1], options,
170170
file_name=root + '-' + chunk + ext)
171171
delayed_job = self.with_context(
172-
job_batch=batch).with_delay(
172+
job_batch=batch.id).with_delay(
173173
description=description,
174174
priority=priority,
175175
keep_context=True

queue_job/job.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import functools
66
import hashlib
77
import logging
8+
import json
9+
import yaml
810
import uuid
911
import sys
1012
from datetime import datetime, timedelta
@@ -301,6 +303,7 @@ def _load_from_db_record(cls, job_db_record):
301303
if stored.company_id:
302304
job_.company_id = stored.company_id.id
303305
job_.identity_key = stored.identity_key
306+
job_.keep_context = stored.context or {}
304307
return job_
305308

306309
def job_record_with_same_identity_key(self):
@@ -355,6 +358,15 @@ def enqueue(cls, func, args=None, kwargs=None,
355358
def db_record_from_uuid(env, job_uuid):
356359
model = env['queue.job'].sudo()
357360
record = model.search([('uuid', '=', job_uuid)], limit=1)
361+
if record.context:
362+
try:
363+
ctx = json.loads(record.context)
364+
except Exception as e:
365+
_logger.error('\n\nERROR Due to JSON CONVERSION: %s\n\n' % e)
366+
return record.with_env(env)
367+
else:
368+
_logger.info("\n\nCONTEXT: %s\n\n" % ctx)
369+
return record.with_context(ctx).with_env(env)
358370
return record.with_env(env)
359371

360372
def __init__(self, func,
@@ -510,8 +522,11 @@ def store(self):
510522
}
511523

512524
dt_to_string = odoo.fields.Datetime.to_string
525+
context = {}
513526
if self.keep_context:
514-
vals.update({"context": str(self.env.context.copy())})
527+
context = self.env.context.copy()
528+
vals.update({"context": json.dumps(context)})
529+
_logger.info("\n\nCURRENT CONTEXT: %s\n\n" % context)
515530
if self.date_enqueued:
516531
vals['date_enqueued'] = dt_to_string(self.date_enqueued)
517532
if self.date_started:
@@ -539,17 +554,13 @@ def store(self):
539554
'args': self.args,
540555
'kwargs': self.kwargs,
541556
})
542-
# By default the context is completely reset
543-
# (compatibility with previous version).
544-
context = {}
545-
if self.keep_context:
546-
context = self.env.context.copy()
547557
# it the channel is not specified, lets the job_model compute
548558
# the right one to use
549559
if self.channel:
550560
vals.update({'channel': self.channel})
551561

552-
self.env[self.job_model_name].sudo().create(vals)
562+
job = self.env[self.job_model_name].sudo().create(vals)
563+
_logger.info('\n\nJOB: %s CONTEXT: %s' % (job, context))
553564

554565
def db_record(self):
555566
return self.db_record_from_uuid(self.env, self.uuid)
@@ -564,9 +575,9 @@ def _get_record_context(self):
564575
company_ids = [self.company_id]
565576
context_txt = self.db_record().context or {}
566577
if isinstance(context_txt, str):
567-
context = safe_eval(context_txt)
578+
context = json.loads(context_txt)
568579
else:
569-
context = context_txt
580+
context = json.loads(context_txt)
570581
context.update(
571582
{"job_uuid": self.uuid, "allowed_company_ids": company_ids})
572583
return context

test_queue_job_batch/tests/test_queue_job_batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def test_batch(self):
88
batch = self.env['queue.job.batch'].get_new_batch('TEST')
99
self.assertFalse(batch.job_ids)
1010
model = self.env['test.queue.job'].with_context(
11-
job_batch=batch
11+
job_batch=batch.id
1212
)
1313
job_1 = model.with_delay().testing_method()
1414
self.assertEqual(job_1.db_record().state, 'pending')

0 commit comments

Comments
 (0)