User process

Some services such as Http/RPC/Websocket/TCP require a background running process to monitor, report, or other special operations. At this time, a user-defined work process can be added when the corresponding service is started. The custom user process starts with the service, and the service closes and exits. If the custom user process is closed unexpectedly, the service restarts a new custom user process, ensuring that the custom user process persists.

2.0.4+ support and need to install the swoft-process component

Declare user process

Before using a custom user process, you must define a user process. For example, define a user process that monitors the reported information as follows:

Custom user process entry:

 <?php declare(strict_types=1);

namespace App\Process;

use App\Model\Logic\MonitorLogic;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Db\Exception\DbException;
use Swoft\Process\Process;
use Swoft\Process\UserProcess;

/**
 * Class MonitorProcess
 *
 * @since 2.0
 *
 * @Bean()
 */
class MonitorProcess extends UserProcess
{
    /**
     * @Inject()
     *
     * @var MonitorLogic
     */
    private $logic;

    /**
     * @param Process $process
     *
     * @throws DbException
     */
    public function run(Process $process): void
    {
        $this->logic->monitor($process);
    }
} 
  • Custom user processes must implement the Swoft\Process\UserProcess interface
  • Custom user processes must be marked with a @Bean as a bean object

Business processing:

 <?php declare(strict_types=1);

namespace App\Model\Logic;

use App\Model\Entity\User;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Db\Exception\DbException;
use Swoft\Log\Helper\CLog;
use Swoft\Process\Process;
use Swoft\Redis\Redis;
use Swoole\Coroutine;

/**
 * Class MonitorProcessLogic
 *
 * @since 2.0
 *
 * @Bean()
 */
class MonitorLogic
{
    /**
     * @param Process $process
     *
     * @throws DbException
     */
    public function monitor(Process $process): void
    {
        $process->name('swoft-monitor');

        while (true) {
            $connections = context()->getServer()->getSwooleServer()->connections;
            CLog::info('monitor = ' . json_encode($connections));

            // Database
            $user = User::find(1)->toArray();
            CLog::info('user='.json_encode($user));

            // Redis
            Redis::set('test', 'ok');
            CLog::info('test='.Redis::get('test'));

            Coroutine::sleep(3);
        }
    }
} 

In the custom user process, the developer must implement a service like `while(true)`, and it can directly use all the IO operations encapsulated by Svoft, such as database, cache, RPC, and other non-Swoft package coroutine operations.

Configuration

The user process is defined and must be configured to be valid. The Http/RPC/Websocket/TCP service configuration custom process is the same. Here is the custom user process configuration as above:

Configure a custom process:

  return [
    'httpServer'     => [
            'class'    => HttpServer::class,
            'port'     => 18306,
            'listener' => [
                'rpc' => bean('rpcServer')
            ],
            'process' => [
                'monitor' => bean(MonitorProcess::class)
            ],
            // ...
        ],
 ];    

Here is a custom user process configured with injection, named monitor

If the configuration is successful, after the service is started, the services in the user process are automatically executed without other operations.

/docs/2.x/en/process/user-process.html
progress-bar