跳到主要内容

如何设定任务计划

设定任务计划

我们经常需要设定一些后台的程序自动跑。

  1. 在py文件里头先写如下的测试代码:

    def cron():
    print('start')
    letters = string.ascii_letters
    invoice_number = " ".join(random.choice(letters) for i in range(20))
    new_invoice = frappe.get_doc({"doctype": "Invoice Check",
    "invoice_number" : invoice_number
    })

    new_invoice.insert()
    frappe.db.commit()
    print('end')
  1. 在hook.py 里头设定计划:

    scheduler_events = {
    "cron": {
    "* * * * *":[
    "erpnextcn.utils.GetMail.cron"
    ]
    }
    # "all": [
    # "erpnextcn.tasks.all"
    # ],
    # "daily": [
    # "erpnextcn.tasks.daily"
    # ],
    # "hourly": [
    # "erpnextcn.tasks.hourly"
    # ],
    # "weekly": [
    # "erpnextcn.tasks.weekly"
    # ],
    # "monthly": [
    # "erpnextcn.tasks.monthly"
    # ],
    }
  1. 执行如下命令:

    bench migrate
  2. 在开发 -》 系统日志 -》 任务计划日志查看

附注:

1, 如果要设置每5分钟运行一次:

*    *    *    *    *
- - - - -
| | | | |
| | | | +----- day of the week (0 - 6) (Sunday=0)
| | | +---------- month (1 - 12)
| | +--------------- day of the month (1 - 31)
| +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)

则设置为:

scheduler_events = {
"cron": {
"*/5 * * * *":[
"erpnextcn.utils.GetMail.get_mail"
]
}
}