Skip to main content

A Python executor for XXL-jobs

Project description

xxl-jobs 的python客户端实现

Package version Supported Python versions Coverage

使用pyxxl可以方便的把Python写的方法注册到XXL-JOB中,使用XXL-JOB-ADMIN管理Python定时任务和周期任务

实现原理:通过XXL-JOB提供的RESTful API接口进行对接

注意!!!如果用同步的方法,请查看下面同步任务注意事项。

已经支持的功能

  • 执行器注册到job-admin
  • task注册,类似于flask路由装饰器的用法
  • 任务的管理(支持在界面上取消,发起等操作,任务完成后会回调admin)
  • 所有阻塞策略的支持
  • 异步支持(推荐)
  • job-admin上查看日志

适配的XXL-JOB版本

  • XXL-JOB:2.3.0

如遇到不兼容的情况请issue告诉我XXL-JOB版本我会尽量适配

如何使用

pip install pyxxl
# 如果日志需要写入redis
pip install pyxxl[redis]
# 如果需要从.env加载配置
pip install pyxxl[dotenv]
# 安装所有功能
pip install pyxxl[all]
import asyncio

from pyxxl import ExecutorConfig, PyxxlRunner

config = ExecutorConfig(
    xxl_admin_baseurl="http://localhost:8080/xxl-job-admin/api/",
    executor_app_name="xxl-job-executor-sample",
    executor_host="172.17.0.1",
)

app = PyxxlRunner(config)

@app.handler.register(name="demoJobHandler")
async def test_task():
    await asyncio.sleep(5)
    return "成功..."

# 如果你代码里面没有实现全异步,请使用同步函数,不然会阻塞其他任务
@app.handler.register(name="xxxxx")
def test_task3():
    return "成功3"


app.run_executor()

更多示例和接口文档请参考 PYXXL文档 ,具体代码在example文件夹下面

监控指标

pip install pyxxl[metrics]

安装metrics扩展后,执行器会自动加载prometheus的指标监控功能

访问地址为: http://executor_host:port/metrics

同步任务注意事项

同步任务会放到线程池中运行,无法正确接受cancel信号和timeout配置

如果需要使用同步任务并且无法控制(预料)里面执行时间,又需要进行超时和cancel功能的,需要接受pyxxl发出的cancel_event,当该cancel_event被设置时需要中断任务,下面是一个案例:

...

@app.handler.register(name="sync_func")
def sync_loop_demo():
    # 如果同步任务里面有循环,为了支持cancel操作,必须每次都判断g.cancel_event.
    task_params_list = []
    while not g.cancel_event.is_set() and task_parasm_list:
        params = task_params_list.pop()
        time.sleep(3)
    return "ok"

# 如下代码会造成线程池里的线程被永远占用,timeout cancel全部不生效
@app.handler.register(name="sync_func2")
def sync_loop_demo2():
    while True:
        time.sleep(3) # 模拟你运行的任务
    return "ok"

开发人员

下面是开发人员如何快捷的搭建开发调试环境

启动xxl的调度中心

./init_dev_env.sh

启动执行器

# if you need. set venv in project.
# poetry config virtualenvs.in-project true
poetry install --all-extras
# 修改app.py中相关的配置信息,然后启动
poetry run python example/app.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyxxl-0.3.0.tar.gz (36.5 kB view details)

Uploaded Source

Built Distribution

pyxxl-0.3.0-py3-none-any.whl (44.0 kB view details)

Uploaded Python 3

File details

Details for the file pyxxl-0.3.0.tar.gz.

File metadata

  • Download URL: pyxxl-0.3.0.tar.gz
  • Upload date:
  • Size: 36.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pyxxl-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6f93967fb944e2a5ba163175763ef97691babd06562ba223f16d1a503f51143d
MD5 87ea23e688ad81b30fa73ecc991dbc63
BLAKE2b-256 38be034db427d47706ce8f30af547a323507e4926521cd3d005c41c2b866ff41

See more details on using hashes here.

Provenance

File details

Details for the file pyxxl-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pyxxl-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 44.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pyxxl-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e0a72f9486ce3eadb6631ed5db0d79117ee00fd4a8b8fa8fbb3d27aab0634ca
MD5 cc427ebadfe52dbd211b44dcf83e06c2
BLAKE2b-256 018f72cbda2d5125e01e0d5cf6b42aa41a7c5c6f2d23e2bf667b9143239a7724

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page