diff --git a/README.md b/README.md index ba1ae61..5a8d998 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Features: - Support setting execution users and user groups for processes - Support multiple platforms: Windows, Linux, MacOS, etc. - Support for collecting output content from task processes. +- Support for plugin development based on process start and stop events. - Provide a Web Page for Task Management. - The project only needs to pre-install the Melang interpreter, and no more others need to be installed. diff --git a/controllers/proc.m b/controllers/proc.m index 6ad11c2..c86f536 100644 --- a/controllers/proc.m +++ b/controllers/proc.m @@ -20,7 +20,7 @@ ['field': 'user', 'type': 'string', 'required': false,], ['field': 'group', 'type': 'string', 'required': false,], ['field': 'replica', 'type': 'int', 'required': false, 'default': 1], - ['field': 'interval', 'type': 'int', 'required': false, 'default': 3], + ['field': 'interval', 'type': 'int', 'required': false, 'default': 3000], ['field': 'deps', 'type': 'array', 'required': false, 'element_type': 'string', 'default': []], ], 'args': [ diff --git a/coroutines/http.m b/coroutines/http.m index 050813a..29bb3cf 100644 --- a/coroutines/http.m +++ b/coroutines/http.m @@ -14,15 +14,6 @@ conf = d['conf']; name = conf['name']; if (Tasks[name]['type'] == 'daemon') { - msg = "Daemon Task " + name + " (" + d['alias']; - if (conf['user'] || conf['group']) { - msg += " running as "; - conf['user'] && (msg += conf['user']); - msg += ':'; - conf['group'] && (msg += conf['group']); - } fi - msg += ") start"; - Log('info', msg); Eval('@/../coroutines/task.m', data, false, d['alias']); } else { Tasks[name] && --(Tasks[name]['running']); diff --git a/coroutines/task.m b/coroutines/task.m index 4d012bf..da68e70 100644 --- a/coroutines/task.m +++ b/coroutines/task.m @@ -1,4 +1,4 @@ -#include "@/../utils/log.m" +#include "@/../utils/event.m" j = Import('json'); s = Import('sys'); @@ -15,17 +15,10 @@ s.msleep(interval); } fi -s.exec(cmd, -1, pid, conf['user'], conf['group'], alias); +process_start_event = data; -msg = "Process " + pid + " (" + alias; -if (conf['user'] || conf['group']) { - msg += " running as "; - conf['user'] && (msg += conf['user']); - msg += ':'; - conf['group'] && (msg += conf['group']); -} fi -msg += ") exit"; -Log('info', msg); +s.exec(cmd, -1, data['pid'], conf['user'], conf['group'], alias); +process_stop_event = data; m.send('http', EVAL_DATA); diff --git a/events/example.m b/events/example.m new file mode 100644 index 0000000..975ef52 --- /dev/null +++ b/events/example.m @@ -0,0 +1,15 @@ +#if !M_EV_EXAMPLE +#define M_EV_EXAMPLE + +Sys = Import('sys'); + +Example { + @start(&proc) { + Sys.print(proc); + } + @stop(&proc) { + Sys.print(proc); + } +} + +#endif diff --git a/meproc.m b/meproc.m index 43f6a1e..a66c8bf 100644 --- a/meproc.m +++ b/meproc.m @@ -10,7 +10,7 @@ return; } fi -Log('info', "Meproc v1.0.5. Listen on: " + Conf['ip'] + ':' + Conf['port']); +Log('info', "Meproc v1.0.6. Listen on: " + Conf['ip'] + ':' + Conf['port']); Eval('@/coroutines/http.m'); Eval('@/coroutines/bootstrap.m', nil, false, 'bootstrap'); diff --git a/utils/event.m b/utils/event.m new file mode 100644 index 0000000..e4b482c --- /dev/null +++ b/utils/event.m @@ -0,0 +1,72 @@ +#if !M_EVENT +#define M_EVENT + +#include "@/log.m" +#include "@/../events" + +Sys = Import('sys'); +Str = Import('str'); + +process_start_event = nil; +process_stop_event = nil; + +@start_event_handler(&proc) { + conf = proc['conf']; + alias = proc['alias']; + type = conf['type']; + + msg = Str.capitalize(type) + " Process (" + alias; + if (conf['user'] || conf['group']) { + msg += " running as "; + conf['user'] && (msg += conf['user']); + msg += ':'; + conf['group'] && (msg += conf['group']); + } fi + msg += ") start"; + Log('info', msg); + + name = Str.capitalize(proc['conf']['name']); + o = $name; + if (!o || !Sys.has(o, 'start')) + return; + fi + o.start(proc); + Sys.print(name); +} + +@stop_event_handler(&proc) { + conf = proc['conf']; + alias = proc['alias']; + pid = proc['pid']; + type = conf['type']; + + msg = Str.capitalize(type) + " Process " + pid + " (" + alias; + if (conf['user'] || conf['group']) { + msg += " running as "; + conf['user'] && (msg += conf['user']); + msg += ':'; + conf['group'] && (msg += conf['group']); + } fi + msg += ") stop"; + Log('info', msg); + + name = Str.capitalize(conf['name']); + o = $name; + if (!o || !Sys.has(o, 'stop')) + return; + fi + o.stop(proc); + Sys.print(name); +} + +Watch(process_start_event, start_event_handler); +Watch(process_stop_event, stop_event_handler); + +#endif +/* + * while (1) { + * process_start_event = ["conf": ['name':"example"]]; + * process_stop_event = ["conf": ['name':"example"]]; + * } + */ + diff --git a/web/index.html b/web/index.html index d72e3aa..3e77aff 100644 --- a/web/index.html +++ b/web/index.html @@ -33,7 +33,7 @@