Skip to content

Commit

Permalink
Made task runner via events attempt to update logs every 10secs.
Browse files Browse the repository at this point in the history
  • Loading branch information
arabcoders committed Aug 20, 2024
1 parent 9a9cd36 commit 6be04bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 0 additions & 1 deletion frontend/pages/events/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
<span class="card-footer-item">
<template v-if="!item.updated_at">
<span v-if="0 === item.status" class="icon">
{{ item.status }}
<i class="fas fa-spinner fa-spin"></i>
</span>
<span v-else>None</span>
Expand Down
21 changes: 19 additions & 2 deletions src/Commands/System/TasksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Libs\Extends\ConsoleOutput;
use App\Libs\Stream;
use App\Model\Events\EventListener;
use App\Model\Events\EventsRepository;
use Closure;
use Cron\CronExpression;
use Exception;
Expand Down Expand Up @@ -45,7 +46,7 @@ final class TasksCommand extends Command
/**
* Class Constructor.
*/
public function __construct()
public function __construct(private EventsRepository $eventsRepo)
{
set_time_limit(0);
ini_set('memory_limit', '-1');
Expand Down Expand Up @@ -188,7 +189,23 @@ public function runEventTask(DataEvent $event): DataEvent
$input->setOption('save-log', true);
$input->setOption('live', false);

$this->writer = fn($msg) => $event->addLog($msg);
$this->writer = function ($msg) use (&$event) {
static $lastSave = null;

$timeNow = hrtime(as_number: true);

if (null === $lastSave) {
$lastSave = $timeNow;
}

$event->addLog($msg);

if ($timeNow > $lastSave) {
$this->eventsRepo->save($event->getEvent());
$lastSave = $timeNow + (10 * 1_000_000_000);
}
};

$event->addLog(r("Task: Run '{command}'.", ['command' => ag($task, 'command')]));
$exitCode = $this->runTask($task, $input, Container::get(iOutput::class));
$event->addLog(r("Task: End '{command}' (Exit Code: {code})", [
Expand Down

0 comments on commit 6be04bb

Please sign in to comment.