crutch/daemon 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

crutch/daemon

最新稳定版本:v1.1.0

Composer 安装命令:

composer require crutch/daemon

包简介

daemon helper

README 文档

README

Daemon helper

Install

composer require crutch/daemon:^1.0

Usage

Create worker runner

#!/usr/bin/env php
<?php
// crutch_worker_demo.php

declare(strict_types=1);

require_once __DIR__ . '/vendor/autoload.php';

$worker = function () {
    while (true) {
        sleep(1);
    }
};

$pidFile = '/tmp/crutch-daemon.pid';

$daemon = new \Crutch\Daemon\Daemon($pidFile);

$action = $argv[1] ?? '';
$num = max(1, intval($argv[2] ?? '1'));


switch ($action) {
    case 'start':
        try {
            $daemon->start($worker, $num);
        } catch (\Crutch\Daemon\Exception\DaemonException $exception) {
            printf("Could not start daemon: %s\n", $exception->getMessage());
            exit($exception->getCode());
        }
        break;
    case 'listen':
        $daemon->listen($worker);
        break;
    case 'stop':
        try {
            $daemon->stop();
        } catch (\Crutch\Daemon\Exception\DaemonException $exception) {
            printf("Could not stop daemon: %s\n", $exception->getMessage());
            exit($exception->getCode());
        }
        break;
    default:
        print("unknown action\n");
        exit(127);
}

Run it

php ./crutch_worker_demo.php start 3

Check processes

ps aux | grep crutch_worker_demo
user       58576  0.0  0.0 140080 11624 ?        Ss   17:58   0:00 php ./crutch_worker_demo.php start
user       58577  0.0  0.0 140080  9584 ?        S    17:58   0:00 php ./crutch_worker_demo.php start
user       58578  0.0  0.0 140080  9584 ?        S    17:58   0:00 php ./crutch_worker_demo.php start
user       58579  0.0  0.0 140080  9584 ?        S    17:58   0:00 php ./crutch_worker_demo.php start
user       58580  0.0  0.0   9116  2488 pts/3    S+   17:58   0:00 grep --color=auto crutch_worker_demo

You see 1 parent process (PID=58576) and 3 child processes (PIDs=58577, 58578 and 58579). Process 58580 is a grep, ignore it.

Stop process

php ./crutch_worker_demo.php stop

Check processes

ps aux | grep crutch_worker_demo
user       59825  0.0  0.0   9116  2488 pts/3    S+   17:58   0:00 grep --color=auto crutch_worker_demo

All processes has been killed.

统计信息

  • 总下载量: 18
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-04