1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
from odoo import models, fields
from datetime import datetime
import logging, time
_logger = logging.getLogger(__name__)
class ApacheSolrQueue(models.Model):
_name = 'apache.solr.queue'
display_name = fields.Char('Display Name', compute="_compute_display_name")
res_model = fields.Char('Resource Model')
res_id = fields.Integer('Resource ID')
function_name = fields.Char('Function Name')
execute_status = fields.Selection([
('success', 'Success'),
('failed', 'Failed'),
('not_found', 'Record not found')
], 'Execute Status')
execute_date = fields.Datetime('Execute Date')
def _compute_display_name(self):
for rec in self:
try:
res_model = rec.res_model
res_id = int(rec.res_id)
model_instance = self.env[res_model].browse(res_id)
rec.display_name = model_instance.display_name or ''
except:
rec.display_name = ''
def process_queue_item(self, limit=100, max_exec_time=30):
records = self.search([('execute_status', '=', False)], order='create_date asc', limit=limit)
start_time = time.time()
for rec in records:
end_time = time.time()
elapsed_time = end_time - start_time
if elapsed_time > max_exec_time:
break
rec.execute_queue()
def execute_queue(self):
for rec in self:
try:
res_model = rec.res_model
res_id = int(rec.res_id)
function_name = rec.function_name
_logger.info(f'Execute Queue: {res_model}.{function_name}() -> {res_id}')
domain = [('id', '=', res_id)]
if res_model in ['product.template']:
domain.append(('active', 'in', [True, False]))
model_instance = self.env[res_model].search(domain)
if model_instance:
getattr(model_instance, function_name)()
rec.execute_status = 'success'
else:
rec.execute_status = 'not_found'
except:
rec.execute_status = 'failed'
rec.execute_date = datetime.utcnow()
self.env.cr.commit()
def create_unique(self, payload={}):
count = self.search_count([
('res_model', '=', payload['res_model']),
('res_id', '=', payload['res_id']),
('function_name', '=', payload['function_name']),
('execute_status', '=', False)
])
if count == 0:
self.create(payload)
|