Skip to content

Commit

Permalink
feat(event): support for plugin development based on process start an…
Browse files Browse the repository at this point in the history
…d stop events
  • Loading branch information
Water-Melon committed Nov 15, 2023
1 parent 446dcd6 commit aecf6fb
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion controllers/proc.m
Original file line number Diff line number Diff line change
Expand Up @@ -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': [
Expand Down
9 changes: 0 additions & 9 deletions coroutines/http.m
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
15 changes: 4 additions & 11 deletions coroutines/task.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "@/../utils/log.m"
#include "@/../utils/event.m"

j = Import('json');
s = Import('sys');
Expand All @@ -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);
15 changes: 15 additions & 0 deletions events/example.m
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion meproc.m
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
72 changes: 72 additions & 0 deletions utils/event.m
Original file line number Diff line number Diff line change
@@ -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"]];
* }
*/

2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h2 style="padding:8px 20px">Start a new task</h2>
</select><br><br>
<div id="interval-box">
<label for="tinterval">Interval:</label>
<input type="text" id="tinterval" name="tinterval" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" placeholder="Optional, default is 3 seconds"/><br><br>
<input type="text" id="tinterval" name="tinterval" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" placeholder="Optional, default is 3000 milliseconds"/> (ms)<br><br>
</div>
<div id="cron-box">
<label for="tcron">Cron:</label>
Expand Down

0 comments on commit aecf6fb

Please sign in to comment.