diff --git a/bin/uce_fastcgi.debug.linux.bin b/bin/uce_fastcgi.debug.linux.bin index eea4039..8e19e76 100755 Binary files a/bin/uce_fastcgi.debug.linux.bin and b/bin/uce_fastcgi.debug.linux.bin differ diff --git a/doc/areas/string.txt b/doc/areas/string.txt index f9a93d6..db2554c 100644 --- a/doc/areas/string.txt +++ b/doc/areas/string.txt @@ -5,4 +5,6 @@ first join nibble split +str_to_lower +str_to_upper trim diff --git a/doc/areas/task.txt b/doc/areas/task.txt new file mode 100644 index 0000000..941fa48 --- /dev/null +++ b/doc/areas/task.txt @@ -0,0 +1,5 @@ +Task API + +kill +task +task_pid diff --git a/doc/pages/kill.txt b/doc/pages/kill.txt new file mode 100644 index 0000000..0094a4f --- /dev/null +++ b/doc/pages/kill.txt @@ -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 diff --git a/doc/pages/str_to_lower.txt b/doc/pages/str_to_lower.txt new file mode 100644 index 0000000..9d4da66 --- /dev/null +++ b/doc/pages/str_to_lower.txt @@ -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 diff --git a/doc/pages/str_to_upper.txt b/doc/pages/str_to_upper.txt new file mode 100644 index 0000000..b3eea34 --- /dev/null +++ b/doc/pages/str_to_upper.txt @@ -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 diff --git a/doc/pages/task.txt b/doc/pages/task.txt new file mode 100644 index 0000000..a23da4c --- /dev/null +++ b/doc/pages/task.txt @@ -0,0 +1,13 @@ +:sig +pid_t task(String key, std::function 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 diff --git a/doc/pages/task_pid.txt b/doc/pages/task_pid.txt new file mode 100644 index 0000000..ca1dc37 --- /dev/null +++ b/doc/pages/task_pid.txt @@ -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 diff --git a/src/lib/functionlib.cpp b/src/lib/functionlib.cpp index 94bc9e8..58092f4 100644 --- a/src/lib/functionlib.cpp +++ b/src/lib/functionlib.cpp @@ -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(); diff --git a/src/lib/functionlib.h b/src/lib/functionlib.h index 81b2deb..9aa750c 100644 --- a/src/lib/functionlib.h +++ b/src/lib/functionlib.h @@ -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"); diff --git a/src/lib/sys.cpp b/src/lib/sys.cpp index c762d6c..2036a2c 100644 --- a/src/lib/sys.cpp +++ b/src/lib/sys.cpp @@ -423,6 +423,21 @@ void spawn_subprocess(std::function 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 exec_after_spawn, u64 timeout) { String status_file_name = context->server->config.BIN_DIRECTORY + "/task-" + key; diff --git a/src/lib/sys.h b/src/lib/sys.h index 817ff9a..0b2cb1f 100644 --- a/src/lib/sys.h +++ b/src/lib/sys.h @@ -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 exec_after_spawn, u64 timeout = 60*10); +pid_t task_pid(String key); diff --git a/src/lib/uri.cpp b/src/lib/uri.cpp index 481190a..0bcbfa4 100644 --- a/src/lib/uri.cpp +++ b/src/lib/uri.cpp @@ -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); diff --git a/test/task-status.uce b/test/task-status.uce new file mode 100644 index 0000000..4891132 --- /dev/null +++ b/test/task-status.uce @@ -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"); +} diff --git a/test/task.uce b/test/task.uce index e3d65a8..ad9d129 100644 --- a/test/task.uce +++ b/test/task.uce @@ -1,12 +1,10 @@ - - - - RENDER() { DTree t; + String task_name = first(context->get["task-name"], "example-task"); + <>

@@ -14,20 +12,69 @@ RENDER() Tasks

-

 
-			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();
+		}
+		
 
-				sleep(10);
+		
+ Task name + + + +
- }), "\n"); + + + Task Status + +
resources.fcgi_socket);
 
 		?>
+ get["cmd"] == "Execute") + { + ?> + Task Start + +
+ Params -
params) ?>
+
get) ?>
}