johnsear/task-scheduler 问题修复 & 功能扩展

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

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

johnsear/task-scheduler

Composer 安装命令:

composer require johnsear/task-scheduler

包简介

A lightweight PHP library designed for scheduling and executing command-line tasks with cron syntax.

README 文档

README

(c) 2024 by John_Sear

A powerful yet lightweight library for scheduling and executing command-line tasks in PHP using flexible cron syntax.

Features

  • Supports tasks with CronTab Syntax for scheduling
  • Debug Mode to monitor scheduled task execution
  • Built-in, configurable PSR-3 Logging (default: logs to STDOUT/STDERR)
  • Fully compatible with ReactPHP for asynchronous task execution
  • Captures and logs exceptions without breaking execution
  • Simple API for defining, managing, and running tasks
  • Task Runner Status Tracking for monitoring execution statistics 📊

Installation

Install the library using Composer:

composer require johnsear/task-scheduler

Usage

Here's how to quickly get started with the task scheduler:

1. Basic Initialization Example:

<?php

use JohnSear\TaskScheduler\Runner;
use JohnSear\TaskScheduler\Task;

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

// Initialize the Runner
$runner = new Runner();

// Define a Task (running every 5 minutes)
$task1 = new Task("*/5 * * * *", "echo 'Running every 5 minutes'");

// Add the task to the scheduler
$runner->addTask($task1);

// Start the Task Scheduler (runs until stopped manually)
$runner->startScheduler();

2. Advanced Usage:

You can further customize the scheduler by:

  • Adding a custom PSR-3 logger.
  • Configuring tasks with names, descriptions, or additional options.

Example:

<?php

use My\CoolLogger;

// Create a task with detailed options
$complexTask = new Task(
    "0 */1 * * *",                    // CronTab: Every hour
    "echo 'Hourly logs updated'",     // Command
    "Hourly Logger",                  // Name
    "Logs progress to stdout hourly"  // Description
);

// Disable command output (silent execution)
$complexTask->setOutput(false);

// Use your custom PSR-3 Logger implementation
$runner->setLogger(new CoolLogger());

// Enable Debug Mode (optional)
$runner->setDebug(true);

// Add the task to the scheduler
$runner->addTask($task1);

// Start the Task Scheduler (runs until stopped manually)
$runner->startScheduler();

3. Runner Status Tracking 📊

The Task Scheduler supports automatic Runner Status Tracking, which tracks:

  • Start time and run duration of the scheduler.
  • All tasks with their status (e.g., completed, active, etc.).
  • Task-specific metadata like run count and description.

This information is stored in JSON format for analysis or monitoring.

How to enable Runner Status Tracking?

By default, status tracking is enabled. You have fine-grained control over it!

<?php

use JohnSear\TaskScheduler\Runner;
use JohnSear\TaskScheduler\Task;

// Initialize the Runner
$runner = new Runner('My Custom Runner');

// Set a custom status directory (optional)
$runner->setStatusDir('/custom/path/to/track_status/');

// Define and add tasks
$task1 = new Task("*/5 * * * *", "echo 'Task 1 running every 5 minutes'");
$runner->addTask($task1);

// Start runner and track status
$runner->startScheduler();

What does the status file contain?

The tracked status is saved as a JSON file. The file is called runner_<name>_status.json and is located in the system's temporary directory by default (can be customized).

Example status JSON file:

{
    "startTime": "2024-01-15 08:00:00",
    "runDuration": "3600 seconds",
    "totalTasks": 2,
    "tasks": {
        "task1": {
            "name": "Task 1",
            "description": "Task 1 running every 5 minutes",
            "cronTab": "*/5 * * * *",
            "runCount": 12,
            "isRunning": false
        },
        "task2": {
            "name": "Task 2",
            "description": "Another periodic task",
            "cronTab": "0 * * * *",
            "runCount": 1,
            "isRunning": true
        }
    },
    "activeTasks": {
        "task2": {
            "name": "Task 2",
            "startedAt": "2024-01-15 09:00:00"
        }
    }
}

Turning Off Status Tracking

If you prefer not to track status, you can disable it explicitly:

$runner->trackStatus(false);

Why is status tracking useful?

  • Real-Time Monitoring: Monitor what tasks are running and for how long.
  • Debugging: Simplify debugging with detailed task metadata.
  • Analytics: Collect run statistics that help optimize tasks (e.g., which tasks run most often).

Important Notes

Important:

  • This library is not suitable for CPU-intensive or long-running tasks, as asynchronous execution can cause resource bottlenecks.
  • Ensure all tasks are lightweight and quick to execute.

(c) 2024 by John Sear. Released under the MIT License.

johnsear/task-scheduler 适用场景与选型建议

johnsear/task-scheduler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cli」 「cron」 「command」 「task」 「background」 「job」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 johnsear/task-scheduler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 johnsear/task-scheduler 我们能提供哪些服务?
定制开发 / 二次开发

基于 johnsear/task-scheduler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-19