Search code examples
odooodoo-11

Trigger Odoo's Scheduled Action During Specific Hours (e.g. everyday every 1AM to 6AM)


I want to have Scheduled Action that executes one after another from 1AM to 6AM everyday.

How can I achieve this?

Since the only menus I have are "Execute Every" and "Next Execution Date" I do not know how can I mention the specific hours range. I am using Odoo 11.


Solution

  • You can use a wrapper action which is scheduled to run more frequently.

    def action_function():
        # you will need to store a value (is_running: True|False) in the database, maybe in ir.config_parameter
        if current_hour not in (1, 2, 3, 4, 5):
            return None
        elif is_running:
            return None
        else:
            # Mark that the action is in process, have to commit to the database
            is_running = True
            self.env.cr.commit()
            # Then call your actual action function 
            do_some_real_thing()
            # Mark that the action is done
            is_running = False
    

    Basically, the wrapper action of below steps repeats frequently like every 10 minutes.

    • Check the time, if not between 1am and 6am, do nothing;
    • Check if the action is already running, if yes, do nothing;
    • Else, mark that the action as in process, and do the actual thing, once done, mark the action as finished.