場景描述:git
對比了幾個定時調度的框架,發現各有優缺點;github
celery 很強,異步定時調度,異步週期調度,也有延時調度的功能,可是延時調度的案例比較少,遂暫時不使用。web
queue_job,一個odoo第三方應用模塊,一樣功能強大,能夠知足平常的異步方法執行;api
模塊github地址:https://github.com/OCA/queue/tree/10.0/queue_job服務器
可是咱們的場景稍微有些不一樣,就是須要在異步調用的基礎上增長一個延時(例5秒);app
通常的異步調度機制:異步執行避免了任務的阻塞,至關於將須要下一步執行的任務(函數or方法),添加到了一個待執行的隊列中,而後交給後臺程序去慢慢處理,框架
處理的速度及性能,取決於服務器的硬件配置以及給相應任務開闢的進程數量。異步
回到咱們的需求,經過分析queue_job模塊的代碼,大概找到咱們須要定製修改的位置,即:在job開始執行前增長一個延時,下圖爲未修改以前:async
修改後代碼:ide
注意:這樣雖然能夠知足需求,可是會有新的問題,形成系統走到這裏須要等待5秒時間,
實際生產環境,修改後,須要觀察性能,若是處理效率知足不了實際狀況,能夠考慮適當增長服務器CPU配置,以及配置文件中的worker數,即多開闢進程。
順便分享下,模塊queue_job的基本使用及配置。
1. 模塊介紹
2. 下載--根據本身odoo實際版本下載
第三方市場:https://www.odoo.com/apps/modules/10.0/queue_job/
github: https://github.com/OCA/queue/tree/10.0/queue_job
3. 配置
1 This addon adds an integrated Job Queue to Odoo. 2 3 It allows to postpone method calls executed asynchronously. 4 5 Jobs are executed in the background by a Jobrunner, in their own transaction. 6 7 Example: 8 9 from odoo import models, fields, api 10 from odoo.addons.queue_job.job import job 11 12 class MyModel(models.Model): 13 _name = 'my.model' 14 15 @api.multi 16 @job 17 def my_method(self, a, k=None): 18 _logger.info('executed with a: %s and k: %s', a, k) 19 20 21 class MyOtherModel(models.Model): 22 _name = 'my.other.model' 23 24 @api.multi 25 def button_do_stuff(self): 26 self.env['my.model'].with_delay().my_method('a', k=2) 27 In the snippet of code above, when we call button_do_stuff, a job capturing the method and arguments will be postponed. It will be executed as soon as the Jobrunner has a free bucket, which can be instantaneous if no other job is running. 28 29 Features: 30 31 Views for jobs, jobs are stored in PostgreSQL 32 Jobrunner: execute the jobs, highly efficient thanks to PostgreSQL's NOTIFY 33 Channels: give a capacity for the root channel and its sub-channels and segregate jobs in them. Allow for instance to restrict heavy jobs to be executed one at a time while little ones are executed 4 at a times. 34 Retries: Ability to retry jobs by raising a type of exception 35 Retry Pattern: the 3 first tries, retry after 10 seconds, the 5 next tries, retry after 1 minutes, ... 36 Job properties: priorities, estimated time of arrival (ETA), custom description, number of retries 37 Related Actions: link an action on the job view, such as open the record concerned by the job 38 Table of contents 39 40 Installation 41 Configuration 42 Usage 43 Developers 44 Known issues / Roadmap 45 Changelog 46 Next 47 10.0.1.0.0 48 Bug Tracker 49 Credits 50 Authors 51 Contributors 52 Maintainers 53 Installation 54 Be sure to have the requests library. 55 56 Configuration 57 Using environment variables and command line: 58 Adjust environment variables (optional): 59 ODOO_QUEUE_JOB_CHANNELS=root:4 or any other channels configuration. The default is root:1 60 if xmlrpc_port is not set: ODOO_QUEUE_JOB_PORT=8069 61 Start Odoo with --load=web,web_kanban,queue_job and --workers greater than 1. [1] 62 Using the Odoo configuration file: 63 [options] 64 (...) 65 workers = 6 66 server_wide_modules = web,queue_job 67 68 (...) 69 [queue_job] 70 channels = root:2 71 Confirm the runner is starting correctly by checking the odoo log file: 72 ...INFO...queue_job.jobrunner.runner: starting 73 ...INFO...queue_job.jobrunner.runner: initializing database connections 74 ...INFO...queue_job.jobrunner.runner: queue job runner ready for db <dbname> 75 ...INFO...queue_job.jobrunner.runner: database connections ready 76 Create jobs (eg using base_import_async) and observe they start immediately and in parallel. 77 Tip: to enable debug logging for the queue job, use --log-handler=odoo.addons.queue_job:DEBUG 78 [1] It works with the threaded Odoo server too, although this way of running Odoo is obviously not for production purposes. 79 Usage 80 To use this module, you need to: 81 82 Go to Job Queue menu 83 Developers 84 Bypass jobs on running Odoo 85 86 When you are developing (ie: connector modules) you might want to bypass the queue job and run your code immediately. 87 88 To do so you can set TEST_QUEUE_JOB_NO_DELAY=1 in your enviroment. 89 90 Bypass jobs in tests 91 92 When writing tests on job-related methods is always tricky to deal with delayed recordsets. To make your testing life easier you can set test_queue_job_no_delay=True in the context. 93 94 Tip: you can do this at test case level like this 95 96 @classmethod 97 def setUpClass(cls): 98 super().setUpClass() 99 cls.env = cls.env(context=dict( 100 cls.env.context, 101 test_queue_job_no_delay=True, # no jobs thanks 102 )) 103 Then all your tests execute the job methods synchronously without delaying any jobs.
先配置odoo配置文件:
在odoo.conf中增長以下:
4. 使用
5. 查看結果