Add command output to MonitoredScheduledTask model
Gummibeer opened this issue ยท 6 comments
It would be nice to be able to see the scheduled command output in the MonitoredScheduledTask
model table.
It should only be logged when the user calls storeOutput()
on the scheduled task.
I can/will PR a solution for this. The issue is for completeness. ๐
Came here looking for this exact feature. I run nightly commands to fetch a bunch of data from third-party services, and they save properly and improperly for a whole bunch of reasons. Ideally, I'd love to be able to keep a record of the output of these commands where am logging info about why they may or may not have been saved.
That's what I got back in the days. So far I remember there was a problem to test it - why I haven't pushed it.
Feel free to take it and finish it.
Index: src/Models/MonitoredScheduledTask.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Models/MonitoredScheduledTask.php b/src/Models/MonitoredScheduledTask.php
--- a/src/Models/MonitoredScheduledTask.php (revision 40964a420d0207780beb9f0667f9ce5d9c25d82a)
+++ b/src/Models/MonitoredScheduledTask.php (date 1604790284900)
@@ -94,6 +94,7 @@
'runtime' => $event->task->runInBackground ? null : $event->runtime,
'exit_code' => $event->task->exitCode,
'memory' => $event->task->runInBackground ? null : memory_get_usage(true),
+ 'output' => $this->getEventTaskOutput($event),
]);
$this->update(['last_finished_at' => now()]);
@@ -175,4 +176,28 @@
'type' => $type,
]);
}
+
+ /**
+ * @param ScheduledTaskFailed|ScheduledTaskFinished $event
+ *
+ * @return string|null
+ */
+ protected function getEventTaskOutput($event): ?string
+ {
+ if ($event->task->output === null) {
+ return null;
+ }
+
+ if ($event->task->output === $event->task->getDefaultOutput()) {
+ return null;
+ }
+
+ if (! is_file($event->task->output)) {
+ return null;
+ }
+
+ $output = file_get_contents($event->task->output);
+
+ return $output ?: null;
+ }
}
Index: tests/ScheduledTaskSubscriber/ScheduledTaskSubscriberTest.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/tests/ScheduledTaskSubscriber/ScheduledTaskSubscriberTest.php b/tests/ScheduledTaskSubscriber/ScheduledTaskSubscriberTest.php
--- a/tests/ScheduledTaskSubscriber/ScheduledTaskSubscriberTest.php (revision 40964a420d0207780beb9f0667f9ce5d9c25d82a)
+++ b/tests/ScheduledTaskSubscriber/ScheduledTaskSubscriberTest.php (date 1604790359331)
@@ -3,6 +3,7 @@
namespace Spatie\ScheduleMonitor\Tests\ScheduledTaskSubscriber;
use Exception;
+use Illuminate\Console\OutputStyle;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\Bus;
use Spatie\ScheduleMonitor\Commands\SyncCommand;
@@ -13,6 +14,9 @@
use Spatie\ScheduleMonitor\Tests\TestClasses\FailingCommand;
use Spatie\ScheduleMonitor\Tests\TestClasses\TestKernel;
use Spatie\TestTime\TestTime;
+use Symfony\Component\Console\Command\ListCommand;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Output\ConsoleOutput;
class ScheduledTaskSubscriberTest extends TestCase
{
@@ -153,4 +157,22 @@
Bus::assertDispatched(PingOhDearJob::class);
}
+
+ /** @test */
+ public function it_will_log_command_output()
+ {
+ TestKernel::replaceScheduledTasks(function (Schedule $schedule) {
+ $schedule->command('list')
+ ->storeOutput()
+ ->everyMinute()
+ ->monitorName('dummy-task');
+ });
+
+ $this->artisan(SyncCommand::class);
+ $this->artisan('schedule:run');
+
+ $task = MonitoredScheduledTask::findByName('dummy-task');
+ $logItem = $task->logItems()->where('type', MonitoredScheduledTaskLogItem::TYPE_FINISHED)->first();
+ dump($logItem->meta);
+ }
}
I can/will PR a solution for this. The issue is for completeness.
Still welcome a PR for this!
Tom, if you don't have time for this, maybe @patinthehat could be interested in adding this?
Yes, I've pasted my code - so far I remember the code itself worked (app tested) but I wasn't able to get a passing & useful Testcase.
In our project we found a hack around this as a package solution (because of test) wasn't doable in time.
I would be very happy if this gets into this package! ๐
But for the moment I don't have the time. ๐
Just for clearance: my posted code is 100% open-source and MIT compatible. Everyone can use/adjust it (on his own risk). I would be happy if I'm authored as long as the code sticks closely to mine but it's in no way required.
@Gummibeer @freekmurze I'm definitely interested in taking this on. I will submit a PR as soon as time allows.
We'll continue this in #43