From 1145217f0a6aa4a7b89dadbe85ef5d0c29d29068 Mon Sep 17 00:00:00 2001 From: overtrue Date: Fri, 26 Apr 2024 17:51:33 +0800 Subject: [PATCH] wip: cli --- src/Watchers/CommandWatcher.php | 33 ++++++++++++++++++++ src/Watchers/ScheduledTaskWatcher.php | 43 +++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/Watchers/CommandWatcher.php create mode 100644 src/Watchers/ScheduledTaskWatcher.php diff --git a/src/Watchers/CommandWatcher.php b/src/Watchers/CommandWatcher.php new file mode 100644 index 0000000..a711a79 --- /dev/null +++ b/src/Watchers/CommandWatcher.php @@ -0,0 +1,33 @@ +listen(CommandStarting::class, function (CommandStarting $event) { + $this->span = Measure::span('[Command] '.$event->command) + ->setAttributes([ + 'command' => $event->command, + 'arguments' => $event->input->getArguments(), + 'options' => $event->input->getOptions(), + ]) + ->start(); + $this->span->storeInContext(Context::getCurrent()); + }); + + $app['events']->listen(CommandFinished::class, function (CommandFinished $event) { + $this->span->end(); + }); + } +} diff --git a/src/Watchers/ScheduledTaskWatcher.php b/src/Watchers/ScheduledTaskWatcher.php new file mode 100644 index 0000000..eb7dd9c --- /dev/null +++ b/src/Watchers/ScheduledTaskWatcher.php @@ -0,0 +1,43 @@ +listen(ScheduledTaskStarting::class, function (ScheduledTaskStarting $event) { + $this->span = Measure::span('[Schedule] '.$event->task->getSummaryForDisplay()) + ->setAttributes([ + 'task.command' => $event->task->command, + 'task.description' => $event->task->description, + 'task.expression' => $event->task->expression, + 'task.exitCode' => $event->task->exitCode, + ]) + ->start(); + $this->span->storeInContext(Context::getCurrent()); + }); + + $app['events']->listen(ScheduledTaskFinished::class, function (ScheduledTaskFinished $event) { + $this->span->end(); + }); + + $app['events']->listen(ScheduledTaskFailed::class, function (ScheduledTaskFailed $event) { + $this->span->setAttribute('task.error', $event->exception->getMessage()); + $this->span->setStatus(StatusCode::STATUS_ERROR); + $this->span->recordException($event->exception); + $this->span->end(); + }); + } +}