Skip to content
Merged
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
118 changes: 118 additions & 0 deletions queue_job_batch/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
===============
Job Queue Batch
===============

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fqueue-lightgray.png?logo=github
:target: https://github.com/OCA/queue/tree/11.0/queue_job_batch
:alt: OCA/queue
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/queue-11-0/queue-11-0-queue_job_batch
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/230/11.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

This addon adds an a grouper for queue jobs.

It allows to show your jobs in a batched form in order to know better the
results.

Example:

.. code-block:: python

from odoo import models, fields, api
from odoo.addons.queue_job.job import job

class MyModel(models.Model):
_name = 'my.model'

@api.multi
@job
def my_method(self, a, k=None):
_logger.info('executed with a: %s and k: %s', a, k)


class MyOtherModel(models.Model):
_name = 'my.other.model'

@api.multi
def button_do_stuff(self):
batch = self.env['queue.job.batch'].get_new_batch('Group')
for i in range(1, 100):
self.env['my.model'].with_context(
job_batch=batch
).with_delay().my_method('a', k=i)
batch.enqueue()


In the snippet of code above, when we call ``button_do_stuff``, 100 jobs
capturing the method and arguments will be postponed. It will be executed as
soon as the Jobrunner has a free bucket, which can be instantaneous if no other
job is running.

Once all the jobs have finished, the grouper will be marked as finished.

**Table of contents**

.. contents::
:local:

Usage
=====

You can manage your batch jobs from the Systray. A new button will be shown
with your currently executing job batches and the recently finished job groups.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/queue/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/queue/issues/new?body=module:%20queue_job_batch%0Aversion:%2011.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Creu Blanca

Contributors
~~~~~~~~~~~~

* Enric Tobella <etobella@creublanca.es>

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/queue <https://github.com/OCA/queue/tree/11.0/queue_job_batch>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions queue_job_batch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
24 changes: 24 additions & 0 deletions queue_job_batch/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)


{
'name': 'Job Queue Batch',
'version': '11.0.1.1.0',
'author': 'Creu Blanca,Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/queue',
'license': 'AGPL-3',
'category': 'Generic Modules',
'depends': [
'queue_job',
],
'qweb': [
'static/src/xml/systray.xml',
],
'data': [
'security/security.xml',
'security/ir.model.access.csv',
'views/queue_job_views.xml',
'views/queue_job_batch_views.xml',
'views/assets_backend.xml',
],
}
2 changes: 2 additions & 0 deletions queue_job_batch/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import queue_job
from . import queue_job_batch
37 changes: 37 additions & 0 deletions queue_job_batch/models/queue_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2019 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo import models, fields, api


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

job_batch_id = fields.Many2one(
'queue.job.batch'
)

@api.model
def create(self, vals):
batch = self.env.context.get('job_batch')
if batch and isinstance(
batch, models.Model
) and batch.state == 'draft':
vals.update({
'job_batch_id': batch.id
})
return super().create(vals)

@api.multi
def write(self, vals):
batches = self.env['queue.job.batch']
for record in self:
if record.job_batch_id and record.state != 'done' and vals.get(
'state', ''
) == 'done':
batches |= record.job_batch_id
for batch in batches:
# We need to make it with delay in order to prevent two jobs
# to work with the same batch
batch.with_delay().check_state()
return super().write(vals)
126 changes: 126 additions & 0 deletions queue_job_batch/models/queue_job_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Copyright 2019 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo import models, fields, api
from odoo.addons.queue_job.job import job


class QueueJobBatch(models.Model):
_name = 'queue.job.batch'
_inherit = ['mail.thread', 'mail.activity.mixin']
_description = 'Batch of jobs'
_log_access = False

name = fields.Char(
required=True,
readonly=True,
track_visibility='onchange',
)
job_ids = fields.One2many(
'queue.job',
inverse_name='job_batch_id',
readonly=True,
)
job_count = fields.Integer(
compute='_compute_job_count',
)
user_id = fields.Many2one(
'res.users',
required=True,
readonly=True,
track_visibility='onchange',
)
state = fields.Selection(
[
('draft', 'Draft'),
('enqueued', 'Enqueued'),
('progress', 'In Progress'),
('finished', 'Finished')
],
default='draft',
required=True,
readonly=True,
track_visibility='onchange',
)
finished_job_count = fields.Float(
compute='_compute_job_count',
)
failed_job_count = fields.Float(
compute='_compute_job_count',
)
company_id = fields.Many2one(
'res.company',
readonly=True,
)
is_read = fields.Boolean(
default=True
)
completeness = fields.Float(
compute='_compute_job_count',
)
failed_percentage = fields.Float(
compute='_compute_job_count',
)

def enqueue(self):
self.filtered(lambda r: r.state == 'draft').write({
'state': 'enqueued'
})
for record in self:
record.check_state()

@job
def check_state(self):
self.ensure_one()
if self.state == 'enqueued' and any(
job.state not in ['pending', 'enqueued'] for job in self.job_ids
):
self.write({'state': 'progress'})
if self.state != 'progress':
return True
if all(job.state == 'done' for job in self.job_ids):
self.write({
'state': 'finished',
'is_read': False,
})
return True

@api.multi
def set_read(self):
return self.write({'is_read': True})

@api.model
def get_new_batch(self, name, **kwargs):
vals = kwargs.copy()
if 'company_id' in self.env.context:
company_id = self.env.context['company_id']
else:
company_model = self.env['res.company']
company_model = company_model.sudo(self.env.uid)
company_id = company_model._company_default_get(
object='queue.job',
field='company_id'
).id
vals.update({
'user_id': self.env.uid,
'name': name,
'state': 'draft',
'company_id': company_id,
})
return self.sudo().create(vals).sudo(self.env.uid)

@api.depends('job_ids')
def _compute_job_count(self):
for record in self:
job_count = len(record.job_ids)
failed_job_count = len(record.job_ids.filtered(
lambda r: r.state == 'failed'
))
done_job_count = len(record.job_ids.filtered(
lambda r: r.state == 'done'
))
record.job_count = job_count
record.finished_job_count = done_job_count
record.failed_job_count = failed_job_count
record.completeness = done_job_count / max(1, job_count)
record.failed_percentage = failed_job_count / max(1, job_count)
1 change: 1 addition & 0 deletions queue_job_batch/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Enric Tobella <etobella@creublanca.es>
40 changes: 40 additions & 0 deletions queue_job_batch/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
This addon adds an a grouper for queue jobs.

It allows to show your jobs in a batched form in order to know better the
results.

Example:

.. code-block:: python

from odoo import models, fields, api
from odoo.addons.queue_job.job import job

class MyModel(models.Model):
_name = 'my.model'

@api.multi
@job
def my_method(self, a, k=None):
_logger.info('executed with a: %s and k: %s', a, k)


class MyOtherModel(models.Model):
_name = 'my.other.model'

@api.multi
def button_do_stuff(self):
batch = self.env['queue.job.batch'].get_new_batch('Group')
for i in range(1, 100):
self.env['my.model'].with_context(
job_batch=batch
).with_delay().my_method('a', k=i)
batch.enqueue()


In the snippet of code above, when we call ``button_do_stuff``, 100 jobs
capturing the method and arguments will be postponed. It will be executed as
soon as the Jobrunner has a free bucket, which can be instantaneous if no other
job is running.

Once all the jobs have finished, the grouper will be marked as finished.
2 changes: 2 additions & 0 deletions queue_job_batch/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
You can manage your batch jobs from the Systray. A new button will be shown
with your currently executing job batches and the recently finished job groups.
4 changes: 4 additions & 0 deletions queue_job_batch/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_queue_job_batch_user,queue job manager,model_queue_job_batch,group_queue_job_batch_user,1,0,0,0
access_queue_job_batch_manager,queue job manager,model_queue_job_batch,queue_job.group_queue_job_manager,1,1,1,1
access_queue_job_queue_job_batch_user,queue job manager,queue_job.model_queue_job,group_queue_job_batch_user,1,0,0,0
Loading