Sunday, March 28, 2010

OSX Process , simple php process controller

Running a PHP Script in background

When we deal with Medium / High sclale web applications, we have to run programs in background. Ex : Insert lacs of records in a table , every day. In such applications, it is not feasible to run the program from user input or browser. In unix systems it is easy to start a program from shell. Using UNIX commands we can control the programs easily. Here i will give you a simple PHP Library for handling process / programs running in background . This will help you to create multiple background process's and control them easily, so that you can effectively use CPU.
What is OSX_PROCESS

OSX_Process is PHP Library for creating and controlling process. You don't have to use any other PHP Extensions to handle process's. OSX_Process works only in UNIX Systems .

Usage example :


--PHP CODE --
include("Process.php");

$dirname = dirname(__FILE__)."/stat/"; // working directory for storing status and files
$fileToRun = dirname(__FILE__)."/demo.php"; // php script to run in background


$obj = new OSX_Process();
$obj->setDir($dirname);

//Create first process //
$obj->setInstance(1000);
$prcId1 = $obj->runPhp($fileToRun,array("someValue"=>900));
if($obj->checkProcess($prcId1)){
echo "Process 1 Running";
print_r($obj->getStat());
}else{
echo " Process 1 Stopped ";
}

//create another process//
$obj->setInstance(2000);
$prcId2 = $obj->runPhp($fileToRun,array("someValue"=>100));
if($obj->checkProcess($prcId2)){
echo "Process 2 Running";
print_r($obj->getStat());
}else{
echo " Process 2 Stopped ";
}
//Get Out,pdi file paths for process //
echo "Outfile".$obj->getOutFile();
echo "Pidfile".$obj->getPidFile();


echo "Killing processes";

$obj->killProcess($prcId1,1000);
$obj->killProcess($prcId2,2000);


$obj->clear(); /// clear should be called at the end ;




--PHP CODE--



Full source code / Downloads

Click here to download OSX_Process package
ZipFile contains , background.php , demo.php and Process.php .
background.php contains the sample implementation of process creation and control .

For further details / query contact : Developer
Note * : You have to run the script at your own risk. This is Beta version of the package.

0 comments: