zoukankan      html  css  js  c++  java
  • odoo开发笔记 -- 借助模块queue_job实现异步方法调用

    场景描述:

    对比了几个定时调度的框架,发现各有优缺点;

    celery 很强,异步定时调度,异步周期调度,也有延时调度的功能,但是延时调度的案例比较少,遂暂时不使用。

    queue_job,一个odoo第三方应用模块,同样功能强大,可以满足日常的异步方法执行;

    模块github地址:https://github.com/OCA/queue/tree/10.0/queue_job

    但是我们的场景稍微有些不同,就是需要在异步调用的基础上增加一个延时(例5秒);

    一般的异步调度机制:异步执行避免了任务的阻塞,相当于将需要下一步执行的任务(函数or方法),添加到了一个待执行的队列中,然后交给后台程序去慢慢处理,

    处理的速度及性能,取决于服务器的硬件配置以及给相应任务开辟的进程数量。

    回到我们的需求,通过分析queue_job模块的代码,大概找到我们需要定制修改的位置,即:在job开始执行前增加一个延时,下图为未修改之前:

    修改后代码:

    注意:这样虽然可以满足需求,但是会有新的问题,造成系统走到这里需要等待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. 查看结果

  • 相关阅读:
    大型网站前端使用图片格式的正确姿势
    移动端开发技术文档
    超详细的Web前端开发规范文档
    try 、catch 、finally 、throw 测试js错误
    ajax大并发问题
    jQuery之Ajax--全局Ajax事件处理器
    如何处理ajax中嵌套一个ajax
    关于for循环里面异步操作的问题
    XMLHttpRequest: 网络错误 0x2f78,…00002f78
    【转载】OGRE中用到的设计模式
  • 原文地址:https://www.cnblogs.com/hellojesson/p/11541057.html
Copyright © 2011-2022 走看看