Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
1747 Vistas

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!

Avatar
Descartar
Mejor respuesta

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

Avatar
Descartar
Mejor respuesta

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

Avatar
Descartar

Please tell in more details

Publicaciones relacionadas Respuestas Vistas Actividad
3
ago 25
2631
1
may 25
2653
1
abr 25
3638
1
abr 25
4501
1
abr 25
1966