55import functools
66import hashlib
77import logging
8+ import json
9+ import yaml
810import uuid
911import sys
1012from 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 \n ERROR Due to JSON CONVERSION: %s\n \n ' % e )
366+ return record .with_env (env )
367+ else :
368+ _logger .info ("\n \n CONTEXT: %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 \n CURRENT 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 \n JOB: %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
0 commit comments