VOOZH about

URL: https://deepwiki.com/hypervel/console/7-task-scheduling-system

⇱ Task Scheduling System | hypervel/console | DeepWiki


Loading...
Menu

Task Scheduling System

Purpose and Scope

This document provides a high-level overview of the task scheduling system in the hypervel/console package. The scheduling system allows developers to define and execute recurring tasks using a fluent API with cron-like expressions. This page covers the core architecture, key components, and execution flow.

For detailed information about specific aspects of the scheduling system, see:

Overview

The task scheduling system provides a programmatic way to define recurring tasks that run at specified intervals. Instead of managing multiple cron entries, developers define all scheduled tasks in code using a fluent API. The system supports:

  • Command scheduling (both console commands and system shell commands)
  • Closure/callback scheduling
  • Job dispatching (queue integration)
  • Flexible frequency definitions (hourly, daily, weekly, custom cron expressions)
  • Concurrency control (prevent overlapping executions)
  • Multi-server coordination (single-server execution)
  • Conditional execution based on filters and environments

The scheduler runs as either a long-running daemon or in one-time mode (suitable for system cron integration).

Sources: src/Scheduling/Schedule.php1-413 src/Scheduling/Event.php1-738

Core Architecture

Component Overview

The scheduling system consists of two primary classes that work together:


Sources: src/Scheduling/Schedule.php32-118 src/Scheduling/Event.php35-104

Class Responsibilities

ClassPurposeLocation
ScheduleCentral registry that manages collections of scheduled events. Provides factory methods for creating different task types.src/Scheduling/Schedule.php32
EventRepresents a single scheduled task with its cron expression, command, callbacks, and configuration.src/Scheduling/Event.php35
CallbackEventSpecialized event for executing PHP closures.Referenced in src/Scheduling/Schedule.php123-135
ManagesFrequenciesTrait providing fluent API for building cron expressions (daily, hourly, etc.).src/Scheduling/Event.php39
ManagesAttributesTrait providing configuration methods (environments, maintenance mode, user context).src/Scheduling/Event.php38
EventMutexContract for preventing overlapping executions of the same event.src/Scheduling/Event.php21
SchedulingMutexContract for single-server execution coordination.src/Scheduling/Schedule.php18

Sources: src/Scheduling/Schedule.php32-118 src/Scheduling/Event.php35-104

Task Registration Flow

Creating Scheduled Tasks

The Schedule class provides factory methods for registering different types of tasks:


Sources: src/Scheduling/Schedule.php123-153 src/Scheduling/Schedule.php273-286

Task Types

The Schedule class supports four primary task types:

MethodTask TypeDescriptionExample
call()ClosureExecutes a PHP closure$schedule->call(fn() => cleanup())->hourly()
command()Console CommandRuns a registered console command$schedule->command('emails:send')->daily()
job()Queue JobDispatches a job to the queue$schedule->job(new ProcessPodcast)->everyFiveMinutes()
exec()System CommandExecutes a shell command$schedule->exec('node script.js')->everyMinute()

Sources: src/Scheduling/Schedule.php123-178 src/Scheduling/Schedule.php239-250

Event Lifecycle and Execution

Eligibility Determination

Before an event executes, it passes through multiple gates to determine eligibility:


Sources: src/Scheduling/Event.php247-309 src/Scheduling/Event.php129-133 src/Scheduling/Schedule.php331-334

Execution Process

Once an event is eligible, it follows this execution sequence:


Sources: src/Scheduling/Event.php111-124 src/Scheduling/Event.php156-196 src/Scheduling/Event.php213-222

Code-to-Concept Mapping

Key Classes and Their Roles


Sources: src/Scheduling/Schedule.php123-250 src/Scheduling/Event.php35-738

Property and State Management

The Event class maintains several key properties that control execution:

PropertyTypePurposeSet By
command?stringCommand or closure identifierConstructor src/Scheduling/Event.php98
expressionstringCron expression defining scheduleManagesFrequencies trait methods
mutexEventMutexMutex implementation for overlap preventionConstructor src/Scheduling/Event.php97
withoutOverlappingboolWhether to prevent overlapping executionswithoutOverlapping() method
onOneServerboolWhether to run on single server onlyonOneServer() method
beforeCallbacksarrayCallbacks to run before executionbefore(), pingBefore() src/Scheduling/Event.php56
afterCallbacksarrayCallbacks to run after executionafter(), then() src/Scheduling/Event.php61
exitCode?intExit status of last executionSet during finish() src/Scheduling/Event.php78
output?stringOutput file pathsendOutputTo() src/Scheduling/Event.php46
lastChecked?CarbonLast eligibility check timeSet during filtersPass() src/Scheduling/Event.php73

Sources: src/Scheduling/Event.php43-88

Integration Points

Container and Dependency Resolution

The scheduling system integrates deeply with the dependency injection container:


Sources: src/Scheduling/Schedule.php100-118 src/Scheduling/Event.php172-180 src/Scheduling/Event.php227-242

External System Integration

Events can integrate with external systems through callbacks:

IntegrationMethodDescription
HTTP WebhookspingBefore(), thenPing(), pingOnSuccess(), pingOnFailure()Send HTTP GET requests to URLs src/Scheduling/Event.php458-517
Email NotificationsemailOutputTo(), emailOutputOnFailure()Send output via email src/Scheduling/Event.php377-414
Queue SystemSchedule::job()Dispatch jobs to queue workers src/Scheduling/Schedule.php158-178
File SystemsendOutputTo(), appendOutputTo()Write output to log files src/Scheduling/Event.php324-370

Sources: src/Scheduling/Event.php377-547

Use Cases

Common Scheduling Patterns

The task scheduling system supports various common patterns:

1. Database Maintenance


2. Cache Warming


3. Report Generation


4. System Health Checks


5. Multi-Server Safe Tasks


Sources: src/Scheduling/Schedule.php123-250 src/Scheduling/Event.php1-738

System Dependencies

The scheduling system requires and integrates with several components:


Sources: src/Scheduling/Schedule.php1-28 src/Scheduling/Event.php1-34