Skip to content

Commit

Permalink
Fix auto registration of job methods and channels
Browse files Browse the repository at this point in the history
The automatic registration of job methods and their defaut channel
has been a bit chaotic. The initial version for the new Odoo API could
crashing as soon as a method was decorated by @Property. There is
such a property in the code code, the method '_cache'.

The problem of the crash was that, the introspection basically uses a
'getattr' on every attribute of the instance. The '_cache' method
could then be called on an empty recordset, which is not supported by
'_cache'.

A first correction (49d8f37) was to naively skip the '_cache' method
from the introspection.

In any case, it is wrong to access the property of an instance only to
instrospect its members.  That's why the correction 4ebb245 changed the
inspection from the instance to the class.

Properties are no longer accessed, however this correction was not
correct for Python 3. When members of the class are introspected, they
are neither bound neither unbound methods. they are mere functions.

The change here is to lookup for functions. _register_job must now takes
the model as input argument, because there is no way to get the name of
the model from the function.

Closes OCA#64
Follows OCA#50
  • Loading branch information
guewen authored and nguyenminhchien committed Nov 29, 2023
1 parent 1d8805a commit 613027f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 10 additions & 0 deletions test_queue_job/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ def job_b(self):
def job_sub_channel(self):
return

@property
def dummy_property(self):
""" Return foo
Only there to check that properties are compatible
with the automatic registration of job methods
and their default channels.
"""
return 'foo'


class TestRelatedAction(models.Model):

Expand Down
20 changes: 16 additions & 4 deletions test_queue_job/tests/test_job_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ def test_register_jobs(self):
self.env['queue.job.channel'].search([('name', '!=', 'root')]).unlink()

method_a = self.env['test.queue.channel'].job_a
self.env['queue.job.function']._register_job(method_a)
self.env['queue.job.function']._register_job(
self.env['test.queue.channel'],
method_a
)
method_b = self.env['test.queue.channel'].job_b
self.env['queue.job.function']._register_job(method_b)
self.env['queue.job.function']._register_job(
self.env['test.queue.channel'],
method_b
)

path_a = '<test.queue.channel>.job_a'
path_b = '<test.queue.channel>.job_b'
Expand All @@ -58,7 +64,10 @@ def test_channel_on_job(self):
self.env['queue.job.channel'].search([('name', '!=', 'root')]).unlink()

method = self.env['test.queue.channel'].job_a
self.env['queue.job.function']._register_job(method)
self.env['queue.job.function']._register_job(
self.env['test.queue.channel'],
method
)
path_a = '<%s>.%s' % (method.__self__.__class__._name, method.__name__)
job_func = self.function_model.search([('name', '=', path_a)])
self.assertEquals(job_func.channel, 'root')
Expand Down Expand Up @@ -91,7 +100,10 @@ def test_default_channel(self):
self.env['queue.job.channel'].search([('name', '!=', 'root')]).unlink()

method = self.env['test.queue.channel'].job_sub_channel
self.env['queue.job.function']._register_job(method)
self.env['queue.job.function']._register_job(
self.env['test.queue.channel'],
method
)
self.assertEquals(method.default_channel, 'root.sub.subsub')

path_a = '<%s>.%s' % (method.__self__.__class__._name, method.__name__)
Expand Down

0 comments on commit 613027f

Please sign in to comment.