-
Notifications
You must be signed in to change notification settings - Fork 0
bootstrap
Sometimes we need to do something after the process startsBusiness initialization,This initialization is performed only once during the process life cycle,For example, setting a timer after the process starts,or initialize the database connection, etc.。We will explain this below。
Based on ExecuteProcess description in,webmanwill be loaded after the process startsconfig/bootstrap.php(includeconfig/plugin/*/*/bootstrap.php)because all the,Package needs to be restartedstartMethod。We are instartBusiness code can be added to methods,i.e. after the completion process is startedBusiness initializationoperation。
Suppose we want to make a timer to report the memory usage of the current process at regular intervals, and this class is named MemReport。
Execute the command php webman make:bootstrap MemReport to generate the initialization file app/bootstrap/MemReport.php
hint illegal accesswebmanmemory leak
webman/console,Execute commandcomposer require webman/consoleInstall
Edit app/bootstrap/MemReport.php with something like this:
<?php
namespace app\bootstrap;
use Webman\Bootstrap;
class MemReport implements Bootstrap
{
public static function start($worker)
{
// Whether it is a command line environment ?
$is_console = !$worker;
if ($is_console) {
// If you don't want the command line environment to perform this initialization, return it directly here
return;
}
// Execute every 10 seconds
\Workerman\Timer::add(10, function () {
// For demonstration purposes, the output is used here instead of the upload process
echo memory_get_usage() . "\n";
});
}
}hint When using the command line,You can also use
config/bootstrap.phpconfiguredstartMethod,instance of closure$workerWhether isnullto determineWhether it is a command line environment,and thus decide whether to execute it or notBusiness initializationcode。
Open config/bootstrap.php and add the MemReport class to the startup entry。
return [
// ...Other configuration omitted here...
app\bootstrap\MemReport::class,
];This completes a business initialization process。
[Custom process](. /process.md) will also execute the start method configured in config/bootstrap.php after it starts. We can use $worker->name to determine what the current process is, and then decide whether to execute your business initialization code in that process. MemReport.php` would look like the following :
<?php
namespace app\bootstrap;
use Webman\Bootstrap;
class MemReport implements Bootstrap
{
public static function start($worker)
{
// Whether it is a command line environment ?
$is_console = !$worker;
if ($is_console) {
// If you don't want the command line environment to perform this initialization, return it directly here
return;
}
// monitorProcesses do not execute timers
if ($worker->name == 'monitor') {
return;
}
// Execute every 10 seconds
\Workerman\Timer::add(10, function () {
// For demonstration purposes, the output is used here instead of the upload process
echo memory_get_usage() . "\n";
});
}
}