task() API, documentation work
This commit is contained in:
@@ -53,6 +53,7 @@ FastCGIServer::RequestInfo::RequestInfo() :
|
||||
in_closed(false),
|
||||
output_closed(false)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -71,6 +72,7 @@ FastCGIServer::FastCGIServer()
|
||||
|
||||
FastCGIServer::~FastCGIServer()
|
||||
{
|
||||
|
||||
if(my_pid != parent_pid) // if we're a child process, we must not close the handles
|
||||
return;
|
||||
|
||||
@@ -214,6 +216,7 @@ FastCGIServer::process(int timeout_ms)
|
||||
if (posix_con == -1)
|
||||
throw std::runtime_error("accept() failed");
|
||||
read_sockets[posix_con] = new Connection();
|
||||
read_sockets[posix_con]->posix_con = posix_con;
|
||||
}
|
||||
|
||||
for (std::map<int, Connection*>::iterator it = read_sockets.begin();
|
||||
@@ -377,6 +380,7 @@ FastCGIServer::process_connection_read(Connection& connection)
|
||||
request_arena->clear();
|
||||
switch_to_arena(request_arena);
|
||||
RequestInfo* new_request = new RequestInfo();
|
||||
new_request->resources.fcgi_socket = connection.posix_con;
|
||||
new_request->mem = request_arena;
|
||||
new_request->stats.time_init = microtime();
|
||||
switch_to_system_alloc();
|
||||
|
||||
@@ -77,6 +77,7 @@ protected:
|
||||
Connection();
|
||||
|
||||
RequestList requests;
|
||||
u64 posix_con = 0;
|
||||
std::string input_buffer;
|
||||
std::string output_buffer;
|
||||
bool close_responsibility;
|
||||
|
||||
@@ -386,68 +386,3 @@ String ob_get_close()
|
||||
return(result);
|
||||
}
|
||||
|
||||
#define BIT_NOISE1 0xB5297A4D
|
||||
#define BIT_NOISE2 0x68E31DA4
|
||||
#define BIT_NOISE3 0x1B56C4E9
|
||||
|
||||
// based on Squirrel3 https://www.youtube.com/watch?v=LWFzPP8ZbdU&t=2666s
|
||||
u32 noise32(u32 index, u32 seed)
|
||||
{
|
||||
u32 r = index;
|
||||
r *= BIT_NOISE1;
|
||||
r += seed;
|
||||
r ^= (r >> 8);
|
||||
r += BIT_NOISE2;
|
||||
r ^= (r << 8);
|
||||
r *= BIT_NOISE3;
|
||||
r ^= (r >> 8);
|
||||
return(r);
|
||||
}
|
||||
|
||||
#define BIT_NOISE61 0x5134811636f8cc8a
|
||||
#define BIT_NOISE62 0xb8E31DA41B56C4E9
|
||||
#define BIT_NOISE63 0x18cd227aaa1168c1
|
||||
|
||||
u64 noise64(u64 index, u64 seed)
|
||||
{
|
||||
u64 r = index;
|
||||
r *= BIT_NOISE61;
|
||||
r += seed;
|
||||
r ^= (r >> 8);
|
||||
r += BIT_NOISE62;
|
||||
r ^= (r << 8);
|
||||
r *= BIT_NOISE63;
|
||||
r ^= (r >> 8);
|
||||
return(r);
|
||||
}
|
||||
|
||||
#define MAX_64 0xffffffffffffffff
|
||||
|
||||
f64 noise01(u64 index, u64 seed)
|
||||
{
|
||||
return((float)noise64(index, seed)/(float)MAX_64);
|
||||
}
|
||||
|
||||
u64 generate_int(u64 from, u64 to, u64 index, u64 seed)
|
||||
{
|
||||
u64 b = 1 + to - from;
|
||||
return(from + (noise64(index, seed) % b));
|
||||
}
|
||||
|
||||
#include <tgmath.h>
|
||||
f64 generate_float(f64 from, f64 to, u64 index, u64 seed, f64 decimal_precision)
|
||||
{
|
||||
f64 b = to - from;
|
||||
return(from + fmod( decimal_precision*(f64)noise64(index, seed), b));
|
||||
}
|
||||
|
||||
u64 draw_int(u64 from, u64 to)
|
||||
{
|
||||
return(generate_int(from, to, context->random_index++, context->random_seed));
|
||||
}
|
||||
|
||||
f64 draw_float(f64 from, f64 to, f64 decimal_precision)
|
||||
{
|
||||
return(generate_float(from, to, context->random_index++, context->random_seed, decimal_precision));
|
||||
}
|
||||
|
||||
|
||||
@@ -46,10 +46,3 @@ void ob_clear();
|
||||
String ob_get_clear();
|
||||
String ob_get();
|
||||
|
||||
u32 noise32(u32 index, u32 seed = 0);
|
||||
u64 noise64(u64 index, u64 seed = 0);
|
||||
f64 noise01(u64 index, u64 seed = 0);
|
||||
u64 generate_int(u64 from, u64 to, u64 index, u64 seed = 0);
|
||||
f64 generate_float(f64 from, f64 to, u64 index, u64 seed = 0, f64 decimal_precision = 0.000000000001);
|
||||
u64 draw_int(u64 from, u64 to);
|
||||
f64 draw_float(f64 from, f64 to, f64 decimal_precision = 0.000000000001);
|
||||
|
||||
@@ -273,3 +273,69 @@ sha1(String s, bool as_binary)
|
||||
result += to_hex(v[i], 2);
|
||||
return(result);
|
||||
}
|
||||
|
||||
#define BIT_NOISE1 0xB5297A4D
|
||||
#define BIT_NOISE2 0x68E31DA4
|
||||
#define BIT_NOISE3 0x1B56C4E9
|
||||
|
||||
// based on Squirrel3 https://www.youtube.com/watch?v=LWFzPP8ZbdU&t=2666s
|
||||
u32 noise32(u32 index, u32 seed)
|
||||
{
|
||||
u32 r = index;
|
||||
r *= BIT_NOISE1;
|
||||
r += seed;
|
||||
r ^= (r >> 8);
|
||||
r += BIT_NOISE2;
|
||||
r ^= (r << 8);
|
||||
r *= BIT_NOISE3;
|
||||
r ^= (r >> 8);
|
||||
return(r);
|
||||
}
|
||||
|
||||
#define BIT_NOISE61 0x5134811636f8cc8a
|
||||
#define BIT_NOISE62 0xb8E31DA41B56C4E9
|
||||
#define BIT_NOISE63 0x18cd227aaa1168c1
|
||||
|
||||
u64 noise64(u64 index, u64 seed)
|
||||
{
|
||||
u64 r = index;
|
||||
r *= BIT_NOISE61;
|
||||
r += seed;
|
||||
r ^= (r >> 8);
|
||||
r += BIT_NOISE62;
|
||||
r ^= (r << 8);
|
||||
r *= BIT_NOISE63;
|
||||
r ^= (r >> 8);
|
||||
return(r);
|
||||
}
|
||||
|
||||
#define MAX_64 0xffffffffffffffff
|
||||
|
||||
f64 noise01(u64 index, u64 seed)
|
||||
{
|
||||
return((float)noise64(index, seed)/(float)MAX_64);
|
||||
}
|
||||
|
||||
u64 generate_int(u64 from, u64 to, u64 index, u64 seed)
|
||||
{
|
||||
u64 b = 1 + to - from;
|
||||
return(from + (noise64(index, seed) % b));
|
||||
}
|
||||
|
||||
#include <tgmath.h>
|
||||
f64 generate_float(f64 from, f64 to, u64 index, u64 seed, f64 decimal_precision)
|
||||
{
|
||||
f64 b = to - from;
|
||||
return(from + fmod( decimal_precision*(f64)noise64(index, seed), b));
|
||||
}
|
||||
|
||||
u64 draw_int(u64 from, u64 to)
|
||||
{
|
||||
return(generate_int(from, to, context->random_index++, context->random_seed));
|
||||
}
|
||||
|
||||
f64 draw_float(f64 from, f64 to, f64 decimal_precision)
|
||||
{
|
||||
return(generate_float(from, to, context->random_index++, context->random_seed, decimal_precision));
|
||||
}
|
||||
|
||||
|
||||
@@ -7,3 +7,13 @@ By Steve Reid <steve@edmweb.com>
|
||||
|
||||
|
||||
String sha1(String s, bool as_binary = false);
|
||||
|
||||
u32 noise32(u32 index, u32 seed = 0);
|
||||
u64 noise64(u64 index, u64 seed = 0);
|
||||
f64 noise01(u64 index, u64 seed = 0);
|
||||
|
||||
u64 generate_int(u64 from, u64 to, u64 index, u64 seed = 0);
|
||||
f64 generate_float(f64 from, f64 to, u64 index, u64 seed = 0, f64 decimal_precision = 0.000000000001);
|
||||
|
||||
u64 draw_int(u64 from, u64 to);
|
||||
f64 draw_float(f64 from, f64 to, f64 decimal_precision = 0.000000000001);
|
||||
|
||||
@@ -42,6 +42,11 @@ bool MySQL::connect(String host, String username, String password)
|
||||
}
|
||||
|
||||
String MySQL::escape(String raw, char quote_char)
|
||||
{
|
||||
return(mysql_escape(raw, quote_char));
|
||||
}
|
||||
|
||||
String mysql_escape(String raw, char quote_char)
|
||||
{
|
||||
String result;
|
||||
result.append(1, quote_char);
|
||||
|
||||
@@ -31,3 +31,37 @@ struct MySQL {
|
||||
DTree get_pending_result();
|
||||
|
||||
};
|
||||
|
||||
MySQL* mysql_connect(String host = "localhost", String username = "root", String password = "")
|
||||
{
|
||||
MySQL* m = new MySQL();
|
||||
m->connect(host, username, password);
|
||||
return(m);
|
||||
}
|
||||
|
||||
void mysql_disconnect(MySQL* m)
|
||||
{
|
||||
m->disconnect();
|
||||
}
|
||||
|
||||
String mysql_error(MySQL* m)
|
||||
{
|
||||
return(m->error());
|
||||
}
|
||||
|
||||
String mysql_escape(String raw, char quote_char);
|
||||
|
||||
DTree mysql_query(MySQL* m, String q)
|
||||
{
|
||||
return(m->query(q));
|
||||
}
|
||||
|
||||
DTree mysql_query(MySQL* m, String q, StringMap params)
|
||||
{
|
||||
return(m->query(q, params));
|
||||
}
|
||||
|
||||
u64 mysql_insert_id(MySQL* m)
|
||||
{
|
||||
return(m->insert_id);
|
||||
}
|
||||
|
||||
@@ -423,6 +423,44 @@ void spawn_subprocess(std::function<void()> exec_after_spawn)
|
||||
}
|
||||
}
|
||||
|
||||
pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout)
|
||||
{
|
||||
String status_file_name = context->server->config.BIN_DIRECTORY + "/task-" + key;
|
||||
String status_file = file_get_contents(status_file_name);
|
||||
pid_t p;
|
||||
if(status_file != "")
|
||||
{
|
||||
p = int_val(status_file);
|
||||
if(kill(p, 0) == 0) // process is still running
|
||||
{
|
||||
printf("(P) worker process '%s' already running: PID %i\n", key.c_str(), p);
|
||||
return(p);
|
||||
}
|
||||
//printf("(P) worker process '%s' had crashed: PID %i\n", key.c_str(), p);
|
||||
unlink(status_file_name);
|
||||
}
|
||||
p = fork();
|
||||
if(p == 0)
|
||||
{
|
||||
my_pid = getpid();
|
||||
file_put_contents(status_file_name, std::to_string(my_pid));
|
||||
|
||||
close(context->resources.fcgi_socket);
|
||||
context->resources.fcgi_socket = 0;
|
||||
//printf("(C) child procress started, PID:%i\n", my_pid);
|
||||
//prctl(PR_SET_PDEATHSIG, SIGHUP);
|
||||
exec_after_spawn();
|
||||
unlink(status_file_name);
|
||||
printf("(P) worker process '%s' terminated: PID %i\n", key.c_str(), my_pid);
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("(P) worker process '%s' spawned: PID %i\n", key.c_str(), p);
|
||||
return(p);
|
||||
}
|
||||
}
|
||||
|
||||
void on_child_exit(int sig)
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
@@ -39,3 +39,4 @@ 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);
|
||||
|
||||
@@ -268,6 +268,7 @@ struct Request {
|
||||
struct Resources {
|
||||
std::vector<u64> sockets;
|
||||
std::vector<void*> mysql_connections;
|
||||
u64 fcgi_socket = 0;
|
||||
} resources;
|
||||
|
||||
void invoke(String file_name);
|
||||
|
||||
Reference in New Issue
Block a user