开源了一个使用自己写的脚本语言实现的进程管理和调度服务

2023-11-02 19:43:45 +08:00
 monkeyNik

最近使用 Melang 语言写了一个 supervisor 相似服务Meproc来管理进程。

Meproc 有如下特性:

使用示范:

首先启动 Meproc

melang meproc.m

默认监听127.0.0.1:8606

启动后我们可以启动一个仅执行一次的任务,拉起一组进程去 sleep 5 秒:

curl -XPUT http://127.1:8606/proc -d '{"name": "sleep1", "cmd": "sleep 5", "type": "once", "replica": 2}'

然后再启动一个一次性任务 sleep 5 秒,但这个任务是依赖于上一个任务(sleep1)的。

curl -v -XPUT http://127.1:8606/proc -d '{"name": "sleep2", "cmd": "sleep 5", "type": "once", "replica": 2, "deps": ["sleep1"]}'

我们也可以创建一个 cron 类型的定时任务,每分钟拉起一组进程,让他们 sleep 5 秒:

curl -v -XPUT http://127.1:8606/proc -d '{"name": "sleep2", "cmd": "sleep 5", "type": "cron", "cron": "* * * * *", "replica": 2}'

也可以查询当前运行中的进程有哪些,以及使用 API 下发的任务有哪些:

curl -v -XGET http://127.1:8606/proc

当然也支持停止任务:

curl -v -XDELETE http://127.1:8606/proc?name=sleep1

或者重启某个任务,例如:

curl -v -XPOST http://127.1:8606/proc?name=sleep2

一起看一个完整的示例,我们可以在启动 Meproc 后,执行如下命令:

curl -v -XPUT http://127.1:8606/proc -d '{"name": "sleep1", "cmd": "sleep 5", "type": "once", "replica": 2}'

curl -v -XPUT http://127.1:8606/proc -d '{"name": "sleep2", "cmd": "sleep 5", "type": "once", "replica": 2, "deps": ["sleep1"]}'

curl -v -XPUT http://127.1:8606/proc -d '{"name": "sleep3", "cmd": "sleep 5", "type": "once", "replica": 2, "deps": ["sleep1", "sleep2"]}'

curl -v -XDELETE http://127.1:8606/proc?name=sleep1

curl -v -XPUT http://127.1:8606/proc -d '{"name": "sleep1", "cmd": "sleep 5", "type": "once", "replica": 2}'

curl -v -XPOST http://127.1:8606/proc?name=sleep1

curl -v -XPUT http://127.1:8606/proc -d '{"name": "sleep4", "cmd": "sleep 5", "type": "cron", "cron": "* * * * *", "replica": 2}'

随后我们就可以在 Meproc 的终端上看到如下输出:

11/01/2023 10:28:28 UTC [INFO]: Listen: 127.0.0.1:8606
11/01/2023 10:28:31 UTC [INFO]: Task sleep1 started
11/01/2023 10:28:31 UTC [INFO]: Task sleep1 stopped
11/01/2023 10:28:31 UTC [INFO]: Task sleep2 started
11/01/2023 10:28:32 UTC [INFO]: Task sleep1 started
11/01/2023 10:28:32 UTC [INFO]: Task sleep1 stopped
11/01/2023 10:28:32 UTC [INFO]: Task sleep1 started
11/01/2023 10:28:37 UTC [INFO]: Process 1533616 (sleep2:1) exit
11/01/2023 10:28:37 UTC [INFO]: Process 1533615 (sleep2:0) exit
11/01/2023 10:28:37 UTC [INFO]: Process 1533626 (sleep1:1) exit
11/01/2023 10:28:37 UTC [INFO]: Process 1533624 (sleep1:0) exit
11/01/2023 10:28:37 UTC [INFO]: Task sleep3 started
11/01/2023 10:28:42 UTC [INFO]: Process 1533685 (sleep3:0) exit
11/01/2023 10:28:42 UTC [INFO]: Process 1533686 (sleep3:1) exit
11/01/2023 10:28:45 UTC [INFO]: Task sleep4 started
11/01/2023 10:28:50 UTC [INFO]: Process 1533747 (sleep4:0) exit
11/01/2023 10:28:50 UTC [INFO]: Process 1533748 (sleep4:1) exit
11/01/2023 10:30:00 UTC [INFO]: Task sleep4 started
11/01/2023 10:30:05 UTC [INFO]: Process 1534633 (sleep4:0) exit
11/01/2023 10:30:05 UTC [INFO]: Process 1534634 (sleep4:1) exit
11/01/2023 10:30:20 UTC [INFO]: Task sleep4 stopped
11/01/2023 10:30:20 UTC [INFO]: Task sleep4 started
11/01/2023 10:30:25 UTC [INFO]: Process 1534878 (sleep4:0) exit
11/01/2023 10:30:25 UTC [INFO]: Process 1534879 (sleep4:1) exit
11/01/2023 10:30:32 UTC [INFO]: Task sleep4 stopped
11/01/2023 10:30:32 UTC [INFO]: Task sleep4 started
11/01/2023 10:30:37 UTC [INFO]: Process 1534999 (sleep4:0) exit
11/01/2023 10:30:37 UTC [INFO]: Process 1535000 (sleep4:1) exit
...

欢迎感兴趣的小伙伴访问 Meproc 的Github 仓库了解更多内容。

1463 次点击
所在节点    分享创造
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/987996

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX