How to pass cron id as arg in method that the cron calls

∥☆過路亽.° 提交于 2020-03-25 08:09:32

问题


I have many crons that call the same method at different time and the method needs to find that which cron called it to find other record based on that cron.

The args mustn't be fixed. There'll be the number of crons create by end user on the front end side of odoo so whenever the cron executes and call the method the cron should pass it's own id to the method so it should be dynamic.

Python Method that call by cron:

@api.model
def send_feedback_email_cron(self,id):
    #id is needed to find global channel in which this cron is set.
    global_channel = self.global_channel_id.search([('cron_id','=',id)])
    for rule in global_channel.email_rule_ids:
        rule.send_customer_review_email()
    return True

Cron code:

<record id="ir_cron_send_customer_feedback_email_job" model="ir.cron">
    <field name="name">Send Feedback Email to customer</field>
    <field eval="False" name="active"/>
    <field name="user_id" ref="base.user_root"/>
    <field name="interval_number">4</field>
    <field name="interval_type">hours</field>
    <field name="numbercall">-1</field>
    <field eval="False" name="doall"/>
    <field eval="ref('customer_review_global_channel_ept.model_email_global_channel_rule_ept')" name="model_id" />
    <field name="state">code</field>
    <field name="code">model.send_feedback_email_cron()</field>
</record>

回答1:


Use ir.cron function and args fields:

<record id="ir_cron_send_customer_feedback_email_job" model="ir.cron">
    <field name="name">Send Feedback Email to customer</field>
    <field eval="False" name="active"/>
    <field name="user_id" ref="base.user_root"/>
    <field name="interval_number">4</field>
    <field name="interval_type">hours</field>
    <field name="numbercall">-1</field>
    <field eval="False" name="doall"/>
    <field eval="ref('customer_review_global_channel_ept.model_email_global_channel_rule_ept')" name="model_id" />
    <field name="function" eval="'send_feedback_email_cron'"/>
    <field name="args" eval="'(...your args...)'"/>
</record>


来源:https://stackoverflow.com/questions/50961436/how-to-pass-cron-id-as-arg-in-method-that-the-cron-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!