Skip to main content

Python port of PowerJob OpenAPI client 5.1.2

Project description

PowerJob Client Python

这是 tech.powerjob:powerjob-client:5.1.2 的 Python 同步客户端移植版,用于调用 PowerJob Server 的 /openApi 接口。它移植的是 OpenAPI 管理客户端,不是接收和执行任务的 Worker SDK。

已覆盖 Java 客户端的 Job、Instance、Workflow、Workflow Instance API,并实现:

  • AppName + 密码 MD5 鉴权
  • access token 自动刷新
  • 多 Server 地址故障切换
  • HTTP/HTTPS、自定义请求头和超时
  • snake_case 方法及 Java camelCase 兼容别名
  • 请求 dataclass、枚举和结构化 Result

安装

python -m pip install .

开发和测试:

python -m pip install -e ".[test]"
pytest

快速开始

from powerjob_client import PowerJobClient

with PowerJobClient("127.0.0.1:7700", "my-app", "powerjob-password") as client:
    jobs = client.fetch_all_job()
    if jobs.success:
        print(jobs.data)

    instance_id = client.run_job(10086, instance_params='{"source":"python"}').unwrap()
    print(instance_id)

集群和 HTTPS 配置:

from powerjob_client import ClientConfig, PowerJobClient

config = ClientConfig(
    app_name="my-app",
    password="secret",
    address_list=["powerjob-1:7700", "powerjob-2:7700"],
    protocol="https",
    connection_timeout=3,
    read_timeout=10,
    default_headers={"X-Tenant": "demo"},
    verify_ssl=True,
    trust_env=False,
)
client = PowerJobClient(config)

为忠实兼容 Java 5.1.2 客户端,HTTPS 的 verify_ssl 默认是 False。生产环境建议显式设为 True 并使用有效证书。

客户端默认不读取 HTTP_PROXYHTTPS_PROXYALL_PROXY 等环境配置,避免访问内网 PowerJob Server 时意外走本机代理。如确实需要继承环境代理,请配置 trust_env=True

新建或更新 Job

from powerjob_client import (
    ExecuteType,
    PowerJobClient,
    ProcessorType,
    SaveJobInfoRequest,
    TimeExpressionType,
)

request = SaveJobInfoRequest(
    job_name="python-created-job",
    job_description="created through PowerJob OpenAPI",
    processor_info="com.example.DemoProcessor",
    execute_type=ExecuteType.STANDALONE,
    processor_type=ProcessorType.BUILT_IN,
    time_expression_type=TimeExpressionType.CRON,
    time_expression="0 0 * * * ?",
)

with PowerJobClient("127.0.0.1:7700", "my-app", "secret") as client:
    job_id = client.save_job(request).unwrap()

也可直接传字典。字典允许 Python snake_case 键,发送时会转换成 PowerJob 的 camelCase

client.query_job({"job_name_like": "demo", "status_in": [1]})

API 对照

Java Python
exportJob / saveJob / copyJob export_job / save_job / copy_job
fetchJob / fetchAllJob / queryJob fetch_job / fetch_all_job / query_job
disableJob / enableJob / deleteJob disable_job / enable_job / delete_job
runJob run_job
stopInstance / cancelInstance / retryInstance stop_instance / cancel_instance / retry_instance
fetchInstanceStatus / fetchInstanceInfo fetch_instance_status / fetch_instance_info
queryInstanceInfo query_instance_info
saveWorkflow / copyWorkflow save_workflow / copy_workflow
saveWorkflowNode / fetchWorkflow save_workflow_node / fetch_workflow
disableWorkflow / enableWorkflow / deleteWorkflow disable_workflow / enable_workflow / delete_workflow
runWorkflow run_workflow
stopWorkflowInstance / retryWorkflowInstance stop_workflow_instance / retry_workflow_instance
markWorkflowNodeAsSuccess mark_workflow_node_as_success
fetchWorkflowInstanceInfo fetch_workflow_instance_info

所有 Java 风格方法名也可直接调用。响应统一为 Result(success, data, message, code)data 保留 Server 原始 JSON 对象,避免版本升级时因新增字段反序列化失败。

与 Java 版的有意差异

  • Python 不区分 Java 的 ResultDTOPowerResultDTO,统一为 Result,并保留可选 code
  • Python 字典响应保持原始形态,不强制转换为大量 DTO 类。
  • Java 的 writeTimeoutrequests 中没有完全对应的独立设置;连接与读取超时分别由 connection_timeoutread_timeout 控制。
  • 动态地址发现使用无参 address_provider: Callable[[], Sequence[str]]

版本范围

实现以 PowerJob 官方仓库标签 v5.1.2 中的 powerjob-clientpowerjob-common 为依据。若 Server 版本较旧,runJob2、分页查询或鉴权响应头可能不可用。

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

powerjob_client_python-5.1.2.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

powerjob_client_python-5.1.2.0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file powerjob_client_python-5.1.2.0.tar.gz.

File metadata

  • Download URL: powerjob_client_python-5.1.2.0.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for powerjob_client_python-5.1.2.0.tar.gz
Algorithm Hash digest
SHA256 f58063944933ad0f5368478cfd6707e88e563ccbae8a577afbd1bb857489bbe4
MD5 a519d2b974aba0f98f4bfb35a02b1263
BLAKE2b-256 f441f2f3fa5e28bebd3418ed6dca4602553a4ab37cc2398b3692234e7767fd60

See more details on using hashes here.

File details

Details for the file powerjob_client_python-5.1.2.0-py3-none-any.whl.

File metadata

  • Download URL: powerjob_client_python-5.1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for powerjob_client_python-5.1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 585852f957bf006784c4de8a493ee034c9ea1e89445c3669050749dc919a376a
MD5 a978e0d409fc8b0724a046ef43ae67f7
BLAKE2b-256 afe5cd24a454e5240f289c9ca2af250d5561f06ffc35809debcba84f4d52ce37

See more details on using hashes here.

Supported by

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