|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\Events\ScheduledTaskFailed; |
| 6 | +use Illuminate\Console\Events\ScheduledTaskFinished; |
| 7 | +use Illuminate\Console\Events\ScheduledTaskStarting; |
| 8 | +use Illuminate\Container\Container; |
| 9 | +use Illuminate\Support\Facades\App; |
| 10 | +use Illuminate\Support\Facades\Event; |
| 11 | +use ProcessMaker\Console\Scheduling\FastSchedule; |
| 12 | +use ProcessMaker\Facades\Metrics; |
| 13 | +use ProcessMaker\Listeners\ScheduledTaskMetricsSubscriber; |
| 14 | +use ProcessMaker\Multitenancy\Tenant; |
| 15 | +use ProcessMaker\Services\MetricsService; |
| 16 | +use Prometheus\Storage\InMemory; |
| 17 | +use ReflectionProperty; |
| 18 | +use RuntimeException; |
| 19 | +use Tests\TestCase; |
| 20 | + |
| 21 | +class ScheduledTaskMetricsSubscriberTest extends TestCase |
| 22 | +{ |
| 23 | + public function setUpMetrics(): void |
| 24 | + { |
| 25 | + config(['app.multitenancy' => false]); |
| 26 | + |
| 27 | + App::instance(MetricsService::class, new MetricsService(new InMemory())); |
| 28 | + } |
| 29 | + |
| 30 | + public function testRecordsSuccessfulScheduledTaskMetrics(): void |
| 31 | + { |
| 32 | + $task = (new FastSchedule())->command('emails:send --queue'); |
| 33 | + |
| 34 | + Event::dispatch(new ScheduledTaskStarting($task)); |
| 35 | + $task->exitCode = 0; |
| 36 | + Event::dispatch(new ScheduledTaskFinished($task, 2.41)); |
| 37 | + |
| 38 | + $metrics = Metrics::renderMetrics(); |
| 39 | + |
| 40 | + $this->assertStringContainsString( |
| 41 | + ScheduledTaskMetricsSubscriber::LAST_SUCCESS_TIMESTAMP . '{job="emails:send"}', |
| 42 | + $metrics |
| 43 | + ); |
| 44 | + $this->assertStringContainsString( |
| 45 | + ScheduledTaskMetricsSubscriber::DURATION_SECONDS . '{job="emails:send"} 2.41', |
| 46 | + $metrics |
| 47 | + ); |
| 48 | + $this->assertStringContainsString( |
| 49 | + ScheduledTaskMetricsSubscriber::RUNS_TOTAL . '{job="emails:send",status="success"} 1', |
| 50 | + $metrics |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + public function testRecordsFailedScheduledTaskMetricsAndDuration(): void |
| 55 | + { |
| 56 | + $task = (new FastSchedule())->command('emails:send'); |
| 57 | + |
| 58 | + Event::dispatch(new ScheduledTaskStarting($task)); |
| 59 | + Event::dispatch(new ScheduledTaskFailed($task, new RuntimeException('Sending failed'))); |
| 60 | + |
| 61 | + $metrics = Metrics::renderMetrics(); |
| 62 | + |
| 63 | + $this->assertStringContainsString( |
| 64 | + ScheduledTaskMetricsSubscriber::LAST_FAILURE_TIMESTAMP . '{job="emails:send"}', |
| 65 | + $metrics |
| 66 | + ); |
| 67 | + $this->assertStringContainsString( |
| 68 | + ScheduledTaskMetricsSubscriber::DURATION_SECONDS . '{job="emails:send"}', |
| 69 | + $metrics |
| 70 | + ); |
| 71 | + $this->assertStringContainsString( |
| 72 | + ScheduledTaskMetricsSubscriber::RUNS_TOTAL . '{job="emails:send",status="failure"} 1', |
| 73 | + $metrics |
| 74 | + ); |
| 75 | + $this->assertStringNotContainsString( |
| 76 | + ScheduledTaskMetricsSubscriber::LAST_SUCCESS_TIMESTAMP . '{job="emails:send"}', |
| 77 | + $metrics |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public function testNonZeroExitIsOnlyRecordedAsFailure(): void |
| 82 | + { |
| 83 | + $task = (new FastSchedule())->command('emails:send'); |
| 84 | + |
| 85 | + Event::dispatch(new ScheduledTaskStarting($task)); |
| 86 | + $task->exitCode = 1; |
| 87 | + Event::dispatch(new ScheduledTaskFinished($task, 1.25)); |
| 88 | + Event::dispatch(new ScheduledTaskFailed($task, new RuntimeException('Exit code 1'))); |
| 89 | + |
| 90 | + $metrics = Metrics::renderMetrics(); |
| 91 | + |
| 92 | + $this->assertStringContainsString( |
| 93 | + ScheduledTaskMetricsSubscriber::RUNS_TOTAL . '{job="emails:send",status="failure"} 1', |
| 94 | + $metrics |
| 95 | + ); |
| 96 | + $this->assertStringNotContainsString( |
| 97 | + ScheduledTaskMetricsSubscriber::RUNS_TOTAL . '{job="emails:send",status="success"}', |
| 98 | + $metrics |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + public function testTenantMetricsServiceIsResolvedAfterTenantBecomesCurrent(): void |
| 103 | + { |
| 104 | + config(['app.multitenancy' => true]); |
| 105 | + app()->instance(config('multitenancy.current_tenant_container_key'), new Tenant(['id' => 123])); |
| 106 | + |
| 107 | + app(ScheduledTaskMetricsSubscriber::class)->handleStarting( |
| 108 | + new ScheduledTaskStarting((new FastSchedule())->command('emails:send')) |
| 109 | + ); |
| 110 | + |
| 111 | + $instances = (new ReflectionProperty(Container::class, 'instances'))->getValue(app()); |
| 112 | + $this->assertArrayNotHasKey(MetricsService::class, $instances); |
| 113 | + |
| 114 | + app()->forgetInstance(config('multitenancy.current_tenant_container_key')); |
| 115 | + } |
| 116 | +} |
0 commit comments