Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(task): support setting user and group for new processes
Browse files Browse the repository at this point in the history
Water-Melon committed Nov 3, 2023
1 parent 238942d commit 5b523cf
Showing 6 changed files with 36 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM melonc/melon
WORKDIR /root
WORKDIR /opt
RUN apt-get update && \
apt-get -y install git && \
git clone https://github.com/MelonCTech/Meproc.git
CMD /usr/bin/melang /root/Meproc/meproc.m
CMD /usr/bin/melang /opt/Meproc/meproc.m
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ The default IP is `127.0.0.1` and port is `8606`.
Here is a simple example.

```bash
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": "sleep1", "cmd": "sleep 5", "type": "once", "replica": 2, "user": "guest"}'
```

Using a PUT HTTP request to start up a new process.
@@ -51,6 +51,7 @@ Using a PUT HTTP request to start up a new process.
- `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.



@@ -187,4 +188,4 @@ We will see the output of Meproc like:

[BSD-3-Clause License](https://github.com/Water-Melon/Melang/blob/master/LICENSE)

Copyright (c) 2023-present, [MelonCTech](https://github.com/MelonCTech)
Copyright (c) 2023-present, [MelonCTech](https://github.com/MelonCTech)
File renamed without changes.
8 changes: 5 additions & 3 deletions log.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if !M_LOG
#define M_LOG
#include "@/conf.m"
#include "@/conf/conf.m"

Sys = Import('sys');
F = Import('file');
@@ -48,10 +48,12 @@
}

f = $F;
if (f.open(Log_path, 'a+') != false) {
if (f.open(Log_path, 'a+')) {
f.write(l + s + "\n");
f.close();
} fi
} else {
Sys.print("Open log file [" + Log_path + "] failed, " + f.errmsg());
}
Sys.print(lc + s);
}

17 changes: 15 additions & 2 deletions proc.m
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@
['field': 'cmd', 'type': 'string', 'required': true],
['field': 'type', 'type': 'string', 'required': true, 'in': ['once', 'daemon', 'cron']],
['field': 'cron', 'type': 'string', 'required': false,],
['field': 'user', 'type': 'string', 'required': false,],
['field': 'group', 'type': 'string', 'required': false,],
['field': 'replica', 'type': 'int', 'required': true, 'default': 0],
['field': 'interval', 'type': 'int', 'required': false, 'default': 3],
['field': 'deps', 'type': 'array', 'required': false, 'element_type': 'string', 'default': []],
@@ -154,7 +156,7 @@
}
}

@Start(prog) {
@Start(&prog) {
/*
[
{
@@ -179,7 +181,18 @@
prog['running'] = n;
prog['last_time'] = now;
prog['run_flag'] = false;
Log('info', 'Task ' + prog['name'] + ' started');

msg = 'Task ' + prog['name'];
if (prog['user'] || prog['group']) {
msg += " (as ";
prog['user'] && (msg += prog['user']);
msg += ':';
prog['group'] && (msg += prog['group']);
msg += ")";
} fi
msg += " started";
Log('info', msg);

for (i = 0; i < n; ++i) {
alias = name + ':' + i;
Eval('@/task.m', J.encode([
13 changes: 11 additions & 2 deletions task.m
Original file line number Diff line number Diff line change
@@ -13,8 +13,17 @@

again:

s.exec(cmd, 0, pid);
Log('info', "Process " + pid + " (" + alias + ") exit");
s.exec(cmd, -1, pid, conf['user'], conf['group']);

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);

if (type == 'daemon') {
s.msleep(interval);

0 comments on commit 5b523cf

Please sign in to comment.