Skip to content

Commit

Permalink
refactor(*): refactor daemon process restart logic, fix bug in string…
Browse files Browse the repository at this point in the history
… validation
  • Loading branch information
Water-Melon committed Nov 15, 2023
1 parent 51d3758 commit f387d66
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<img src="https://raw.githubusercontent.com/MelonCTech/Meproc/master/docs/logo.png" style="width:500px;" />
<img src="https://raw.githubusercontent.com/MelonCTech/Meproc/master/docs/logo.png" />


<br>
Expand All @@ -17,6 +17,10 @@ Features:
- The project only needs to pre-install the Melang interpreter, and no more others need to be installed.


## Web UI Screenshot

<img src="https://raw.githubusercontent.com/MelonCTech/Meproc/master/docs/webui.png" style="width:600px"/>


## Installation

Expand Down
4 changes: 4 additions & 0 deletions controllers/proc.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
R['code'] = 400;
return J.encode(['code': 400, 'msg': 'Invalid JSON field']);
} fi
if (S.int(body['interval']) <= 0) {
R['code'] = 400;
return J.encode(['code': 400, 'msg': "Interval must be a positive integer'"]);
} fi
if (S.int(body['replica']) <= 0) {
R['code'] = 400;
return J.encode(['code': 400, 'msg': "Replica must be a positive integer'"]);
Expand Down
21 changes: 20 additions & 1 deletion coroutines/http.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,29 @@
mq = Import('mq');
http = Import('http');
str = Import('str');
json = Import('json');

while (1) {
data = mq.recv('http', 1000);
!sys.is_nil(data) && Tasks[data] && --(Tasks[data]['running']);
if (!sys.is_nil(data)) {
d = json.decode(data);
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']);
}
} fi

cron_job_process();

Expand Down
10 changes: 4 additions & 6 deletions coroutines/task.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
cmd = conf['cmd'];
interval = conf['interval'];

again:
if (type == 'daemon') {
s.msleep(interval);
} fi

s.exec(cmd, -1, pid, conf['user'], conf['group'], alias);

Expand All @@ -25,9 +27,5 @@
msg += ") exit";
Log('info', msg);

if (type == 'daemon') {
s.msleep(interval);
goto again;
} fi

m.send('http', conf['name']);
m.send('http', EVAL_DATA);
Binary file added docs/webui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.4. Listen on: " + Conf['ip'] + ':' + Conf['port']);
Log('info', "Meproc v1.0.5. Listen on: " + Conf['ip'] + ':' + Conf['port']);

Eval('@/coroutines/http.m');
Eval('@/coroutines/bootstrap.m', nil, false, 'bootstrap');
Expand Down
2 changes: 1 addition & 1 deletion utils/validator.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
continue;
fi
} else {
if (!sys.has(data, r['field'])) {
if (!sys.has(data, r['field']) || ((sys.is_str(data[r['field']]) || sys.is_array(data[r['field']])) && !data[r['field']])) {
return false;
} fi
}
Expand Down
16 changes: 8 additions & 8 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,18 @@ <h2 style="padding:8px 20px">Start a new task</h2>
$('#submitbtn').on('click', function(ev) {
ev.preventDefault();
$('#tips').css('display', 'none')
var type = $('#ttype').val()
var type = $('#ttype').val().trim()
var data = {
name: $('#tname').val(),
cmd: $('#tcmd').val(),
name: $('#tname').val().trim(),
cmd: $('#tcmd').val().trim(),
type: type,
replica: parseInt($('#treplica').val()),
replica: parseInt($('#treplica').val().trim()),
}
if ($('#tuser').val()) data.user = $('#tuser').val()
if ($('#tgrp').val()) data.group = $('#tgrp').val()
if ($('#tuser').val()) data.user = $('#tuser').val().trim()
if ($('#tgrp').val()) data.group = $('#tgrp').val().trim()
if ($('#tdep').val()) data.deps = $('#tdep').val()
if (type == 'daemon' && $('#tinterval').val()) data.interval = $('#tinterval').val()
if (type == 'cron' && $('#tcron').val()) data.cron = $('#tcron').val()
if (type == 'daemon' && $('#tinterval').val().trim()) data.interval = $('#tinterval').val().trim()
if (type == 'cron' && $('#tcron').val().trim()) data.cron = $('#tcron').val().trim()
$.ajax({
url: "http://{{IP}}:{{PORT}}/proc",
type: "POST",
Expand Down

0 comments on commit f387d66

Please sign in to comment.