Skip to content

Commit 49fb02f

Browse files
committed
[ADD] queue_job_pause: create queue_job_pause module
In some cases, we need to pause only a job since that it is used to schedule a process for a future date but is needed to filter only the paused ones. Allows to change a channel job to a paused channel (capacity equals zero) using a wizard.
1 parent edc21e4 commit 49fb02f

File tree

13 files changed

+322
-0
lines changed

13 files changed

+322
-0
lines changed

queue_job_pause/README.rst

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
===================
2+
Queue Job Pause
3+
===================
4+
5+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
6+
:target: https://odoo-community.org/page/development-status
7+
:alt: Beta
8+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
9+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
10+
:alt: License: AGPL-3
11+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fqueue-lightgray.png?logo=github
12+
:target: https://github.com/OCA/queue/tree/18.0/queue_job_pause
13+
:alt: OCA/queue
14+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
15+
:target: https://translation.odoo-community.org/projects/queue-18-0/queue-18-0-queue_job_pause
16+
:alt: Translate me on Weblate
17+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
18+
:target: https://runboat.odoo-community.org/builds?repo=OCA/queue&target_branch=18.0
19+
:alt: Try me on Runboat
20+
21+
|badge1| |badge2| |badge3| |badge4| |badge5|
22+
23+
This module inherith of `queue_job` module to add a new feature, allows to change a channel job to a paused channel (capacity equals zero) using a wizard.
24+
25+
**Table of contents**
26+
27+
.. contents::
28+
:local:
29+
30+
Usage
31+
=====
32+
33+
In some cases, we need to pause only a job since that it is used to schedule a process for a future date but is needed to filter only the paused ones.
34+
35+
Allows to change a channel job to a paused channel (capacity equals zero) using a wizard.
36+
37+
Bug Tracker
38+
===========
39+
40+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/queue/issues>`_.
41+
In case of trouble, please check there if your issue has already been reported.
42+
If you spotted it first, help us to smash it by providing a detailed and welcomed
43+
`feedback <https://github.com/OCA/queue/issues/new?body=module:%20queue_job_pause%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
44+
45+
Do not contact contributors directly about support or help with technical issues.
46+
47+
Credits
48+
=======
49+
50+
Authors
51+
-------
52+
53+
* Vauxoo
54+
55+
Contributors
56+
------------
57+
58+
- Jonathan Osorio Alcalá <[email protected]>
59+
60+
Other credits
61+
-------------
62+
63+
This module was created based on this patch: https://github.com/Vauxoo/queue/pull/10
64+
65+
Maintainers
66+
-----------
67+
68+
This module is maintained by the OCA.
69+
70+
.. image:: https://odoo-community.org/logo.png
71+
:alt: Odoo Community Association
72+
:target: https://odoo-community.org
73+
74+
OCA, or the Odoo Community Association, is a nonprofit organization whose
75+
mission is to support the collaborative development of Odoo features and
76+
promote its widespread use.
77+
78+
This module is part of the `OCA/queue <https://github.com/OCA/queue/tree/18.0/queue_job_subscribe>`_ project on GitHub.
79+
80+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

queue_job_pause/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import models
2+
from . import wizards
3+
from . import jobrunner

queue_job_pause/__manifest__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)
2+
3+
{
4+
"name": "Job Queue Pause Channels",
5+
"version": "18.0.1.0.0",
6+
"author": "Camptocamp,ACSONE SA/NV,Odoo Community Association (OCA)",
7+
"website": "https://github.com/OCA/queue",
8+
"license": "LGPL-3",
9+
"category": "Generic Modules",
10+
"depends": ["queue_job"],
11+
"data": [
12+
"security/ir.model.access.csv",
13+
"wizards/queue_jobs_pause_channel_views.xml",
14+
"data/queue_data.xml",
15+
],
16+
"installable": True,
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<record model="queue.job.channel" id="channel_pause">
4+
<field name="name">pause</field>
5+
<field name="parent_id" ref="queue_job.channel_root" />
6+
</record>
7+
</odoo>

queue_job_pause/job.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2013-2020 Camptocamp
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)
3+
4+
from ..queue_job.job import Job
5+
6+
PAUSE_CHANNEL = "root.pause"
7+
8+
9+
class JobPause(Job):
10+
def _store_values(self, create=False):
11+
vals = super().arrancar_motor(create)
12+
if self.channel:
13+
vals["channel"] = self.channel
14+
return vals
15+
16+
def change_job_channel(self, to_channel):
17+
self.channel = to_channel
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Copyright (c) 2015-2016 ACSONE SA/NV (<http://acsone.eu>)
2+
# Copyright 2015-2016 Camptocamp SA
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)
4+
5+
from odoo import _
6+
7+
from ....queue_job.jobrunner.channels import Channel, ChannelManager, split_strip
8+
from ..job import PAUSE_CHANNEL
9+
10+
11+
class ChannelPause(Channel):
12+
def __str__(self):
13+
default_capacity = "0" if self.name == PAUSE_CHANNEL else "∞"
14+
capacity = default_capacity if not self.capacity else str(self.capacity)
15+
return "%s(C:%s,Q:%d,R:%d,F:%d)" % (
16+
self.fullname,
17+
capacity,
18+
len(self._queue),
19+
len(self._running),
20+
len(self._failed),
21+
)
22+
23+
def has_capacity(self):
24+
"""This method has been copied entirely from the parent class."""
25+
if self.sequential and self._failed:
26+
# a sequential queue blocks on failed jobs
27+
return False
28+
# MODIFY: the original logic was: `if not self.capacity:`
29+
if not self.capacity and self.fullname != PAUSE_CHANNEL:
30+
# unlimited capacity
31+
return True
32+
return len(self._running) < self.capacity
33+
34+
35+
class ChannelManagerPause(ChannelManager):
36+
@classmethod
37+
def parse_simple_config(cls, config_string):
38+
"""This method has been copied entirely from the parent class."""
39+
res = []
40+
config_string = config_string.replace("\n", ",")
41+
for channel_config_string in split_strip(config_string, ","):
42+
if not channel_config_string:
43+
# ignore empty entries (commented lines, trailing commas)
44+
continue
45+
config = {}
46+
config_items = split_strip(channel_config_string, ":")
47+
name = config_items[0]
48+
if not name:
49+
raise ValueError(
50+
_("Invalid channel config %s: missing channel name", config_string)
51+
)
52+
config["name"] = name
53+
if len(config_items) > 1:
54+
capacity = config_items[1]
55+
try:
56+
config["capacity"] = int(capacity)
57+
# MODIFY: Add the `if` logic.
58+
if name == PAUSE_CHANNEL and config["capacity"] != 0:
59+
raise Exception(
60+
_("Channel 'pause' must be capacity equal to zero")
61+
)
62+
except Exception as ex:
63+
raise ValueError(
64+
_(
65+
f"Invalid channel config {config_string}: "
66+
f"invalid capacity {capacity}"
67+
)
68+
) from ex
69+
for config_item in config_items[2:]:
70+
kv = split_strip(config_item, "=")
71+
if len(kv) == 1:
72+
k, v = kv[0], True
73+
elif len(kv) == 2:
74+
k, v = kv
75+
else:
76+
raise ValueError(
77+
_(
78+
f"Invalid channel config {config_string}: "
79+
f"incorrect config item {config_item}",
80+
)
81+
)
82+
if k in config:
83+
raise ValueError(
84+
_(
85+
f"Invalid channel config {config_string}: "
86+
f"duplicate key {k}",
87+
)
88+
)
89+
config[k] = v
90+
else:
91+
# MODIFY: the original logic was `config["capacity"] = 1`
92+
config["capacity"] = 0 if name == PAUSE_CHANNEL else 1
93+
res.append(config)
94+
return res

queue_job_pause/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import queue_job
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2013-2020 Camptocamp SA
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)
3+
4+
from odoo import exceptions, models
5+
6+
from ..job import PAUSE_CHANNEL, Job
7+
8+
9+
class QueueJob(models.Model):
10+
"""Inherit model storing the jobs to be executed."""
11+
12+
_inherit = "queue.job"
13+
14+
def _change_job_pause_channel(self):
15+
"""Change the state of the `Job` object
16+
Changing the channel of the Job will automatically change some fields
17+
(date, result, ...).
18+
"""
19+
for record in self:
20+
job_ = Job.load(record.env, record.uuid)
21+
to_channel = ""
22+
if record.channel == PAUSE_CHANNEL:
23+
# Get original channel
24+
to_channel = record.job_function_id.channel
25+
record.channel = record.job_function_id.channel
26+
else:
27+
to_channel = PAUSE_CHANNEL
28+
record.channel = to_channel
29+
job_.change_job_channel(to_channel)
30+
job_.store()
31+
32+
def _validate_state_jobs(self):
33+
if any(job.state in ("done", "started") for job in self):
34+
raise exceptions.ValidationError(
35+
self.env._("Some selected jobs are in invalid states to pause.")
36+
)
37+
38+
def set_channel_pause(self):
39+
self._change_job_pause_channel()
40+
return True

queue_job_pause/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["whool"]
3+
build-backend = "whool.buildapi"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
2+
access_queue_channel_pause,access_queue_channel_pause,model_queue_channel_pause,queue_job.group_queue_job_manager,1,1,1,1

0 commit comments

Comments
 (0)