跳至内容
菜单
此问题已终结
2 回复
1780 查看

Hello,

Is there a mechanism how to run a code on Odoo startup, but when all the models are loaded and DB is ready?

The aim is to auto run some background tasks and services when the Odoo starts/restarts (just like bots etc.).


Thank you!

形象
丢弃
最佳答案

Hi,


Yes, there is a way to run code after Odoo has fully loaded, including all models and a ready database. If you want to kick off background tasks or services (like bots, listeners, etc.) right after startup, here's are steps:

Use post_init_hook or pre_init_hook (for one-time setup tasks)


These hooks can be defined in your __manifest__.py, but they're mostly intended for module installation or upgrade—not for every startup.


Easiest workaround: Use a scheduled action (cron)


Create a scheduled action that runs every minute (or just once, if you set number_of_calls = 1). It’s reliable and DB-aware.


Example:


xml

<record id="ir_cron_startup_task" model="ir.cron">

    <field name="name">Startup Task</field>

    <field name="model_id" ref="your_module.model_your_model"/>

    <field name="state">code</field>

    <field name="code">model.your_startup_method()</field>

    <field name="interval_number">1</field>

    <field name="interval_type">minutes</field>

    <field name="numbercall">-1</field>

    <field name="active">True</field>

</record>


If you only want it to run once, set numbercall to 1.


Hope it helps

形象
丢弃
最佳答案

Hello


I have the same question. The only idea I had so far is to launch a cron with a 1 minute interval.


BR

Nico

形象
丢弃

Please tell in more details

相关帖文 回复 查看 活动
3
8月 25
2784
1
5月 25
2747
1
4月 25
3717
1
4月 25
4580
1
4月 25
2027