最近使用 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 仓库了解更多内容。