Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][IMP] report_async: change job_ids to One2many with queue.job #905

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions report_async/migrations/12.0.1.2.0/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',
)
22 changes: 9 additions & 13 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,16 +75,11 @@ 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
if rec.job_ids else False)
jobs = rec.sudo().job_ids
rec.job_status = (jobs[0].state if jobs else False)
rec.job_info = (jobs[0].exc_info if jobs else False)

@api.multi
def _compute_file(self):
Expand Down
Loading