Business Initialization
Sometimes, we need to perform some business initialization after the process starts. This initialization is only performed once during the process lifecycle, such as setting up a timer or initializing database connections after the process starts. In the following sections, we will explain how to achieve this.
Principle
According to the explanation in the Execution Process section, webman loads the classes set in config/bootstrap.php (including config/plugin/*/*/bootstrap.php) after the process starts, and executes the start method of these classes. We can add our business code to the start method to complete the business initialization after the process starts.
Procedure
Let's say we want to create a timer that reports the memory usage of the current process periodically. We'll name this class MemReport.
Execute Command
Run the command php webman make:bootstrap MemReport to generate the initialization file app/bootstrap/MemReport.php.
Note
If your webman does not havewebman/consoleinstalled, run the commandcomposer require webman/consoleto install it.
Edit Initialization File
Edit app/bootstrap/MemReport.php, and the content will look like the following:
<?php
namespace app\bootstrap;
use Webman\Bootstrap;
class MemReport implements Bootstrap
{
public static function start($worker)
{
// Is it a command line environment?
$is_console = !$worker;
if ($is_console) {
// If you don't want this initialization to be executed in the command line environment, return directly here.
return;
}
// Execute every 10 seconds
\Workerman\Timer::add(10, function () {
// For the sake of demonstration, we use output instead of reporting process.
echo memory_get_usage() . "\n";
});
}
}
Note
The framework also executes thestartmethod configured inconfig/bootstrap.phpwhen using the command line. We can determine whether it is a command line environment by checking whether$workerisnull, and decide whether to execute the business initialization code accordingly.
Configuration Starts with the Process
Open config/bootstrap.php and add the MemReport class to the list of startup items.
return [
// ... Other configurations are omitted here...
app\bootstrap\MemReport::class,
];
In this way, we have completed the business initialization process.
Additional Explanation
Custom Processes also execute the start method configured in config/bootstrap.php after they start. We can use $worker->name to determine which process is currently running, and further use $worker->id to determine the process number. Then we can decide whether to execute your business initialization code in that process. For example, if we only need to execute in webman's process 0, the content of MemReport.php will be as follows:
<?php
namespace app\bootstrap;
use Webman\Bootstrap;
class MemReport implements Bootstrap
{
public static function start($worker)
{
// Is it a command line environment?
$is_console = !$worker;
if ($is_console) {
// If you don't want this initialization to be executed in the command line environment, return directly here.
return;
}
// Only execute in webman's process 0
if ($worker->name != 'webman' || $worker->id != 0) {
return;
}
// Execute every 10 seconds
\Workerman\Timer::add(10, function () {
// For the sake of demonstration, we use output instead of reporting process.
echo memory_get_usage() . "\n";
});
}
}