bedelightful / task-scheduler
Installs: 0
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/bedelightful/task-scheduler
Requires
- php: >=8.1
- dragonmantank/cron-expression: ^3.3
- hyperf/config: ^3.0
- hyperf/crontab: ^3.0
- hyperf/db-connection: ^3.0
- hyperf/di: ^3.0
- hyperf/logger: ^3.0
- hyperf/redis: ^3.0
- hyperf/snowflake: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^10.0
- swoole/ide-helper: dev-master
Suggests
- swow/swow: Required to create swow components.
This package is auto-updated.
Last update: 2026-01-14 18:13:03 UTC
README
Installation
composer require bedelightful/task-scheduler
php bin/hyperf.php vendor:publish bedelightful/task-scheduler
Usage
\BeDelightful\TaskScheduler\Service\TaskSchedulerDomainService
Notes
Supports minute-level invocation only
Scheduling modes
- Cron scheduling
- Fixed-time scheduling
Creating scheduled tasks
- Cron schedules need a timer to generate tasks to run within the next n hours.
- Generate scheduled tasks based on the task time.
Executing tasks
- Run due tasks, update status, and emit an error event on failure.
- After finishing, archive the record to the log table.
Background jobs
- Daily: delete completed tasks older than n days to prevent the schedule table from growing too large.
- Every minute: generate tasks to run within the next n days.
- Every minute: execute tasks whose time has arrived.
Database
- task_scheduler: records tasks to be executed.
- task_scheduler_log: archives completed tasks for later review.
- task_scheduler_crontab: stores cron rules.
Create the tables
php bin/hyperf.php migrate
-- auto-generated definition create table task_scheduler ( id bigint unsigned not null primary key, external_id varchar(64) not null comment 'business id', name varchar(64) not null comment 'name', expect_time datetime not null comment 'expected execution time', actual_time datetime null comment 'actual execution time', type tinyint default 2 not null comment 'schedule type: 1 cron, 2 fixed time', cost_time int default 0 not null comment 'duration milliseconds', retry_times int default 0 not null comment 'remaining retries', status tinyint default 0 not null comment 'status', callback_method json not null comment 'callback method', callback_params json not null comment 'callback parameters', remark varchar(255) default '' not null comment 'remark', creator varchar(64) default '' not null comment 'creator', created_at datetime not null comment 'created at' ) collate = utf8mb4_unicode_ci; create index task_scheduler_external_id_index on task_scheduler (external_id); create index task_scheduler_status_expect_time_index on task_scheduler (status, expect_time); -- auto-generated definition create table task_scheduler_crontab ( id bigint unsigned not null primary key, name varchar(64) not null comment 'name', crontab varchar(64) not null comment 'crontab expression', last_gen_time datetime null comment 'last generation time', enabled tinyint(1) default 1 not null comment 'enabled', retry_times int default 0 not null comment 'total retry attempts', callback_method json not null comment 'callback method', callback_params json not null comment 'callback parameters', remark varchar(255) default '' not null comment 'remark', creator varchar(64) default '' not null comment 'creator', created_at datetime not null comment 'created at' ) collate = utf8mb4_unicode_ci; -- auto-generated definition -- auto-generated definition create table task_scheduler_log ( id bigint unsigned not null primary key, task_id bigint unsigned not null comment 'task ID', external_id varchar(64) not null comment 'business identifier', name varchar(64) not null comment 'name', expect_time datetime not null comment 'expected execution time', actual_time datetime null comment 'actual execution time', type tinyint default 2 not null comment 'type', cost_time int default 0 not null comment 'duration', status tinyint default 0 not null comment 'status', callback_method json not null comment 'callback method', callback_params json not null comment 'callback parameters', remark varchar(255) default '' not null comment 'remark', creator varchar(64) default '' not null comment 'creator', created_at datetime not null comment 'created at', result json null comment 'result' ) collate = utf8mb4_unicode_ci; create index task_scheduler_log_external_id_index on task_scheduler_log (external_id); create index task_scheduler_log_status_expect_time_index on task_scheduler_log (status, expect_time); create index task_scheduler_log_task_id_index on task_scheduler_log (task_id);