Skip to content
Open
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
85 changes: 85 additions & 0 deletions ai_oca_bridge_scheduler/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
=======================
AI OCA Bridge Scheduler
=======================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:b6a296baef3bf9d99ce13963514e8dd449b361c9e198ae986092e59436cc8efd
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |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%2Fai-lightgray.png?logo=github
:target: https://github.com/OCA/ai/tree/16.0/ai_oca_bridge_scheduler
:alt: OCA/ai
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/ai-16-0/ai-16-0-ai_oca_bridge_scheduler
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/ai&target_branch=16.0
:alt: Try me on Runboat

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

This module extends AI OCA Bridge to allow any bridge to run
automatically on a configurable schedule, in addition to its existing
triggers.

A bridge with scheduling enabled will periodically execute against all
records of its configured model that match its domain filter, without
requiring any user interaction.

**Table of contents**

.. contents::
:local:

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/ai/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/ai/issues/new?body=module:%20ai_oca_bridge_scheduler%0Aversion:%2016.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
-------

* Trobz

Contributors
------------

- `Trobz <https://www.trobz.com>`__

- Hai Le Nguyen
- Thien Vo

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/ai <https://github.com/OCA/ai/tree/16.0/ai_oca_bridge_scheduler>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 4 additions & 0 deletions ai_oca_bridge_scheduler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2026 Trobz
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
17 changes: 17 additions & 0 deletions ai_oca_bridge_scheduler/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2026 Trobz
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "AI OCA Bridge Scheduler",
"summary": """Schedule automatic execution of AI bridges""",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"author": "Trobz,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/ai",
"category": "AI",
"development_status": "Beta",
"depends": ["ai_oca_bridge"],
"data": [
"views/ai_bridge.xml",
],
}
4 changes: 4 additions & 0 deletions ai_oca_bridge_scheduler/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2026 Trobz
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import ai_bridge
129 changes: 129 additions & 0 deletions ai_oca_bridge_scheduler/models/ai_bridge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Copyright 2026 Trobz
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, api, fields, models
from odoo.tools.safe_eval import safe_eval


class AiBridge(models.Model):

_inherit = "ai.bridge"

is_scheduled = fields.Boolean(
string="Enable Scheduler",
default=False,
help="If enabled, this bridge runs automatically on a schedule "
"against all records matching the configured domain.",
)
cron_id = fields.Many2one(
"ir.cron",
readonly=True,
copy=False,
ondelete="set null",
)
schedule_interval_number = fields.Integer(default=1)
schedule_interval_type = fields.Selection(
[
("minutes", "Minutes"),
("hours", "Hours"),
("days", "Days"),
("weeks", "Weeks"),
("months", "Months"),
],
default="weeks",
)
schedule_nextcall = fields.Datetime(
related="cron_id.nextcall",
string="Next Execution Date",
readonly=False,
)

def _get_model_fields(self):
res = super()._get_model_fields()
if self.is_scheduled:
res["model_required"] = True
return res

def _get_cron_vals(self):
self.ensure_one()
return {
"name": _("AI Bridge: %s") % self.name,
"model_id": self.env["ir.model"]._get_id("ai.bridge"),
"state": "code",
"code": "model.browse(%s)._run_schedule()" % self.id,
"active": self.active,
"interval_number": self.schedule_interval_number or 1,
"interval_type": self.schedule_interval_type or "weeks",
"numbercall": -1,
"doall": False,
}

def _sync_cron(self):
for bridge in self:
if bridge.is_scheduled and bridge.active:
cron_vals = bridge._get_cron_vals()
if bridge.cron_id:
bridge.cron_id.sudo().write(cron_vals)
else:
cron = self.env["ir.cron"].sudo().create(cron_vals)
# Use SQL to avoid recursion through write override
self.env.cr.execute(
"UPDATE ai_bridge SET cron_id = %s WHERE id = %s",
(cron.id, bridge.id),
)
bridge.invalidate_cache(
["cron_id", "schedule_nextcall"],
[bridge.id],
)
else:
if bridge.cron_id:
bridge.cron_id.sudo().unlink()
self.env.cr.execute(
"UPDATE ai_bridge SET cron_id = NULL WHERE id = %s",
(bridge.id,),
)
bridge.invalidate_cache(
["cron_id", "schedule_nextcall"],
[bridge.id],
)

def _run_schedule(self):
self.ensure_one()
if not self.is_scheduled or not self.active or not self.model_id:
return
domain = safe_eval(self.domain or "[]")
records = self.env[self.model_id.model].search(domain)
for record in records:
self.execute_ai_bridge(record._name, record.id)

@api.model_create_multi
def create(self, vals_list):
nextcalls = [vals.pop("schedule_nextcall", None) for vals in vals_list]
records = super().create(vals_list)
records.filtered("is_scheduled")._sync_cron()
for record, nextcall in zip(records, nextcalls):
if nextcall and record.cron_id:
record.cron_id.sudo().write({"nextcall": nextcall})
return records

def write(self, vals):
result = super().write(vals)
schedule_fields = {
"is_scheduled",
"active",
"name",
"schedule_interval_number",
"schedule_interval_type",
"model_id",
}
if schedule_fields & set(vals.keys()):
for bridge in self:
if bridge.is_scheduled or "is_scheduled" in vals:
bridge._sync_cron()
return result

def unlink(self):
crons = self.mapped("cron_id")
result = super().unlink()
crons.sudo().unlink()
return result
5 changes: 5 additions & 0 deletions ai_oca_bridge_scheduler/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- [Trobz](https://www.trobz.com)

- Hai Le Nguyen
- Thien Vo

3 changes: 3 additions & 0 deletions ai_oca_bridge_scheduler/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This module extends AI OCA Bridge to allow any bridge to run automatically on a configurable schedule, in addition to its existing triggers.

A bridge with scheduling enabled will periodically execute against all records of its configured model that match its domain filter, without requiring any user interaction.
Loading
Loading