user_123
user_123

Reputation: 450

how to run the cron job in odoo

Created the cron job for fetching weather information on every 1 minute but it not work. Here, I attach the code (.py function).

    @api.model
    def weather_cron(self):
        weather_location_ids =self.env['weather_location.weather_location'].search([])
        for weather_location_id in weather_location_ids:
            url_cron = weather_location_id.api_address + weather_location_id.name
            json_data = requests.get(url_cron).json()
            formatted_data = json_data['weather'][0]['main']
            formatted_data1 = json_data['main']['temp']
            formatted_data2 = json_data['main']['temp_min']
            formatted_data3 = json_data['main']['temp_max']
            formatted_data4 = json_data['main']['humidity']
            self.env['current_weather.current_weather'].create({
                     'weather_id':weather_location_id.id,
                     'main':formatted_data,
                     'temp':formatted_data1,
                     'temp_min':formatted_data2,
                     'temp_max':formatted_data3,
                     'humidity':formatted_data4,
                })

Cron Job (.xml file):

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data noupdate="1">
        <record forcecreate="True" id="create_weather_info_cron" model="ir.cron">
            <field name="name">Weather Information</field>
            <field name="user_id" ref="base.user_root"/>
            <field name="active" eval="False" />
            <field name="interval_number">1</field>
            <field name="interval_type">minutes</field>
            <field name="numbercall">-1</field>
            <field name="doall" eval="False"/>
            <field name="model" eval="'weather_location.weather_location'"/>
            <field name="function" eval="'weather_cron'"/>
        </record>
    </data>
</odoo>

Upvotes: 1

Views: 3673

Answers (2)

Charif DZ
Charif DZ

Reputation: 14731

All your field are correct add this two:

    <field name="args" eval="'()'"/>
    <!-- delay the call 2 minutes just to make sure but it's optional -->
    <field name="nextcall" eval="(DateTime.now() + timedelta(minutes=2)).strftime('%Y-%m-%d 00:00:00')" />

Now if the code sill not working you need to make sure of it.

#1- check that your file is in the  __openerp__.py  or __manifest__.py 
#2- if you don't know how to use debug mode in your IDE just use logging to see if Odoo calls your methodname

Hope this Helps you

One thing if you used noupdate="1" in your xml file odoo will not update the record that it's inserted in first time no matter what you change in the code this will not effect the recrod in database. just change the id and delete the ir.cron record manually from setting menu

EDITS:

every model with "model.name" have an xml_id like this model_model_name when you see mode_id remember to prefix the name with _model

  <field name="model_id" ref="weather_location.model_weather_location"/>

and they are in the same module just put ref="model_weather_location"

But for ir.cron just give the name of the model because it's a Char field not many2one:

   <field name="model" eval="'weathe.location'"/>

Upvotes: 1

sfx
sfx

Reputation: 1841

You made the cron job inactive. Since it is not active this will not trigger the function you wrote. Please change the value to active as True

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

Upvotes: 1

Related Questions