找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 217|回复: 0

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

[复制链接]

1538

主题

0

回帖

4652

积分

管理员

积分
4652
发表于 2023-12-9 16:29:55 | 显示全部楼层 |阅读模式
最近使用 Melang 语言写了一个 supervisor 相似服务Meproc来管理进程。
Meproc 有如下特性:

使用 HTTP API 管理控制 Meproc 来管理进程
跨平台,支持 UNIX/Linux 、Mac 、Windows 等平台
支持 cron 类定时调度任务
支持简单的任务间依赖关系
无需安装,有 Melang 解释器即可,也有现成的 docker image 直接可用

使用示范:
首先启动 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 仓库了解更多内容。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|陪我练

GMT+8, 2024-10-31 09:43 , Processed in 0.598594 second(s), 24 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表