Databases Reference
In-Depth Information
"pidDir" : "..."
}
The process must create an empty file at the path specified by pidDir , whose name is
the process ID, and write the PID to standard out as a JSON object.
{ "pid" : 1234 }
For example, if you receive /tmp/example\n and the PID of your script is 123 , you should
create an empty file at /tmp/example/123 and print the lines {"pid": 123}n and end\n to
standard output. This is how Storm keeps track of the PID and kills the process when
it shuts down. Let's see how to do it in PHP:
$config = json_decode ( read_msg (), true );
$heartbeatdir = $config [ 'pidDir' ];
$pid = getmypid ();
fclose ( fopen ( " $heartbeatdir / $pid " , "w" ));
storm_send ([ "pid" => $pid ]);
flush ();
You've created a function called read_msg to handle reading messages from standard
input. The multilang protocol states that messages can be either a single line or multiple
lines encoded in JSON. A message is complete when Storm sends a single line with the
word end\n .
function read_msg () {
$msg = "" ;
while ( true ) {
$l = fgets ( STDIN );
$line = substr ( $l , 0 , - 1 );
if ( $line == "end" ) {
break ;
}
$msg = " $msg$line \n " ;
}
return substr ( $msg , 0 , - 1 );
}
function storm_send ( $json ) {
write_line ( json_encode ( $json ));
write_line ( "end" );
}
function write_line ( $line ) {
echo ( " $line \n " );
}
The use of flush() is very important; there might be a buffer that won't
be flushed until a specific amount of characters are accumulated. This
means that your script can hang forever waiting for an input from Storm,
which it will never receive because Storm is in turn waiting on an output
from your script. So it's important to make sure that when your script
outputs something it gets flushed immediately.
 
Search WWH ::




Custom Search