Skip to content

Commit

Permalink
Merge pull request #56 from lion-packages/refactoring
Browse files Browse the repository at this point in the history
Refactoring for queued tasks
  • Loading branch information
Santiago1010 authored Apr 29, 2024
2 parents b5ce2b5 + 180d35f commit 20a4893
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
2 changes: 1 addition & 1 deletion config/queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,11 +115,16 @@
Route::addMiddleware(Routes::getMiddleware());
// -----------------------------------------------------------------------------
Route::get('/', function () {
// TaskQueue::push('send:email:verify', json([
// 'email' => 'sleon@dev.com',
// 'template' => '<h1>Tasks Test</h1>'
// ]));

return info('[index]');
});

Route::get('logger', function () {
logger('test-logger', 'info', ['user' => 'Sleon'], true);
logger('test-logger', LogTypeEnum::INFO, ['user' => 'Sleon'], true);

return success();
});
Expand Down
38 changes: 20 additions & 18 deletions src/LionBundle/Commands/Lion/Schedule/RunQueuedTasksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

/**
Expand Down Expand Up @@ -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;
}
}
}

Expand Down
23 changes: 19 additions & 4 deletions src/LionBundle/Helpers/Bundle/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,34 @@ 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 {
$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(json_encode($message), $data);
$logger->$logTypeValue(json_encode($message), $data);
} else {
$logger->$logType(json_encode(['uri' => $_SERVER['REQUEST_URI'], 'data' => json_decode($message)]), $data);
$logger->$logTypeValue(
json_encode([
'uri' => $_SERVER['REQUEST_URI'],
'data' => json_decode($message)
]),
$data
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Helpers/Bundle/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 20a4893

Please sign in to comment.