Task Scheduler
Nexphant provides an integrated in-process task scheduler. Unlike traditional frameworks that rely on system cron jobs firing every minute to execute a CLI command, Nexphant runs scheduled tasks directly inside the event loop of your persistent runtime.
Defining Schedules
Scheduled tasks are defined in your application boot code. Since the server is long-running, the scheduler tracks time-based tasks with millisecond precision without spawning new OS processes.
use Nexphant\App;
use Nexphant\Scheduler\Schedule;
$app = App::create();
$app->schedule(function (Schedule $schedule) {
// Run a closure every minute
$schedule->call(function () {
logger()->info('Heartbeat tick');
})->everyMinute();
// Run a class-based Job every hour
$schedule->job(new \App\Jobs\CleanupDatabase())->hourly();
// Run a CLI command daily
$schedule->command('cache:clear')->dailyAt('02:00');
});Schedule Frequencies
You can customize execution frequencies using clean, readable fluents:
| Fluent Method | Description |
|---|---|
->everySecond() | Run every second |
->everyFiveSeconds() | Run every 5 seconds |
->everyMinute() | Run every minute |
->everyFiveMinutes() | Run every 5 minutes |
->hourly() | Run once an hour |
->daily() | Run daily at midnight |
->dailyAt('13:00') | Run daily at 1:00 PM |
->weekly() | Run once a week |
->cron('*/15 * * * *') | Run based on a standard cron expression |
Overlap Prevention
By default, scheduled tasks can overlap. If a task takes longer to run than its frequency, a new instance of the task will be scheduled anyway. To prevent this, use the withoutOverlapping method:
$schedule->call(function () {
// Long database migration task
})->everyFiveMinutes()->withoutOverlapping();This locks the task execution within the memory context of the runtime, releasing the lock automatically when the task finishes.
Task Cancellation & Deadlines
All scheduled tasks run inside isolated fibers. If a task exceeds its allocated duration, you can configure it to terminate cleanly:
$schedule->job(new SyncExternalAPI())
->hourly()
->timeout(300); // stops the fiber if it runs for more than 5 minutes