VOOZH about

URL: https://deepwiki.com/hypervel/components/4.2-task-scheduling

⇱ Task Scheduling | hypervel/components | DeepWiki


Loading...
Last indexed: 7 March 2026 (96fbab)
Menu

Task Scheduling

Purpose and Scope

This document explains the task scheduling system in Hypervel Components, which provides a programmatic interface for defining recurring tasks (similar to cron jobs). The scheduler integrates with the Console Kernel to manage command execution on a schedule using cron expression syntax and frequency helpers.

For information about console command registration and discovery, see Console Kernel and Command Discovery. For general service layer management patterns, see Service Layer.


Architecture Overview

The scheduling system is built around the Hypervel\Console\Scheduling\Schedule class, which is integrated into the Console Kernel lifecycle. Developers define their application's scheduled tasks by overriding the schedule() method in their kernel class.

Core Components

ComponentClass/InterfacePurpose
KernelHypervel\Foundation\Console\KernelManages schedule lifecycle and configuration
ScheduleHypervel\Console\Scheduling\ScheduleDefines and stores scheduled tasks
Kernel ContractHypervel\Foundation\Console\Contracts\KernelDefines schedule() interface
EventsHyperf\Framework\Event\BootApplicationTriggers schedule initialization

Schedule Lifecycle Flow


Sources: src/foundation/src/Console/Kernel.php77-90 src/foundation/src/Console/Kernel.php257-281 src/foundation/src/Console/Contracts/Kernel.php23

Schedule Factory Pattern

The schedule is created using a factory pattern bound to the container at src/foundation/src/Console/Kernel.php276-280:


This pattern ensures that every resolution of Schedule from the container receives:

  1. Correct timezone configuration via scheduleTimezone()
  2. Correct cache store via scheduleCache()
  3. Application-defined tasks via schedule() method

Sources: src/foundation/src/Console/Kernel.php77-90 src/foundation/src/Console/Kernel.php257-281


Schedule Definition in Console Kernel

The Console Kernel provides the schedule() method as the primary entry point for defining scheduled tasks. This method receives a fully configured Schedule instance and should be overridden by the application's kernel.

Schedule Method Hierarchy











































MethodSignatureLocationPurpose
schedule()schedule(Schedule $schedule): voidsrc/foundation/src/Console/Kernel.php257-259Template method for defining application schedules
resolveConsoleSchedule()resolveConsoleSchedule(): Schedulesrc/foundation/src/Console/Kernel.php264-269Creates and configures a Schedule instance on-demand
defineConsoleSchedule()defineConsoleSchedule(): voidsrc/foundation/src/Console/Kernel.php274-281Binds Schedule to container during application boot
scheduleTimezone()scheduleTimezone(): ?stringsrc/foundation/src/Console/Kernel.php286-291Resolves timezone for scheduled tasks
scheduleCache()scheduleCache(): ?stringsrc/foundation/src/Console/Kernel.php296-299Resolves cache store for mutex management

Implementation Pattern

The schedule() method is a template method following the Template Method design pattern. The base implementation at src/foundation/src/Console/Kernel.php257-259 is intentionally empty:


Applications extend Hypervel\Foundation\Console\Kernel and override this method:


The schedule() method is called automatically during Schedule resolution at src/foundation/src/Console/Kernel.php267 and src/foundation/src/Console/Kernel.php278

Sources: src/foundation/src/Console/Kernel.php257-259 src/foundation/src/Console/Kernel.php264-269 src/foundation/src/Console/Kernel.php274-281 src/foundation/src/Console/Contracts/Kernel.php23


Schedule Configuration

The scheduling system is configured through two primary mechanisms: timezone settings and cache store configuration for mutex management.

Timezone Configuration

The scheduler uses timezone configuration to determine when tasks should execute. The timezone resolution follows this priority chain:


Implementation: src/foundation/src/Console/Kernel.php286-291

The scheduleTimezone() method implementation:


This allows applications to use a different timezone for scheduled tasks than the general application timezone, which is useful for:

  • Applications serving multiple time zones
  • Centralizing task execution times regardless of server location
  • Separating business logic timezone from infrastructure timezone

Cache Store for Mutexes

Scheduled tasks use a cache store to implement mutexes (mutual exclusion locks), which prevent overlapping executions of the same task. The cache store is resolved through:


Implementation: src/foundation/src/Console/Kernel.php296-299

The scheduleCache() method implementation:


Mutex Usage Pattern

Mutexes prevent race conditions when multiple scheduler instances might attempt to run the same task simultaneously. The cache-based mutex works as follows:

  1. Before task execution, acquire lock in cache with task identifier
  2. If lock exists, skip execution (task already running)
  3. Execute task if lock acquired successfully
  4. Release lock after task completes or times out

The cache store choice impacts mutex behavior:

Cache DriverMutex CharacteristicsUse Case
redisDistributed, persistentMulti-server deployments
fileLocal, persistentSingle-server deployments
arrayIn-memory, non-persistentTesting, single-process

Sources: src/foundation/src/Console/Kernel.php286-299


Container Integration

The Schedule instance is bound to the service container during the application boot process, making it available for dependency injection throughout the application.

Schedule Binding Process


The binding occurs in two distinct stages:

Stage 1: Deferred Binding Setup src/foundation/src/Console/Kernel.php87-89


  • Kernel constructor registers callback via app->booted()
  • Ensures Schedule binding happens after all providers boot
  • Delays schedule definition until application is fully initialized

Stage 2: Schedule Factory Registration src/foundation/src/Console/Kernel.php276-280


The factory closure:

  1. Creates new Schedule($this->scheduleTimezone())
  2. Calls $schedule->useCache($this->scheduleCache())
  3. Invokes $this->schedule($schedule) for task definition
  4. Returns fully configured Schedule instance

Container Resolution Methods

The Schedule instance can be resolved through multiple approaches:

Resolution MethodCode ExampleUse Case
Type Hintingpublic function __construct(Schedule $schedule)Dependency injection in classes
Container Direct$app->make(Schedule::class)Manual resolution
Kernel Method$kernel->resolveConsoleSchedule()Creating ad-hoc Schedule instances
Array Access$app[Schedule::class]Alternative container access

resolveConsoleSchedule() Method

The resolveConsoleSchedule() method at src/foundation/src/Console/Kernel.php264-269 provides direct Schedule creation without container:


This method:

  • Creates a fresh Schedule instance each call
  • Does not use the container binding
  • Useful for testing or creating temporary schedules
  • Applies same configuration (timezone, cache, tasks) as container-bound instance

Key Difference: Container-bound Schedule is singleton per resolution cycle, while resolveConsoleSchedule() always creates new instance.

Sources: src/foundation/src/Console/Kernel.php77-90 src/foundation/src/Console/Kernel.php264-281


Integration Points with Console Kernel

The scheduling system integrates with the Console Kernel at multiple lifecycle points, ensuring proper initialization and configuration.

Kernel Lifecycle Integration


Bootstrap Sequence

The Console Kernel defines its bootstrap sequence at src/foundation/src/Console/Kernel.php71-75:


The Schedule definition occurs after all bootstrappers have executed, ensuring:

  • All service providers are registered
  • All service providers are booted
  • Application configuration is fully loaded
  • Cache drivers are available for mutex storage

Kernel Method Summary

MethodVisibilityLine ReferencePurpose
__construct()public77-90Initialize kernel, register schedule callback
schedule()public257-259Template method for task definition
resolveConsoleSchedule()public264-269Create configured Schedule instance
defineConsoleSchedule()protected274-281Bind Schedule to container
scheduleTimezone()protected286-291Resolve timezone configuration
scheduleCache()protected296-299Resolve cache store configuration

Sources: src/foundation/src/Console/Kernel.php71-90 src/foundation/src/Console/Kernel.php257-299


Configuration Reference

Application Configuration

The scheduling system reads configuration from the application's config/app.php file:

Configuration KeyTypeDefaultPurpose
app.schedule_timezone`stringnull`null
app.timezonestringSystem defaultApplication-wide timezone, used as fallback

Cache Configuration

Mutex storage configuration is read from config/cache.php:

Configuration KeyTypeDefaultPurpose
cache.schedule_store`stringnull`null

Environment Variables

The scheduler supports the following environment variable:

VariableTypePurpose
SCHEDULE_CACHE_DRIVER`stringnull`

Sources: src/foundation/src/Console/Kernel.php286-299


Extension and Customization

Applications can customize the scheduling system by extending the Console Kernel and overriding specific methods. The kernel provides several template methods designed for customization.

Customization Points


Common Customization Patterns

1. Environment-Based Timezone

Different environments may require different task execution times:


2. Environment-Based Cache Store

Different environments may need different mutex strategies:


3. Dynamic Schedule Definition

Conditional task registration based on configuration:


4. Integrating Scheduled Tasks with Commands

The kernel's commands() method can register commands that are then scheduled:


Advanced Customization: Custom Schedule Resolution

For complete control over Schedule creation, override resolveConsoleSchedule():


Sources: src/foundation/src/Console/Kernel.php257-259 src/foundation/src/Console/Kernel.php264-269 src/foundation/src/Console/Kernel.php286-299 src/foundation/src/Console/Kernel.php304-306


Related Systems

The task scheduling system integrates with several other Hypervel subsystems:

SystemRelationshipReference Page
Console KernelParent system managing command lifecycle4.1
Cache SystemProvides mutex storage for overlap prevention5.1
Service ContainerManages Schedule instance lifecycle2.1
Configuration ManagementProvides timezone and cache settings2.3

Sources: src/foundation/src/Console/Kernel.php1-438

Refresh this wiki

On this page