Process
Running the program or certain commands via the command line, or running part of the program in parallel and in the background, are among the things you can do using the packages\base\process class. The result of every process must be a response of the packages\base\response class type.
For more information, refer to the Response page.
Example 1 - a process file
namespace packages\packagename\processes;
use packages\base\{process, response};
use packages\packagename\ticket;
class tickets extends process
{
public function auto_close(): response
{
$response = new response(false);
$ticket = new ticket();
$ticket->where("status", 1); // ticket answered
$ticket->where("answered_at", time() - 86400, "<=");
$tickets = $ticket->get();
foreach ($tickets as $ticket) {
$ticket->status = 4; // ticket closed
$ticket->save();
}
$response->setStatus(true);
return $response;
}
}
Example 2 - a process file
namespace packages\packagename\processes;
use packages\base\{process, response};
use packages\packagename\phpmailer;
class Email extends process
{
public function send(array $parameter): response
{
$mail = new phpmailer();
$mail->setFrom($parameter["sender"], $parameter["sender_name"]);
$mail->addAddress($parameter["send_to"]);
$mail->Subject = "Jeyserver Support";
$mail->Body = "Your request has been done !";
$result = $mail->send();
return new response($result);
}
}
Calling processes
Process classes can be called and executed in two ways: via the command line or by instantiating the process class.
Calling via the command line
From the command line, go to the project's installation directory and run the index.php file, passing the process class name and method — joined together with @ — in a parameter named process.
cd /home/projectname/webserver
php index.php --process=packages/packagename/processes/tickets@auto_close
Calling via instantiation
For more information, refer to the Object-Oriented Interaction with the Database page.
To instantiate the process class, you must specify a name and a parameter. The name you specify for the process is the class and method to be called. If the process cannot find a class or method with the specified name, it throws an exception of type packages\base\proccessClass and halts the program's execution. The passed parameters are also sent to the process method exactly as-is. After saving the instance, you can call the process using one of the three methods below.
Running a process in the background
To run a process in parallel and in the background, you must use the background_run method.
Example
namespace packages\packagename\controllers;
use packages\base\{controller, process};
class Main extends controller
{
public function request_done(): response
{
$process = new process();
$process->name = "packages\\packagename\\processes\\Email@send";
$process->parameters = array(
"sender_name" => "Jey Support",
);
$process->save();
$process->background_run();
$response = new response(true);
$view = view::byName(views\tickets_list::class);
$response->setData("message (will be) sent", "message");
$response->setView($view);
return $response;
}
}
Running and waiting for a response
There are two ways to run the program and wait for its response:
Example 1
namespace packages\packagename\controllers;
use packages\base\{controller, process};
class Main extends controller
{
public function request_done(): response
{
$response = new response(true);
$view = view::byName(views\tickets_list::class);
$process = new process();
$process->name = "packages\\packagename\\processes\\Email@send";
$process->parameters = array(
"sender_name" => "Jey Support",
);
$process->save();
$process->run();
$is_done = $process->waitFor(10); // 10 second
if ($is_done) {
$response->setData("message sent", "message");
} else {
$response->setData("message will be sent", "message");
}
$response->setView($view);
return $response;
}
}
Example 2
namespace packages\packagename\controllers;
use packages\base\{controller, process};
class Main extends controller
{
public function request_done(): response
{
$response = new response(true);
$view = view::byName(views\tickets_list::class);
$process = new process();
$process->name = "packages\\packagename\\processes\\Email@send";
$process->parameters = array(
"sender_name" => "Jey Support",
);
$process->save();
$is_done = $process->runAndWaitFor(10); // 10 second
if ($is_done) {
$response->setData("message sent", "message");
} else {
$response->setData("message will be sent", "message");
}
$response->setView($view);
return $response;
}
}
Checking the process status
Using the isRunning method, you can check the status of a process. This method returns true if the process is running and false otherwise.