bootstrapWorker(); $scheduler = $container->get(SchedulerService::class); echo "โœ… Framework bootstrapped successfully\n"; echo "โœ… SchedulerService retrieved\n\n"; // Test 1: Schedule a task with cron expression echo "๐Ÿ“‹ Test 1: Scheduling a cron task (every minute)\n"; echo "------------------------------------------------\n"; $cronSchedule = CronSchedule::fromExpression('* * * * *'); // Every minute $scheduler->schedule('cleanup-task', $cronSchedule, function () { echo "๐Ÿงน Running cleanup task at " . date('Y-m-d H:i:s') . "\n"; return ['status' => 'cleaned', 'files_removed' => 42]; }); echo "โœ… Cron task scheduled successfully\n\n"; // Test 2: Schedule an interval task echo "๐Ÿ“‹ Test 2: Scheduling an interval task (every 30 seconds)\n"; echo "---------------------------------------------------------\n"; $intervalSchedule = IntervalSchedule::every(Duration::fromSeconds(30)); $scheduler->schedule('heartbeat-task', $intervalSchedule, function () { echo "๐Ÿ’“ Heartbeat at " . date('Y-m-d H:i:s') . "\n"; return ['status' => 'alive', 'timestamp' => time()]; }); echo "โœ… Interval task scheduled successfully\n\n"; // Test 3: Schedule a one-time task echo "๐Ÿ“‹ Test 3: Scheduling a one-time task (in 5 seconds)\n"; echo "----------------------------------------------------\n"; $oneTimeSchedule = OneTimeSchedule::at(Timestamp::fromFloat(time() + 5)); $scheduler->schedule('welcome-task', $oneTimeSchedule, function () { echo "๐Ÿ‘‹ Welcome task executed at " . date('Y-m-d H:i:s') . "\n"; return ['status' => 'welcomed', 'message' => 'Hello World!']; }); echo "โœ… One-time task scheduled successfully\n\n"; // Test 4: Check scheduled tasks echo "๐Ÿ“‹ Test 4: Checking scheduled tasks\n"; echo "-----------------------------------\n"; $tasks = $scheduler->getScheduledTasks(); echo "๐Ÿ“Š Total scheduled tasks: " . count($tasks) . "\n"; foreach ($tasks as $taskId => $task) { echo " โ€ข Task: $taskId\n"; echo " Type: " . $task->schedule->getType() . "\n"; echo " Description: " . $task->schedule->getDescription() . "\n"; echo " Next execution: " . ($task->nextExecution ? $task->nextExecution->format('c') : 'N/A') . "\n"; echo "\n"; } // Test 5: Check due tasks echo "๐Ÿ“‹ Test 5: Checking due tasks\n"; echo "-----------------------------\n"; $dueTasks = $scheduler->getDueTasks(); echo "๐Ÿ“Š Due tasks: " . count($dueTasks) . "\n"; if (count($dueTasks) > 0) { foreach ($dueTasks as $task) { echo " โ€ข Due task: {$task->id}\n"; } } else { echo " โ„น๏ธ No tasks are due for execution right now\n"; } echo "\n"; // Test 6: Get scheduler statistics echo "๐Ÿ“‹ Test 6: Scheduler statistics\n"; echo "-------------------------------\n"; $stats = $scheduler->getStats(); echo "๐Ÿ“Š Scheduler Statistics:\n"; echo " โ€ข Total tasks: {$stats['total_tasks']}\n"; echo " โ€ข Due tasks: {$stats['due_tasks']}\n"; echo " โ€ข Next execution: " . ($stats['next_execution'] ?? 'N/A') . "\n"; echo " โ€ข Schedule types:\n"; foreach ($stats['schedule_types'] as $type => $count) { echo " - $type: $count\n"; } echo "\n"; // Test 7: Execute due tasks (if any) echo "๐Ÿ“‹ Test 7: Executing due tasks\n"; echo "------------------------------\n"; $results = $scheduler->executeDueTasks(); if (count($results) > 0) { echo "โœ… Executed " . count($results) . " tasks:\n"; foreach ($results as $result) { echo " โ€ข Task: {$result->taskId}\n"; echo " Success: " . ($result->success ? 'Yes' : 'No') . "\n"; echo " Execution time: {$result->executionTimeSeconds}s\n"; if ($result->result) { echo " Result: " . json_encode($result->result) . "\n"; } if ($result->error) { echo " Error: " . $result->error->getMessage() . "\n"; } echo "\n"; } } else { echo "โ„น๏ธ No tasks were due for execution\n\n"; } echo "๐Ÿ“Š Test Results:\n"; echo "===============\n"; echo "โœ… Scheduler system is working correctly\n"; echo "โœ… Different schedule types can be created\n"; echo "โœ… Tasks can be scheduled and tracked\n"; echo "โœ… Due task detection works\n"; echo "โœ… Task execution system functional\n"; echo "โœ… Statistics and monitoring available\n"; } catch (Exception $e) { echo "โŒ Test failed: " . $e->getMessage() . "\n"; echo "Stack trace:\n" . $e->getTraceAsString() . "\n"; exit(1); } echo "\n๐ŸŽ‰ Scheduler system test completed successfully!\n";