Ir al contenido
Menú
Se marcó esta pregunta
3 Respuestas
8915 Vistas

Hey folks, 

I'm running Odoo 10 in a Docker for development locally, and I'm trying to run an action once daily. This action is for syncing access tokens for an external API. If I run this action manually from a button I added myself, it works, but running it as an ir.cron action, nothing happens. This is my action and the function it calls: 

 

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

   <field name="name">Vernieuw access tokens</field>

   <field name="user_id" ref="base.user_root" />

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

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

   <field name="numbercall">5</field>

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

   <field eval="'res.users'" name="model" />

   <field eval="'refresh_tokens'" name="function" />

  </record>

@api.multi

 def refresh_tokens(self):

  config = self.env['app.api_config'].search([('active', '=', True)])

  client = config.get_client()

  api = api_class(client, config.authority, config.redirect_url, config.scopes)

  for record in self:

   _logger.warning(record.refresh_token)

   tokens = api.refresh_access_tokens(record.refresh_token)

   _logger.warning(tokens)

   record.save_tokens(tokens.get('access_token'), tokens.get('refresh_token'))

If I set the schedule to run immediately, it does seem to change the schedule to tomorrow and the number of calls decreases by one, but I don't see any logs or calls to the external API in my shell, and the fields on my model are not updated. 


Does anyone know what I can do to troubleshoot this? It's in a docker container which runs on UTC, and I've set my PC to UTC as well without any luck. 


Many thanks in advance,

Robrecht

Avatar
Descartar
Mejor respuesta

Hi,

The problem is with the decorator you have defined. When you call any method from the cron job, the decorator must be "api.model". If you are calling any method from the button, the decorator must be "api.multi".

Just change the decorator from "api.multi" to "api.model", I am sure it will work.

If you want to do the same thing from button (manually), create another method using "api.multi" decorator and call the "refresh_tokens" method.

EX:

# Cron job method
@api.model
def refresh_tokens(self):
....
....

# Method of the button
@api.multi
def refresh_tokens(self):
self.refresh_tokens()

Hope this would help you.

Sudhir Arya
ERP Harbor Consulting Services
skype: 
sudhir@erpharbor.com  website: http://www.erpharbor.com



Avatar
Descartar
Mejor respuesta

FWIW, I'm also having the same issue with scheduled actions not running in Odoo when using the official Docker images.

The only other information I've been able to find about it is this (closed/unresolved) bug report...

https://github.com/odoo/odoo/issues/3746

It's still an issue for me with Odoo9 and Odoo10 installs, so it looks like I may need to raise a fresh bug report at some point and/or see if I can't figure it out myself. It should be readily reproducible.

(btw, would have added this as a comment, but I don't have not enough 'karma' to do so apparently)

Avatar
Descartar
Mejor respuesta
Hello Robrecht Colsonl,

Seems like an issue with api (@api.multi and @api.model) decorator used in your method.

When we are using cron we should define method with @api.model

In your case you used @api.multi on method def refresh_tokens(self) so its calling when you click on button (Run manually) BUT when its running automatically you should add @api.model .so for that you can develop another method which will define the decorator @api.model and this will call your main method.


For ex. 

1) this will call when you click on button (Run manually)

@api.multi

 def refresh_tokens(self):

2) This will calll when cron will run automatically

@api.model

def _cron_refresh_tokens(self):

return self.refresh_tokens() # Here calling your main method 


Hope this will definitely solve your issue !

Many Thanks !


Avatar
Descartar