task API demo page, string stuff
This commit is contained in:
parent
cbf57a9a35
commit
08496afa11
Binary file not shown.
@ -5,4 +5,6 @@ first
|
||||
join
|
||||
nibble
|
||||
split
|
||||
str_to_lower
|
||||
str_to_upper
|
||||
trim
|
||||
|
||||
5
doc/areas/task.txt
Normal file
5
doc/areas/task.txt
Normal file
@ -0,0 +1,5 @@
|
||||
Task API
|
||||
|
||||
kill
|
||||
task
|
||||
task_pid
|
||||
15
doc/pages/kill.txt
Normal file
15
doc/pages/kill.txt
Normal file
@ -0,0 +1,15 @@
|
||||
:sig
|
||||
s64 kill(pid_t pid, s64 sig)
|
||||
|
||||
:params
|
||||
pid : PID of the process
|
||||
sig : signal number
|
||||
return value : 0 if signal was sent, -1 otherwise
|
||||
|
||||
:desc
|
||||
This is the standard POSIX kill() function, provided here for reference.
|
||||
|
||||
Possible signal numbers are: SIGABND, SIGABRT, SIGALRM, SIGBUS, SIGFPE, SIGHUP, SIGILL, SIGINT, SIGKILL, SIGPIPE, SIGPOLL, SIGPROF, SIGQUIT, SIGSEGV, SIGSYS, SIGTERM, SIGTRAP, SIGURG, SIGUSR1, SIGUSR2, SIGVTALRM, SIGXCPU, SIGXFSZ, SIGCHLD, SIGIO, SIGIOERR, SIGWINCH, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGCONT.
|
||||
|
||||
:see
|
||||
>task
|
||||
14
doc/pages/str_to_lower.txt
Normal file
14
doc/pages/str_to_lower.txt
Normal file
@ -0,0 +1,14 @@
|
||||
:sig
|
||||
String str_to_lower(String s)
|
||||
|
||||
:params
|
||||
s : the string to be converted
|
||||
return value : returns a version of 's' where all upper case characters have been changed into lower case
|
||||
|
||||
:desc
|
||||
Returns a lower case version of the input string 's'.
|
||||
|
||||
Note: this function is not yet Unicode-aware.
|
||||
|
||||
:see
|
||||
>string
|
||||
14
doc/pages/str_to_upper.txt
Normal file
14
doc/pages/str_to_upper.txt
Normal file
@ -0,0 +1,14 @@
|
||||
:sig
|
||||
String str_to_upper(String s)
|
||||
|
||||
:params
|
||||
s : the string to be converted
|
||||
return value : returns a version of 's' where all lower case characters have been changed into upper case
|
||||
|
||||
:desc
|
||||
Returns a upper case version of the input string 's'.
|
||||
|
||||
Note: this function is not yet Unicode-aware.
|
||||
|
||||
:see
|
||||
>string
|
||||
13
doc/pages/task.txt
Normal file
13
doc/pages/task.txt
Normal file
@ -0,0 +1,13 @@
|
||||
:sig
|
||||
pid_t task(String key, std::function<void()> exec_func)
|
||||
|
||||
:params
|
||||
key : string uniquely identifying the task
|
||||
exec_func : function to execute
|
||||
return value : the process ID of the started (or still running) task
|
||||
|
||||
:desc
|
||||
task() starts the 'exec_func' in a new process and returns that process' ID. If a process with the same 'key' is already running, task will not start a new process but instead just return the PID of the process that is already running.
|
||||
|
||||
:see
|
||||
>task
|
||||
12
doc/pages/task_pid.txt
Normal file
12
doc/pages/task_pid.txt
Normal file
@ -0,0 +1,12 @@
|
||||
:sig
|
||||
pid_t task_pid(String key)
|
||||
|
||||
:params
|
||||
key : string uniquely identifying the task
|
||||
return value : the process ID of the task
|
||||
|
||||
:desc
|
||||
Checks whether a process with the given 'key' is running and returns its PID if it is. Returns 0 otherwise.
|
||||
|
||||
:see
|
||||
>task
|
||||
@ -40,6 +40,30 @@ u8 hex_to_u8(String src)
|
||||
return(char_to_u8(src[0])*16 + char_to_u8(src[1]));
|
||||
}
|
||||
|
||||
String str_to_lower(String s)
|
||||
{
|
||||
String result = "";
|
||||
for(auto c : s)
|
||||
{
|
||||
if(c >= 'A' && c <= 'Z')
|
||||
c = tolower(c);
|
||||
result.append(1, c);
|
||||
}
|
||||
return(result);
|
||||
}
|
||||
|
||||
String str_to_upper(String s)
|
||||
{
|
||||
String result = "";
|
||||
for(auto c : s)
|
||||
{
|
||||
if(c >= 'A' && c <= 'Z')
|
||||
c = toupper(c);
|
||||
result.append(1, c);
|
||||
}
|
||||
return(result);
|
||||
}
|
||||
|
||||
String trim(String raw)
|
||||
{
|
||||
u32 len = raw.length();
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
u8 char_to_u8(char input);
|
||||
u8 hex_to_u8(String src);
|
||||
u64 int_val(String s, u32 base = 10);
|
||||
String str_to_lower(String s);
|
||||
String str_to_upper(String s);
|
||||
|
||||
String trim(String raw);
|
||||
StringList split(String str, String delim = "\n");
|
||||
|
||||
@ -423,6 +423,21 @@ void spawn_subprocess(std::function<void()> exec_after_spawn)
|
||||
}
|
||||
}
|
||||
|
||||
pid_t task_pid(String key)
|
||||
{
|
||||
String status_file_name = context->server->config.BIN_DIRECTORY + "/task-" + key;
|
||||
String status_file = file_get_contents(status_file_name);
|
||||
pid_t p = 0;
|
||||
if(status_file != "")
|
||||
{
|
||||
p = int_val(status_file);
|
||||
if(kill(p, 0) == 0) // process is still running
|
||||
return(p);
|
||||
unlink(status_file_name);
|
||||
}
|
||||
return(p);
|
||||
}
|
||||
|
||||
pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout)
|
||||
{
|
||||
String status_file_name = context->server->config.BIN_DIRECTORY + "/task-" + key;
|
||||
|
||||
@ -39,4 +39,6 @@ pid_t parent_pid = 0;
|
||||
pid_t my_pid = 0;
|
||||
|
||||
void on_segfault(int sig);
|
||||
|
||||
pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout = 60*10);
|
||||
pid_t task_pid(String key);
|
||||
|
||||
@ -21,6 +21,10 @@ String uri_decode(String q)
|
||||
result.append(1, hex_to_u8(q.substr(i+1, 2)));
|
||||
i += 2;
|
||||
}
|
||||
else if(c == '+')
|
||||
{
|
||||
result.append(1, ' ');
|
||||
}
|
||||
else
|
||||
{
|
||||
result.append(1, c);
|
||||
|
||||
9
test/task-status.uce
Normal file
9
test/task-status.uce
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
RENDER()
|
||||
{
|
||||
String task_name = first(context->get["task-name"], "example-task");
|
||||
|
||||
print("Task Name: ", task_name, "\n");
|
||||
print("Task ID: ", task_pid(task_name), "\n");
|
||||
print("Task Running: ", task_pid(task_name) == 0 ? "no" : "yes", "\n");
|
||||
}
|
||||
@ -1,12 +1,10 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
RENDER()
|
||||
{
|
||||
DTree t;
|
||||
|
||||
String task_name = first(context->get["task-name"], "example-task");
|
||||
|
||||
<>
|
||||
<link rel="stylesheet" href='style.css'></link>
|
||||
<h1>
|
||||
@ -14,20 +12,69 @@ RENDER()
|
||||
Tasks
|
||||
</h1>
|
||||
|
||||
<pre style="white-space: pre-wrap"><?
|
||||
<script>
|
||||
|
||||
print("Task ID: ", task("example-task", []() {
|
||||
function load(target, url) {
|
||||
var r = new XMLHttpRequest();
|
||||
r.open("GET", url, true);
|
||||
r.onreadystatechange = function () {
|
||||
if (r.readyState != 4 || r.status != 200) return;
|
||||
target.innerHTML = r.responseText;
|
||||
};
|
||||
r.send();
|
||||
}
|
||||
</script>
|
||||
|
||||
sleep(10);
|
||||
<form action="?">
|
||||
Task name
|
||||
<input type="text" name="task-name" value="<?= task_name ?>"/>
|
||||
<input type="submit" name="cmd" value="Get Info"/>
|
||||
<input type="submit" name="cmd" value="Execute"/>
|
||||
</form>
|
||||
|
||||
}), "\n");
|
||||
<script>
|
||||
|
||||
setInterval(() => {
|
||||
|
||||
load(document.getElementById('task-status'), 'task-status.uce?task-name=<?= uri_encode(task_name) ?>');
|
||||
|
||||
}, 200);
|
||||
|
||||
</script>
|
||||
|
||||
Task Status
|
||||
|
||||
<pre id="task-status" style="white-space: pre-wrap"><?
|
||||
|
||||
print("Task Name: ", task_name, "\n");
|
||||
print("Task ID: ", task_pid(task_name), "\n");
|
||||
print("Task Running: ", task_pid(task_name) == 0 ? "no" : "yes", "\n");
|
||||
|
||||
print("FCGI SOCKET: ", context->resources.fcgi_socket);
|
||||
|
||||
?></pre>
|
||||
|
||||
<?
|
||||
if(context->get["cmd"] == "Execute")
|
||||
{
|
||||
?>
|
||||
Task Start
|
||||
|
||||
<pre style="white-space: pre-wrap"><?
|
||||
|
||||
print("New Task ID: ", task("example-task", []() {
|
||||
|
||||
sleep(10);
|
||||
|
||||
}), "\n");
|
||||
|
||||
print("Task run time: 10 seconds");
|
||||
|
||||
?></pre><?
|
||||
}
|
||||
?>
|
||||
|
||||
Params
|
||||
<pre><?= var_dump(context->params) ?></pre>
|
||||
<pre><?= var_dump(context->get) ?></pre>
|
||||
</>
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user