目标: 基于docker的布式任务调度器, 比quartzs
,xxl-job
更强大的分布式任务调度器。
可以将要执行的任务打包为docker镜像,或者选择已有镜像,自定义脚本程序,通过pipeline框架来实现调度。
开源地址: https://github.com/jadepeng/docker-pipeline
一个pipeline 任务:
@Data public class Pipeline { @Id private String id; private String name; @JSONField(name = "pipeline") private List<PipelineTask> pipelineTasks = new ArrayList<>(); private List<Network> networks = Lists.newArrayList(new Network()); private List<Volume> volumes = Lists.newArrayList(new Volume()); private String startNode; /** * 调度类型: * 1) CRON, 设置cronExpression * 2) FIX_RATE, 设置fixRateInSeconds */ private ScheduleType scheduleType = ScheduleType.NONE; /** * CRON表达式,在scheduleType=CRON 时生效 */ private String cronExpression; /** * 固定周期运行,比如每隔多少s,在scheduleType=FIX_RATE 时生效 */ private int fixRateInSeconds; /** * 是否需要调度,为true时,才调度 */ @Indexed private boolean enableTrigger; private long lastTriggerTime; @Indexed private long nextTriggerTime; /** * 执行超时时间 */ private int executorTimeout; /** * 重试次数 */ private int executorFailRetryCount; /** * 内存限制 */ private String memory; /** * CPU 限制 */ private String cpu; @Data @Builder public static class PipelineTask { /** * 名称 */ String name; /** * 别名 */ String alias; /** * 依赖的pipelines,必须依赖的执行完成才能运行该PipelineTask */ List<String> dependencies; /** * 任务步骤,顺序执行 */ List<Step> steps; } @Data public static class Network { String name = "pipeline_default"; String driver = "bridge"; } @Data public static class Volume { String name = "pipeline_default"; String driver = "local"; } @Data public static class StepNetwork { private String name; private List<String> aliases = Lists.newArrayList("default"); public StepNetwork(String name) { this.name = name; } } }
举例:
{ "_id" : "29103d5e4a77409b9f6050eea8110bb3", "name" : "docker image pipeline", "pipelineTasks" : [ { "name" : "docker image pipeline", "steps" : [ { "name" : "defaultJob", "image" : "java-pipeline:1.0.1", "workingDir" : "/workspace", "environment" : {}, "networks" : [ { "name" : "pipeline_default", "aliases" : [ "default" ] } ], "onSuccess" : false, "authConfig" : {} } ] } ], "networks" : [ { "name" : "pipeline_default", "driver" : "bridge" } ], "volumes" : [ { "name" : "pipeline_default", "driver" : "local" } ], "cronExpression" : "0 0 * * * ?", "fixRateInSeconds" : 0, "scheduleType" : "CRON", "enableTrigger" : true, "lastTriggerTime" : 1627744509047, "nextTriggerTime" : 1627747200000, "executorTimeout" : 0, "executorFailRetryCount" : 0, "isAvailable" : 1, "runningPipelines" : [], "finishedPipeliens" : [], "created_by" : "admin", "created_date" : "2021-07-20T04:33:16.477Z", "last_modified_by" : "system", "last_modified_date" : "2021-07-31T15:15:09.048Z" }
使用mvn编译
mvn package -DskipTests
根据需要,修改master的prod配置文件application-prod.yml
。
包含kafka
配置,server
端口,mongodb
地址,jwt secret
配置。
mongodb 会自动新建collection和初始化数据,无需手动导入数据。
kafka: producer: bootstrap-servers: 127.0.0.1:9092 retries: 3 batch-size: 2000 buffer-memory: 33554432 consumer: group-id: consumer-pipeline auto-offset-reset: earliest enable-auto-commit: true bootstrap-servers: 172.31.161.38:9092 server: port: 8080 spring: data: mongodb: uri: mongodb://127.0.0.1:28017 database: pipeline jhipster: security: authentication: jwt: base64-secret:
注意master的jwt secret需要和agent的保持一致。
配置好后,启动:
nohup java -jar pipeline-master-$version.jar --spring.profiles.active=prod &
可以将application-prod.yml
放在和jar包同一目录。
根据需要,修改master的prod配置文件application-prod.yml
。
包含:
eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://admin:${jhipster.registry.password}@127.0.0.1:8080/eureka/ server: port: 8081 application: docker-server: docker-tls-verify: true docker-cert-path: /mnt/parastor/pipeline/ca/ pipeline-log-path: /mnt/parastor/pipeline/logs/ jhipster: security: authentication: jwt: base64-secret:
POST /api/pipelines/exec-old Body:
{ "networks":[ { "driver":"bridge", "name":"pipeline_network_3eac4b36209a41e58a5f22dd403fee50" } ], "pipeline":[ { "alias":"Word", "dependencies":[], "name":"pipeline_task_3eac4b36209a41e58a5f22dd403fee50_1", "nextPipelines":[], "steps":[ { "alias":"Word", "auth_config":{}, "command":[ "echo $CI_SCRIPT | base64 -d | /bin/bash -e" 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U4NGJiMDg4MzIwNWRiM2YyZGFlNzkvcGRmVG9Xb3JkL2VjNWMwYjk0M2QwYjRmNDI5MzcyMmE1ZGRjNjFlNjZkL2VudGl0eUluZm8udHh0Cg==" }, "image":"registry.iflyresearch.com/aimind/java:v1.0.0", "name":"pipeline_task_3eac4b36209a41e58a5f22dd403fee50_1", "networks":[ { "aliases":[ "default" ], "name":"pipeline_network_3eac4b36209a41e58a5f22dd403fee50" } ], "on_success":true, "volumes":[ "pipeline_default:/aimind", "/mnt/parastor/aimind/shared/:/share", "/mnt/parastor/aimind/pipeline-jobs/2021/07/26/3eac4b36209a41e58a5f22dd403fee50:/workspace", "/mnt/parastor/aimind/datavolumes/carmaster:/datavolume" ], "working_dir":"/workspace" } ] } ], "volumes":[ { "driver":"local", "name":"pipeline_default" } ] }
成功返回:
{ "retcode": "000000", "desc": "成功", "data": { "id": "8137f344-f52d-4595-bdbb-425363847b61", } }
可根据id获取日志。
GET /api/pipelines/jobLog/{jobid}/
结果:
{ "retcode": "000000", "desc": "成功", "data": { "currentTask": null, "logs": [ { "id": "e76a686f68b64c0783b7721b058be137", "jobId": "8137f344-f52d-4595-bdbb-425363847b61", "status": "FINISHED", "taskName": "pipeline_task_3eac4b36209a41e58a5f22dd403fee50_1", "exitedValue": 0, "logs": [ "proc \"pipeline_task_3eac4b36209a41e58a5f22dd403fee50_1\" started", "pipeline_task_3eac4b36209a41e58a5f22dd403fee50_1:+ java -cp /datavolume/pdf_to_word/pdfbox_util-1.0-SNAPSHOT.jar com.iflytek.indexer.Runner --inputPDF /datavolume/extract/f92c2a75bae84bb0883205db3f2dae79/pdf/ec5c0b943d0b4f4293722a5ddc61e66d/HS7.pdf --outputWord /datavolume/extract/f92c2a75bae84bb0883205db3f2dae79/pdfToWord/ec5c0b943d0b4f4293722a5ddc61e66d/ --schemaInputPath /datavolume/extract/kbqa/f92c2a75bae84bb0883205db3f2dae79 --entityIndexPath /datavolume/extract/f92c2a75bae84bb0883205db3f2dae79/pdfToWord/entity --fieldIndexPath /datavolume/extract/f92c2a75bae84bb0883205db3f2dae79/pdfToWord/field --type lucene --output /datavolume/extract/f92c2a75bae84bb0883205db3f2dae79/pdfToWord/ec5c0b943d0b4f4293722a5ddc61e66d/entityInfo.txt", "proc \"pipeline_task_3eac4b36209a41e58a5f22dd403fee50_1\" exited with status 0" ] } ], "exitedValue": 0, "status": "FINISHED", "pipelineJobSt": 1627477250599, "pipelineJobFt": 1627477274299 } }
如果pipelien需要周期执行,需要配置enableTrigger
为true,同时设置按照CRON
或者’
FIX_RATE` 运行:
fixRateInSeconds
配置周期运行时间示例:每360
秒运行一次:
{ // pipeline ... "pipelineTasks" : [ ], "fixRateInSeconds" : 360, "scheduleType" : "FIX_RATE", "enableTrigger" : true }
cronExpression
配置.示例:每小时开始的时候运行一次:
{ // pipeline ... "pipelineTasks" : [ ], "cronExpression" : "0 0 * * * ?", "scheduleType" : "CRON", "enableTrigger" : true }