Skip to content

Commit

Permalink
[IMP] report_async: change job_ids to One2many with queue.job
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardCForgeFlow committed Jul 5, 2024
1 parent 9a0f99f commit e7a5bf0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
19 changes: 19 additions & 0 deletions report_async/migrations/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from openupgradelib import openupgrade


def populate_report_async_id(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE queue_job qj
SET report_async_id = ra.id
FROM report_async ra
WHERE qj.func_string LIKE '%%report.async(%%' || ra.id || ',%%'
AND qj.user_id = ra.create_uid
"""
)


@openupgrade.migrate()
def migrate(env, version):
populate_report_async_id(env)
1 change: 1 addition & 0 deletions report_async/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

from . import report_async
from . import ir_report
from . import queue_job
5 changes: 4 additions & 1 deletion report_async/models/ir_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ def report_action(self, docids, data=None, config=True):
rpt_async_id = res["context"]["active_id"]
report_async = self.env["report.async"].browse(rpt_async_id)
if res["report_type"] in REPORT_TYPES:
report_async.with_delay(
job = report_async.with_delay(
eta=res["context"].get("eta", False)
).run_report(
res["context"].get("active_ids", []), data, self.id, self._uid
)
if job:
(self.env['queue.job'].search([('uuid', '=', job.uuid)])
.write({'report_async_id': report_async.id}))
return {}
return res
12 changes: 12 additions & 0 deletions report_async/models/queue_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from odoo import models, fields


class QueueJob(models.Model):
_inherit = 'queue.job'

report_async_id = fields.Many2one(
comodel_name='report.async',
string='Report Async',
index=True,
ondelete='cascade',
)
15 changes: 6 additions & 9 deletions report_async/models/report_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ class ReportAsync(models.Model):
help="Only user in selected groups can use this report."
"If left blank, everyone can use",
)
job_ids = fields.Many2many(
job_ids = fields.One2many(
comodel_name='queue.job',
compute='_compute_job',
inverse_name='report_async_id',
string='Jobs',
help="List all jobs related to this running report",
)
job_status = fields.Selection(
Expand All @@ -58,11 +59,11 @@ class ReportAsync(models.Model):
('started', 'Started'),
('done', 'Done'),
('failed', 'Failed')],
compute='_compute_job',
compute='_compute_job_status_info',
help="Latest Job Status",
)
job_info = fields.Text(
compute='_compute_job',
compute='_compute_job_status_info',
help="Latest Job Error Message",
)
file_ids = fields.Many2many(
Expand All @@ -74,12 +75,8 @@ class ReportAsync(models.Model):
schedule_time = fields.Char(string='Schedule time')

@api.multi
def _compute_job(self):
def _compute_job_status_info(self):
for rec in self:
rec.job_ids = self.sudo().env['queue.job'].search(
[('func_string', 'like', 'report.async(%s,)' % rec.id),
('user_id', '=', self._uid)],
order='id desc')
rec.job_status = (rec.job_ids[0].sudo().state
if rec.job_ids else False)
rec.job_info = (rec.job_ids[0].sudo().exc_info
Expand Down

0 comments on commit e7a5bf0

Please sign in to comment.