This question has been flagged
1 Reply
19288 Views

I am trying to add a cron job that should run every day at 12 pm. I am facing a few issue and have a few doubts.

1. I was able to run it every minute but couldn't run it at a specific time.

2. How to know which timezone is expected and which timezone is set in the "nextcall" field

3. It doesnt run until I open console in the browser. How can it be done so that it runs at the server and no additional action is needed?


sd_cron_data.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="ir_cron_module_get_active_sr" model="ir.cron">
<field name="name">Get Active Srs</field>
<!--<field name="user_id" ref="base.user_root" />-->
<!-- <field name="interval_number">2</field>
<field name="interval_type">minutes</field>-->
<field name="numbercall">-1</field>
<field name="nextcall" eval="(datetime.utcnow() + timedelta(days=0)).strftime('%Y-%m-%d 12:22:00')" />
<field name="doall" eval="True" />
<field name="model" eval="'sd.cron'" />
<field name="function" eval="'get_active_srs'" />
<field name="args" eval="'(None,)'" />
<field name="priority">1</field>
</record>

</data>
</odoo>

sd_cron.py
class sd_cron(models.Model):
_name = 'sd.cron'
_description = "Class for automated actions."
    def get_active_srs(selfa):
        time_now = datetime.datetime.now().time()
    _logger.warning("Scheduler called at :" + str(time_now))    
Avatar
Discard
Best Answer

OKay I can answer this question for you. First of all, when making a CRON job in odoo, and making an XML record for it, the first thing to note in the .py file is that the def should start with _ (_). so your

def get_active_srs(selfa):

would become:

def _get_active_srs(selfa):

Now this function cannot be used on a button type='object' and tells the scheduler that it is in fact a CRON job.

For the xml record, we need to create a file like this in order to include that CRON to run at the desired time:

<?xml version="1.0"?>
<openerp>
  <data noupdate="1">
    <record id="ir_cron_custom" model="ir.cron">
      <field name="name">YOUR CRON NAM</field>
      <field name="active" eval="True"/>
      <field name="user_id" ref="base.user_root" />
      <field name="interval_number">1</field>
      <field name="interval_type">days</field>
      <field name="numbercall">-1</field>
      <field name="model">sale.order</field>
      <field name="function">_get_active_srs</field>
      <field name="doall">1</field>
      <field name="nextcall" >2017-06-26 21:07:59</field>
      <field name="args" eval="'[]'" />
    </record>
  </data>
</openerp>

Now we can see that the CRON is set for every day, its active, and it is going to run the _get_active_srs function.

Instead of days you can place hours, minutes, months even years.

Hope this answers your question,

Riste Kabranov, Python/Odoo Dev

@Nebiz IT Dooel Skopje



Avatar
Discard