@@ -43,27 +43,22 @@ class QueueJob(models.Model):
4343 "date_created" ,
4444 "model_name" ,
4545 "method_name" ,
46+ "func_string" ,
47+ "channel_method_name" ,
48+ "job_function_id" ,
4649 "records" ,
4750 "args" ,
4851 "kwargs" ,
4952 )
5053
5154 uuid = fields .Char (string = "UUID" , readonly = True , index = True , required = True )
52- user_id = fields .Many2one (
53- comodel_name = "res.users" ,
54- string = "User ID" ,
55- compute = "_compute_user_id" ,
56- inverse = "_inverse_user_id" ,
57- store = True ,
58- )
55+ user_id = fields .Many2one (comodel_name = "res.users" , string = "User ID" )
5956 company_id = fields .Many2one (
6057 comodel_name = "res.company" , string = "Company" , index = True
6158 )
6259 name = fields .Char (string = "Description" , readonly = True )
6360
64- model_name = fields .Char (
65- string = "Model" , compute = "_compute_model_name" , store = True , readonly = True
66- )
61+ model_name = fields .Char (string = "Model" , readonly = True )
6762 method_name = fields .Char (readonly = True )
6863 # record_ids field is only for backward compatibility (e.g. used in related
6964 # actions), can be removed (replaced by "records") in 14.0
@@ -73,9 +68,7 @@ class QueueJob(models.Model):
7368 )
7469 args = JobSerialized (readonly = True , base_type = tuple )
7570 kwargs = JobSerialized (readonly = True , base_type = dict )
76- func_string = fields .Char (
77- string = "Task" , compute = "_compute_func_string" , readonly = True , store = True
78- )
71+ func_string = fields .Char (string = "Task" , readonly = True )
7972
8073 state = fields .Selection (STATES , readonly = True , required = True , index = True )
8174 priority = fields .Integer ()
@@ -95,21 +88,13 @@ class QueueJob(models.Model):
9588 "max. retries.\n "
9689 "Retries are infinite when empty." ,
9790 )
98- channel_method_name = fields .Char (
99- readonly = True , compute = "_compute_job_function" , store = True
100- )
91+ # FIXME the name of this field is very confusing
92+ channel_method_name = fields .Char (readonly = True )
10193 job_function_id = fields .Many2one (
102- comodel_name = "queue.job.function" ,
103- compute = "_compute_job_function" ,
104- string = "Job Function" ,
105- readonly = True ,
106- store = True ,
94+ comodel_name = "queue.job.function" , string = "Job Function" , readonly = True ,
10795 )
10896
109- override_channel = fields .Char ()
110- channel = fields .Char (
111- compute = "_compute_channel" , inverse = "_inverse_channel" , store = True , index = True
112- )
97+ channel = fields .Char (index = True )
11398
11499 identity_key = fields .Char (readonly = True )
115100 worker_pid = fields .Integer (readonly = True )
@@ -126,65 +111,18 @@ def init(self):
126111 "'enqueued') AND identity_key IS NOT NULL;"
127112 )
128113
129- @api .depends ("records" )
130- def _compute_user_id (self ):
131- for record in self :
132- record .user_id = record .records .env .uid
133-
134- def _inverse_user_id (self ):
135- for record in self .with_context (_job_edit_sentinel = self .EDIT_SENTINEL ):
136- record .records = record .records .with_user (record .user_id .id )
137-
138- @api .depends ("records" )
139- def _compute_model_name (self ):
140- for record in self :
141- record .model_name = record .records ._name
142-
143114 @api .depends ("records" )
144115 def _compute_record_ids (self ):
145116 for record in self :
146117 record .record_ids = record .records .ids
147118
148- def _inverse_channel (self ):
149- for record in self :
150- record .override_channel = record .channel
151-
152- @api .depends ("job_function_id.channel_id" )
153- def _compute_channel (self ):
154- for record in self :
155- channel = (
156- record .override_channel or record .job_function_id .channel or "root"
157- )
158- if record .channel != channel :
159- record .channel = channel
160-
161- @api .depends ("model_name" , "method_name" , "job_function_id.channel_id" )
162- def _compute_job_function (self ):
163- for record in self :
164- func_model = self .env ["queue.job.function" ]
165- channel_method_name = func_model .job_function_name (
166- record .model_name , record .method_name
167- )
168- function = func_model .search ([("name" , "=" , channel_method_name )], limit = 1 )
169- record .channel_method_name = channel_method_name
170- record .job_function_id = function
171-
172- @api .depends ("model_name" , "method_name" , "records" , "args" , "kwargs" )
173- def _compute_func_string (self ):
174- for record in self :
175- model = repr (record .records )
176- args = [repr (arg ) for arg in record .args ]
177- kwargs = ["{}={!r}" .format (key , val ) for key , val in record .kwargs .items ()]
178- all_args = ", " .join (args + kwargs )
179- record .func_string = "{}.{}({})" .format (model , record .method_name , all_args )
180-
181119 @api .model_create_multi
182120 def create (self , vals_list ):
183121 if self .env .context .get ("_job_edit_sentinel" ) is not self .EDIT_SENTINEL :
184122 # Prevent to create a queue.job record "raw" from RPC.
185123 # ``with_delay()`` must be used.
186124 raise exceptions .AccessError (
187- _ ("Queue jobs must created by calling 'with_delay()'." )
125+ _ ("Queue jobs must be created by calling 'with_delay()'." )
188126 )
189127 return super ().create (vals_list )
190128
@@ -200,10 +138,25 @@ def write(self, vals):
200138 )
201139 )
202140
141+ different_user_jobs = self .browse ()
142+ if vals .get ("user_id" ):
143+ different_user_jobs = self .filtered (
144+ lambda records : records .env .user .id != vals ["user_id" ]
145+ )
146+
203147 if vals .get ("state" ) == "failed" :
204148 self ._message_post_on_failure ()
205149
206- return super ().write (vals )
150+ result = super ().write (vals )
151+
152+ for record in different_user_jobs :
153+ # the user is stored in the env of the record, but we still want to
154+ # have a stored user_id field to be able to search/groupby, so
155+ # synchronize the env of records with user_id
156+ super (QueueJob , record ).write (
157+ {"records" : record .records .with_user (vals ["user_id" ])}
158+ )
159+ return result
207160
208161 def open_related_action (self ):
209162 """Open the related action associated to the job"""
@@ -515,7 +468,8 @@ class JobFunction(models.Model):
515468 "retry_pattern "
516469 "related_action_enable "
517470 "related_action_func_name "
518- "related_action_kwargs " ,
471+ "related_action_kwargs "
472+ "job_function_id " ,
519473 )
520474
521475 def _default_channel (self ):
@@ -637,6 +591,7 @@ def job_default_config(self):
637591 related_action_enable = True ,
638592 related_action_func_name = None ,
639593 related_action_kwargs = {},
594+ job_function_id = None ,
640595 )
641596
642597 def _parse_retry_pattern (self ):
@@ -669,6 +624,7 @@ def job_config(self, name):
669624 related_action_enable = config .related_action .get ("enable" , True ),
670625 related_action_func_name = config .related_action .get ("func_name" ),
671626 related_action_kwargs = config .related_action .get ("kwargs" ),
627+ job_function_id = config .id ,
672628 )
673629
674630 def _retry_pattern_format_error_message (self ):
0 commit comments