Skip to content

Commit

Permalink
feat(*): support coroutine task
Browse files Browse the repository at this point in the history
It allows to initiate a coroutine to execute the given Melang script file.
  • Loading branch information
Water-Melon committed Dec 15, 2023
1 parent f167bed commit 2de94d6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ Using a POST HTTP request to start up a new process.

`name` is the task name. One task is a group of same processes. In this example, we will start up two process to run `sleep 5`.

`once` means that the processes in this task will only be executed once. And there are three values of `type` field:
`once` means that the processes in this task will only be executed once. And there are four values of `type` field:

- `once` means this task will only be executed once even if it exits unexpectedly.
- `daemon` means this task is a daemon, so if this process exits in any reason, it will be restarted.
- `cron` means this task is a cron job, it will contain a field named `cron` in task JSON.
- `user` indicates the user of the new process. Please make sure that Meproc has the permission to do this. `user` and `group` are NOT working on Windows.
- `coroutine` means that this task is a melang coroutine task. It will start a melang coroutine based on the provided script file path.


`user` indicates the user of the new process. Please make sure that Meproc has the permission to do this. `user` and `group` are NOT working on Windows.



Expand Down
21 changes: 19 additions & 2 deletions controllers/proc.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'body': [
['field': 'name', 'type': 'string', 'required': true],
['field': 'cmd', 'type': 'string', 'required': true],
['field': 'type', 'type': 'string', 'required': true, 'in': ['once', 'daemon', 'cron']],
['field': 'type', 'type': 'string', 'required': true, 'in': ['once', 'daemon', 'cron', 'coroutine']],
['field': 'cron', 'type': 'string', 'required': false, 'default': '* * * * *'],
['field': 'user', 'type': 'string', 'required': false,],
['field': 'group', 'type': 'string', 'required': false,],
Expand Down Expand Up @@ -112,8 +112,18 @@
fi
data[keys[i]] = Tasks[keys[i]];
}
co = Eval();
run = S.exec();
n = S.size(co);
for (i = 0; i < n; ++i) {
run[] = [
'command': '',
'pid': '',
'alias': co[i],
];
}
return J.encode(['code': 200, 'msg': 'OK', 'data': [
'running': S.exec(),
'running': run,
'tasks': data,
]]);
}
Expand All @@ -134,6 +144,10 @@
R['code'] = 400;
return J.encode(['code': 400, 'msg': 'Invalid JSON field']);
} fi
if (body['name'] == '__coroutine__') {
R['code'] = 400;
return J.encode(['code': 400, 'msg': "name '" + body['name'] + "' is reserved"]);
} fi
if (S.int(body['interval']) <= 0) {
R['code'] = 400;
return J.encode(['code': 400, 'msg': "Interval must be a positive integer'"]);
Expand Down Expand Up @@ -256,6 +270,9 @@
Log('info', 'Task ' + prog['name'] + ' stopped');
for (i = 0; i < n; ++i) {
Kill(name + ':' + i);
if (prog['type'] == 'coroutine') {
Kill('__' + prog['type'] + '__:' + name + ':' + i);
} fi
}
}

Expand Down
15 changes: 14 additions & 1 deletion coroutines/task.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,20 @@

process_start_event = data;

s.exec(cmd, -1, data['pid'], conf['user'], conf['group'], alias);
if (type != 'coroutine') {
s.exec(cmd, -1, data['pid'], conf['user'], conf['group'], alias);
} else {
name = '__' + type + '__:' + alias;
Eval(cmd, EVAL_DATA, false, name);

while (true) {
list = Eval();
if (!s.has(list, name))
break;
fi
s.msleep(1000);
}
}

process_stop_event = data;

Expand Down
4 changes: 3 additions & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ <h2 style="padding:8px 20px">Start a new task</h2>
<option value="once">once</option>
<option value="daemon">daemon</option>
<option value="cron">cron</option>
<option value="coroutine">coroutine</option>
</select><br><br>
<div id="interval-box">
<label for="tinterval">Interval:</label>
Expand Down Expand Up @@ -494,7 +495,8 @@ <h2 style="padding:8px 20px">Start a new task</h2>
}, 2000);
},
error: function(xhr, status, error) {
$('#tips').text(error)
var e = JSON.parse(xhr.responseText)
$('#tips').text(e.msg)
$('#tips').css('display', 'block')
$('#tips').css('color', 'red')
}
Expand Down

0 comments on commit 2de94d6

Please sign in to comment.