Business Initialization
Sometimes we need to perform some business initialization after the process starts. This initialization only executes once during the lifecycle of the process, such as setting up a timer or initializing a database connection after the process starts. We will explain this below.
Principle
According to the Execution Flow description, webman will load the classes set in config/bootstrap.php
(including config/plugin/*/*/bootstrap.php
) after the process starts and execute the start method of the classes. We can add business logic in the start method to complete the business initialization operation after the process starts.
Process
Suppose we want to create a timer to report the current memory usage of the process regularly; we will name this class MemReport
.
Execute Command
Run the command php webman make:bootstrap MemReport
to generate the initialization file app/bootstrap/MemReport.php
.
Tip
If your webman is not installed withwebman/console
, run the commandcomposer require webman/console
to install it.
Edit the Initialization File
Edit app/bootstrap/MemReport.php
, and the content will look something like this:
<?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 a command line environment, return here
return;
}
// Execute every 10 seconds
\Workerman\Timer::add(10, function () {
// For demonstration purposes, using output instead of the reporting process
echo memory_get_usage() . "\n";
});
}
}
Tip
When using the command line, the framework will also execute the start method configured inconfig/bootstrap.php
. We can determine whether it is a command line environment by checking if$worker
is null, thus deciding whether to execute the business initialization code.
Configuration With Process Startup
Open config/bootstrap.php
and add the MemReport
class to the startup items.
return [
// ...other configurations omitted...
app\bootstrap\MemReport::class,
];
This completes a business initialization process.
Additional Notes
The start method configured in config/bootstrap.php
will also be executed after custom processes start. We can determine what type of process it is by using $worker->name
and further identify the process number through $worker->id
, and then decide whether to execute your business initialization code in that process. For example, if we only need to execute it in webman's 0th process, the content of MemReport.php
will look like this:
<?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 a command line environment, return here
return;
}
// Execute only in webman's 0th process
if ($worker->name != 'webman' && $worker->id != 0) {
return;
}
// Execute every 10 seconds
\Workerman\Timer::add(10, function () {
// For demonstration purposes, using output instead of the reporting process
echo memory_get_usage() . "\n";
});
}
}