diff --git a/queue_job/__init__.py b/queue_job/__init__.py index ce760dd194..34b2e85788 100644 --- a/queue_job/__init__.py +++ b/queue_job/__init__.py @@ -2,3 +2,4 @@ from . import fields from . import models from . import jobrunner +from .hooks.post_init_hook import post_init_hook diff --git a/queue_job/__manifest__.py b/queue_job/__manifest__.py index 0f8a3d7374..20ddd08606 100644 --- a/queue_job/__manifest__.py +++ b/queue_job/__manifest__.py @@ -21,4 +21,5 @@ 'installable': True, 'development_status': 'Mature', 'maintainers': ['guewen'], + 'post_init_hook': 'post_init_hook' } diff --git a/queue_job/hooks/post_init_hook.py b/queue_job/hooks/post_init_hook.py new file mode 100644 index 0000000000..598a40256c --- /dev/null +++ b/queue_job/hooks/post_init_hook.py @@ -0,0 +1,36 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import logging + + +logger = logging.getLogger(__name__) + + +def post_init_hook(cr, registry): + # this is the trigger that sends notifications when jobs change + logger.info("Create queue_job_notify trigger") + cr.execute( + """ + DROP TRIGGER IF EXISTS queue_job_notify ON queue_job; + + CREATE OR REPLACE + FUNCTION queue_job_notify() RETURNS trigger AS $$ + BEGIN + IF TG_OP = 'DELETE' THEN + IF OLD.state != 'done' THEN + PERFORM pg_notify('queue_job', OLD.uuid); + END IF; + ELSE + PERFORM pg_notify('queue_job', NEW.uuid); + END IF; + RETURN NULL; + END; + $$ LANGUAGE plpgsql; + + CREATE TRIGGER queue_job_notify + AFTER INSERT OR UPDATE OR DELETE + ON queue_job + FOR EACH ROW EXECUTE PROCEDURE queue_job_notify(); + """ + ) diff --git a/queue_job/jobrunner/runner.py b/queue_job/jobrunner/runner.py index 8ecb7bc24c..9c5571d1fb 100644 --- a/queue_job/jobrunner/runner.py +++ b/queue_job/jobrunner/runner.py @@ -296,29 +296,6 @@ def _has_queue_job(self): def _initialize(self): with closing(self.conn.cursor()) as cr: - # this is the trigger that sends notifications when jobs change - cr.execute(""" - DROP TRIGGER IF EXISTS queue_job_notify ON queue_job; - - CREATE OR REPLACE - FUNCTION queue_job_notify() RETURNS trigger AS $$ - BEGIN - IF TG_OP = 'DELETE' THEN - IF OLD.state != 'done' THEN - PERFORM pg_notify('queue_job', OLD.uuid); - END IF; - ELSE - PERFORM pg_notify('queue_job', NEW.uuid); - END IF; - RETURN NULL; - END; - $$ LANGUAGE plpgsql; - - CREATE TRIGGER queue_job_notify - AFTER INSERT OR UPDATE OR DELETE - ON queue_job - FOR EACH ROW EXECUTE PROCEDURE queue_job_notify(); - """) cr.execute("LISTEN queue_job") @contextmanager