jiordiviera/laravel-smart-scheduler 问题修复 & 功能扩展

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

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

jiordiviera/laravel-smart-scheduler

Composer 安装命令:

composer require jiordiviera/laravel-smart-scheduler

包简介

A Laravel package for smart task scheduling

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads License

A Laravel package for intelligent scheduled task management with observability, reliability features, and automatic stuck task detection.

Features

  • Task Execution Tracking: Record every scheduled task execution with status, duration, and output
  • Overlap Prevention: Automatically prevent concurrent executions of the same task
  • Stuck Task Detection: Automatically detect and handle tasks that are stuck (server crash, process killed)
  • Failure Notifications: Email notifications for failed and stuck tasks (extensible to other channels)
  • Task History: Maintain execution history with server information
  • Purge Command: Clean up old execution records

Requirements

  • PHP 8.2+
  • Laravel 11.x or 12.x

Installation

Install via Composer:

composer require jiordiviera/laravel-smart-scheduler

Publish the configuration and migrations:

php artisan vendor:publish --tag=smart-scheduler-config
php artisan vendor:publish --tag=smart-scheduler-migrations

Run migrations:

php artisan migrate

Configuration

The package automatically registers a service provider. Configure settings in config/smart-scheduler.php:

return [
    // Days to retain execution records
    'purge_days' => 7,

    // Minutes before a task is considered stuck
    'stuck_timeout_minutes' => 60,

    'notifications' => [
        'email' => [
            'recipients' => ['admin@example.com'],
        ],
        'notify_on_stuck' => true,
    ],
];

Or use environment variables:

SMART_SCHEDULER_PURGE_DAYS=7
SMART_SCHEDULER_STUCK_TIMEOUT=60
SMART_SCHEDULER_EMAIL_RECIPIENTS=admin@example.com,ops@example.com
SMART_SCHEDULER_NOTIFY_STUCK=true

Usage

The package automatically tracks all scheduled tasks defined in routes/console.php:

use Illuminate\Support\Facades\Schedule;

Schedule::command('backup:run')->daily();
Schedule::command('reports:generate')->hourly();

Viewing Execution History

Query the smart_schedule_runs table:

use Jiordiviera\SmartScheduler\LaravelSmartScheduler\Models\ScheduleRun;

// Get recent executions
$runs = ScheduleRun::latest('started_at')->limit(10)->get();

// Get failed executions
$failed = ScheduleRun::where('status', ScheduleRun::STATUS_FAILED)->get();

// Get stuck executions
$stuck = ScheduleRun::where('status', ScheduleRun::STATUS_STUCK)->get();

Purging Old Records

Remove old successful and ignored records:

# Purge records older than configured days
php artisan smart-scheduler:purge

# Purge with custom retention period
php artisan smart-scheduler:purge --days=30

# Dry run to preview what would be deleted
php artisan smart-scheduler:purge --dry-run

Detecting Stuck Tasks

Manually detect and mark stuck tasks:

# Detect stuck tasks (default: 60 minutes timeout)
php artisan smart-scheduler:detect-stuck

# Custom timeout
php artisan smart-scheduler:detect-stuck --timeout=30

# Dry run (preview only)
php artisan smart-scheduler:detect-stuck --dry-run

# Without sending notifications
php artisan smart-scheduler:detect-stuck --no-notify

Note: Stuck tasks are also automatically detected when a new execution of the same task starts.

Recommended Scheduler Setup

Add these commands to your routes/console.php for automatic maintenance:

use Illuminate\Support\Facades\Schedule;

// Purge old records weekly
Schedule::command('smart-scheduler:purge')->weekly();

// Detect stuck tasks every 30 minutes
Schedule::command('smart-scheduler:detect-stuck')->everyThirtyMinutes();

How It Works

The package uses Laravel's event system to intercept scheduled task lifecycle:

┌─────────────────────────────────────────────────────────────────┐
│                    Task Execution Flow                          │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  ScheduledTaskStarting                                          │
│         │                                                       │
│         ▼                                                       │
│  ┌──────────────┐     ┌──────────────┐                         │
│  │ Check stuck  │────▶│ Mark as stuck│ (if > timeout)          │
│  │ tasks        │     │ + notify     │                         │
│  └──────────────┘     └──────────────┘                         │
│         │                                                       │
│         ▼                                                       │
│  ┌──────────────┐     ┌──────────────┐                         │
│  │ Check overlap│────▶│ STATUS_IGNORED│ (if running)           │
│  └──────────────┘     └──────────────┘                         │
│         │                                                       │
│         ▼                                                       │
│  ┌──────────────┐                                              │
│  │STATUS_STARTING│                                              │
│  └──────────────┘                                              │
│         │                                                       │
│         ▼                                                       │
│  ScheduledTaskFinished                                          │
│         │                                                       │
│         ▼                                                       │
│  ┌──────────────┐     ┌──────────────┐                         │
│  │ Exit code = 0│────▶│STATUS_SUCCESS │                         │
│  └──────────────┘     └──────────────┘                         │
│         │                                                       │
│         ▼                                                       │
│  ┌──────────────┐     ┌──────────────┐                         │
│  │ Exit code ≠ 0│────▶│STATUS_FAILED  │ + notify               │
│  └──────────────┘     └──────────────┘                         │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Task Statuses

Status Description
starting Task is currently running
success Task completed successfully (exit code 0)
failed Task failed (exit code ≠ 0)
ignored Task skipped due to overlap
stuck Task was running too long and marked as stuck

Extending Notifications

Implement SmartNotifierInterface to add custom notification channels:

use Jiordiviera\SmartScheduler\LaravelSmartScheduler\Contracts\SmartNotifierInterface;
use Jiordiviera\SmartScheduler\LaravelSmartScheduler\Models\ScheduleRun;

class SlackNotifier implements SmartNotifierInterface
{
    public function sendFailureNotification(ScheduleRun $run): void
    {
        // Send to Slack
    }

    public function sendStuckNotification(ScheduleRun $run): void
    {
        // Send to Slack
    }
}

Register in a service provider:

$this->app->singleton(SmartNotifierInterface::class, SlackNotifier::class);

Testing

composer test

Code Style

# Check
composer pint

# Fix
composer pint:fix

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

The MIT License (MIT). Please see License File for more information.

Credits

jiordiviera/laravel-smart-scheduler 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-01