Skip to content

Commit 606597a

Browse files
authored
Add Process Manager example with server and clustering
A simple Process Manager ( like PM2 ) made with nodepp; <65 LOC
1 parent 2feb3ab commit 606597a

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

examples/17-Process-Manager.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <nodepp/nodepp.h>
2+
#include <nodepp/worker.h>
3+
#include <nodepp/date.h>
4+
#include <nodepp/http.h>
5+
#include <nodepp/cluster.h>
6+
7+
using namespace nodepp;
8+
9+
/*────────────────────────────────────────────────────────────────────────────*/
10+
11+
void server( int x ){
12+
13+
auto server = http::server([=]( http_t cli ){
14+
15+
console::log( cli.path, cli.get_fd() );
16+
17+
cli.write_header( 200, header_t({
18+
{ "content-type", "text/html" }
19+
}));
20+
21+
cli.write( date::fulltime() );
22+
cli.close();
23+
24+
});
25+
26+
server.listen( "localhost", 8000, [=]( socket_t server ){
27+
console::log("server started at http://localhost:8000");
28+
});
29+
30+
}
31+
32+
/*────────────────────────────────────────────────────────────────────────────*/
33+
34+
void spawn( int x ) {
35+
36+
auto cid = regex::format( "?CPU=${0}", x );
37+
auto pid = cluster::add( ptr_t<string_t>({ cid }) );
38+
39+
if( pid.has_value() ){
40+
pid.value().onClose.once([=](){ spawn(x); });
41+
pid.value().onData([=]( string_t msg ){
42+
conio::log( msg );
43+
});
44+
}
45+
46+
}
47+
48+
/*────────────────────────────────────────────────────────────────────────────*/
49+
50+
void onMain(){
51+
52+
os::set_process_priority( os::PRIORITY::HIGH_PRIORITY );
53+
os::set_hard_fileno/*-*/( (uint)-1 );
54+
os::set_soft_fileno/*-*/( (uint)-1 );
55+
56+
int y = string::to_uint( process::env::get( "CPU" ) );
57+
58+
for ( auto x=os::cpus(); x-->0; ){
59+
if ( cluster::is_child() )
60+
{ server(y); break; }
61+
else { spawn (x); } }
62+
63+
}
64+
65+
/*────────────────────────────────────────────────────────────────────────────*/

0 commit comments

Comments
 (0)