PHP code example of mojiehai / process_manage

1. Go to this page and download the library: Download mojiehai/process_manage library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mojiehai / process_manage example snippets



    $config = [
        // 进程基础配置
        'titlePrefix' => 'process_m',   // 进程前缀
        'baseTitle' => 'test',  // 进程基础名称

        // master 进程配置
        'checkWorkerInterval' => 0,    // n秒检测一次进程(<=0则为不检测)
        'maxWorkerNum' => 1,    //1个进程

        // worker 进程配置
        'executeTimes' => 1,    // 任务的最大执行次数(0为没有最大执行次数,一直执行)
        'executeUSleep' => 10000000,  // 每次执行任务睡眠时间(微秒) 1s = 1 000 000 us (1s)
        'limitSeconds' => 10800,    // 工作进程最大执行时长(秒)(跑3个小时重启)
    ];

    try {
        // 创建进程管理器
        (new Manage($config))
            ->setWorkInit(
                // 工作内容初始化
                function (Process $process) {
                    // init
                    \ProcessManage\Log\ProcessLog::Record('info', $process, 'work init ... ');
                }
            )
            ->setWork(
                // 执行的工作内容
                function(Worker $process) {
                    // work
                    \ProcessManage\Log\ProcessLog::Record('info', $process, 'work run ... ');
                })
            ->start();
    } catch (ProcessException $e) {
        echo $e->getExceptionAsString();
    }
    

    
    $config = [
        // 进程基础配置
        'baseTitle' => 'test',  // 进程基础名称
    ];
    
    try {
        // 创建进程管理器
        (new Manage($config))->stop();
    } catch (ProcessException $e) {
        echo $e->getExceptionAsString();
    }
    

    
    $config = [
        // 进程基础配置
        'titlePrefix' => 'process_m',   // 进程前缀
        'baseTitle' => 'test',  // 进程基础名称

        // master 进程配置
        'checkWorkerInterval' => 0,    // n秒检测一次进程(<=0则为不检测)
        'maxWorkerNum' => 1,    //1个进程

        // worker 进程配置
        'executeTimes' => 1,    // 任务的最大执行次数(0为没有最大执行次数,一直执行)
        'executeUSleep' => 10000000,  // 每次执行任务睡眠时间(微秒) 1s = 1 000 000 us (1s)
        'limitSeconds' => 10800,    // 工作进程最大执行时长(秒)(跑3个小时重启)
    ];
    
    try {
        // 创建进程管理器
        (new Manage($config))
            ->setWorkInit(
                // 工作内容初始化
                function (Process $process) {
                    // init
                    \ProcessManage\Log\ProcessLog::Record('info', $process, 'work init ... ');
                }
            )
            ->setWork(
                // 执行的工作内容
                function(Worker $process) {
                    // work
                    \ProcessManage\Log\ProcessLog::Record('info', $process, 'work run ... ');
                })
            ->setBackground()->restart();
    } catch (ProcessException $e) {
        echo $e->getExceptionAsString();
    }
    

     
    $config = [
        // 进程基础配置
        'baseTitle' => 'test',  // 进程基础名称
    ];
    
    try {
        // 创建进程管理器
        (new Manage($config))->showStatus();
    } catch (ProcessException $e) {
        echo $e->getExceptionAsString();
    }
    

		(new Manage($config))->setWorkInit(
		    // 工作内容初始化
		    function (Worker $process) {
			// init
			$link = mysqli_connect(...);
			...
			$redis = new Redis(...);
			...
			return ['mysql' => $link, 'redis' => $redis];
		    }
		 )
		

		(new Manage($config))->setWork(
		    // 执行的工作内容
		    function(Worker $process, $result = []) {
			// work
			$mysqlLink = $result['mysql'];
			$redisLink = $result['redis'];
		    })
		 )
		

            /**
             * 影响action的行为
             * @param Action $action
             * @return mixed
             */
            public function impactAction(Action $action)
            {
                // $this->param 存储了用户输入的这个附加参数所带的值
                $action->setParam('runInBackground', true);
            }
            

            /**
             * 命令映射的类
             * @var array
             */
            public $mapping = [
                'action' => [
                    'start' => '\ProcessManage\Command\Action\Start',
                    'stop' => '\ProcessManage\Command\Action\Stop',
                    'restart' => '\ProcessManage\Command\Action\ReStart',
                ],
                'options' => [
                    'd' => '\ProcessManage\Command\Options\D',
                ],
            ];
            

            /**
             * 获取模板内容
             * @return string
             */
            public function getTemplateStr()
            {
                return '<start|stop|restart> -[d]';
            }
            

    use ProcessManage\Command\Command;
    use ProcessManage\Command\Template\ManageProcessTemplate;

    $command = new Command(new ManageProcessTemplate());
    $command->run();