From 25355b343ae0a5af2c34080b392a61e315482579 Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Mon, 29 Apr 2024 11:22:30 -0500 Subject: [PATCH 1/2] refactor: the execution of queued tasks by status has been validated --- config/queue.php | 2 +- .../Lion/Schedule/RunQueuedTasksCommand.php | 38 ++++++++++--------- src/LionBundle/Helpers/Bundle/helpers.php | 6 +-- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/config/queue.php b/config/queue.php index 2afdbc40..553442fe 100644 --- a/config/queue.php +++ b/config/queue.php @@ -42,7 +42,7 @@ function (object $queue): void { } catch (Exception $e) { TaskQueue::edit($queue, TaskStatusEnum::FAILED); - logger($e->getMessage(), LogTypeEnum::ERROR->value, [ + logger($e->getMessage(), LogTypeEnum::ERROR, [ 'idtask_queue' => $queue->idtask_queue, 'task_queue_type' => $queue->task_queue_type, 'task_queue_data' => $queue->task_queue_data diff --git a/src/LionBundle/Commands/Lion/Schedule/RunQueuedTasksCommand.php b/src/LionBundle/Commands/Lion/Schedule/RunQueuedTasksCommand.php index f73eb9b8..19821d53 100644 --- a/src/LionBundle/Commands/Lion/Schedule/RunQueuedTasksCommand.php +++ b/src/LionBundle/Commands/Lion/Schedule/RunQueuedTasksCommand.php @@ -27,7 +27,7 @@ protected function configure(): void { $this ->setName('schedule:run') - ->setDescription('Query and execute queued tasks in the background'); + ->setDescription('Run queued tasks.'); } /** @@ -67,39 +67,41 @@ protected function execute(InputInterface $input, OutputInterface $output): int foreach ($data as $queue) { $data = (object) json_decode($queue->task_queue_data, true); - $output->writeln($this->warningOutput("\t>> SCHEDULE: {$queue->task_queue_type}")); + $output->writeln($this->warningOutput("\t>> SCHEDULE: {$queue->task_queue_type} [PROCESSING]")); - $output->writeln($this->successOutput("\t>> SCHEDULE: {$data->template} [PROCESSING]")); + if (TaskStatusEnum::PENDING->value === $queue->task_queue_status) { + $output->writeln($this->successOutput("\t>> SCHEDULE: {$queue->task_queue_type} [IN-PROGRESS]")); - if (TaskStatusEnum::COMPLETED->value === $queue->task_queue_status) { - $output->writeln($this->successOutput("\t>> SCHEDULE: {$data->template} [COMPLETED]")); - - $output->writeln($this->successOutput("\t>> SCHEDULE: {$data->template} [REMOVED]")); - - TaskQueue::remove($queue); + TaskQueue::edit($queue, TaskStatusEnum::IN_PROGRESS); TaskQueue::pause(1); continue; } - TaskQueue::edit($queue, TaskStatusEnum::IN_PROGRESS); + if (TaskStatusEnum::IN_PROGRESS->value === $queue->task_queue_status) { + $callable = TaskQueue::get($queue->task_queue_type); - $output->writeln($this->successOutput("\t>> SCHEDULE: {$data->template} [IN-PROGRESS]")); + $callable($queue); - TaskQueue::pause(1); + $output->writeln($this->successOutput("\t>> SCHEDULE: {$queue->task_queue_type} [COMPLETED]")); - $callable = TaskQueue::get($queue->task_queue_type); + TaskQueue::edit($queue, TaskStatusEnum::COMPLETED); - $callable($queue); + TaskQueue::pause(1); - TaskQueue::pause(1); + continue; + } - TaskQueue::edit($queue, TaskStatusEnum::COMPLETED); + if (TaskStatusEnum::COMPLETED->value === $queue->task_queue_status) { + $output->writeln($this->successOutput("\t>> SCHEDULE: {$queue->task_queue_type} [REMOVED]")); + + TaskQueue::remove($queue); - $output->writeln($this->successOutput("\t>> SCHEDULE: {$data->template} [COMPLETED]")); + TaskQueue::pause(1); - TaskQueue::pause(1); + continue; + } } } diff --git a/src/LionBundle/Helpers/Bundle/helpers.php b/src/LionBundle/Helpers/Bundle/helpers.php index d61ad6c1..d39f046e 100644 --- a/src/LionBundle/Helpers/Bundle/helpers.php +++ b/src/LionBundle/Helpers/Bundle/helpers.php @@ -189,7 +189,7 @@ function vd(mixed $response): void * * @return void */ - function logger(string $message, string $logType = LogTypeEnum::INFO, array $data = [], bool $index = true): void + function logger(string $message, LogTypeEnum $logType = LogTypeEnum::INFO, array $data = [], bool $index = true): void { $path = storage_path('logs/monolog/', $index); $fileName = "{$path}lion-" . Carbon::now()->format('Y-m-d') . '.log'; @@ -199,9 +199,9 @@ function logger(string $message, string $logType = LogTypeEnum::INFO, array $dat $logger->pushHandler(new StreamHandler($fileName, Level::Info)); if (!isset($_SERVER['REQUEST_URI'])) { - $logger->$logType(json_encode($message), $data); + $logger->$logType->value(json_encode($message), $data); } else { - $logger->$logType(json_encode(['uri' => $_SERVER['REQUEST_URI'], 'data' => json_decode($message)]), $data); + $logger->$logType->value(json_encode(['uri' => $_SERVER['REQUEST_URI'], 'data' => json_decode($message)]), $data); } } } From 180d35f3409d25067d555f41567feaa08fb3a5d1 Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Mon, 29 Apr 2024 11:23:02 -0500 Subject: [PATCH 2/2] refactor: data type has been modified for the logger helper --- routes/web.php | 9 ++++++++- src/LionBundle/Helpers/Bundle/helpers.php | 23 +++++++++++++++++++---- tests/Helpers/Bundle/HelpersTest.php | 2 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/routes/web.php b/routes/web.php index 3927dd21..9c1b7a8b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -17,6 +17,8 @@ use Lion\Route\Route; use Dotenv\Dotenv; +use Lion\Bundle\Enums\LogTypeEnum; +use Lion\Bundle\Helpers\Commands\Schedule\TaskQueue; use Lion\Bundle\Helpers\ExceptionCore; use Lion\Bundle\Helpers\Http\Routes; use Lion\Database\Driver; @@ -113,11 +115,16 @@ Route::addMiddleware(Routes::getMiddleware()); // ----------------------------------------------------------------------------- Route::get('/', function () { + // TaskQueue::push('send:email:verify', json([ + // 'email' => 'sleon@dev.com', + // 'template' => '

Tasks Test

' + // ])); + return info('[index]'); }); Route::get('logger', function () { - logger('test-logger', 'info', ['user' => 'Sleon'], true); + logger('test-logger', LogTypeEnum::INFO, ['user' => 'Sleon'], true); return success(); }); diff --git a/src/LionBundle/Helpers/Bundle/helpers.php b/src/LionBundle/Helpers/Bundle/helpers.php index d39f046e..f5ae5b02 100644 --- a/src/LionBundle/Helpers/Bundle/helpers.php +++ b/src/LionBundle/Helpers/Bundle/helpers.php @@ -189,19 +189,34 @@ function vd(mixed $response): void * * @return void */ - function logger(string $message, LogTypeEnum $logType = LogTypeEnum::INFO, array $data = [], bool $index = true): void - { + function logger( + string $message, + LogTypeEnum $logType = LogTypeEnum::INFO, + array $data = [], + bool $index = true + ): void { + $logTypeValue = $logType->value; + $path = storage_path('logs/monolog/', $index); + $fileName = "{$path}lion-" . Carbon::now()->format('Y-m-d') . '.log'; + (new Store())->folder($path); $logger = new Logger('log'); + $logger->pushHandler(new StreamHandler($fileName, Level::Info)); if (!isset($_SERVER['REQUEST_URI'])) { - $logger->$logType->value(json_encode($message), $data); + $logger->$logTypeValue(json_encode($message), $data); } else { - $logger->$logType->value(json_encode(['uri' => $_SERVER['REQUEST_URI'], 'data' => json_decode($message)]), $data); + $logger->$logTypeValue( + json_encode([ + 'uri' => $_SERVER['REQUEST_URI'], + 'data' => json_decode($message) + ]), + $data + ); } } } diff --git a/tests/Helpers/Bundle/HelpersTest.php b/tests/Helpers/Bundle/HelpersTest.php index 4d543bca..4dbb4747 100644 --- a/tests/Helpers/Bundle/HelpersTest.php +++ b/tests/Helpers/Bundle/HelpersTest.php @@ -143,7 +143,7 @@ public function testLogger(): void $fileName = "{$path}lion-" . Carbon::now()->format('Y-m-d') . '.log'; - logger(self::LOGGER_CONTENT, LogTypeEnum::INFO->value, ['user' => 'Sleon'], false); + logger(self::LOGGER_CONTENT, LogTypeEnum::INFO, ['user' => 'Sleon'], false); $this->assertFileExists($fileName); }