定制 danil-kashin/worker 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

danil-kashin/worker

最新稳定版本:v1.0.1

Composer 安装命令:

composer require danil-kashin/worker

包简介

Abstract tick-based worker with signal handling support.

README 文档

README

A minimal PHP library for building tick-based background workers with signal handling.

Requirements

  • PHP 8.3+
  • Extension: ext-pcntl

Installation

composer require danil-kashin/worker

Usage

Extend the Worker class and implement the tick() method:

use DanilKashin\Worker\Worker;

class MyWorker extends Worker
{
    protected function tick(): void
    {
        // Your logic runs here on every tick
    }
}

(new MyWorker())->run();

Limiting ticks

Pass maxTicks to run the worker a fixed number of times and exit — useful for cron jobs or batch processing:

(new MyWorker(maxTicks: 100))->run();

Override getDefaultMaxTicks() if the limit is intrinsic to the worker class itself:

protected function getDefaultMaxTicks(): ?int
{
    return 100;
}

null (the default) means the worker runs indefinitely until a stop signal is received.

Tick interval

Override getTickIntervalMs() to control how long the worker sleeps between ticks (default: 100ms):

protected function getTickIntervalMs(): int
{
    return 500; // 500ms between ticks
}

Graceful shutdown

The worker handles SIGTERM and SIGINT automatically — it finishes the current tick before stopping.

Override onStopping() to run cleanup logic before the process exits:

protected function onStopping(): void
{
    // flush buffers, close connections, etc.
}

If you need to react to a stop signal mid-tick (e.g. to break out of an inner loop early):

protected function tick(): void
{
    foreach ($this->getItems() as $item) {
        if ($this->shouldStop()) {
            break;
        }

        $this->process($item);
    }
}

Error handling

Exceptions thrown inside tick() are caught, written to stderr, and the worker continues running. Override handleTickError() to customize this behavior:

protected function handleTickError(Throwable $e): void
{
    // custom error handling
}

Running via CLI

The package ships with bin/run_worker, which instantiates and runs any Worker subclass by its fully qualified class name. Constructor parameters are passed as --name=value flags.

vendor/bin/run_worker "App\Workers\MyWorker"

Required constructor parameters without defaults will cause the script to fail with a clear error if omitted.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-03-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固