|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Matomo - free/libre analytics platform |
| 5 | + * |
| 6 | + * @link https://matomo.org |
| 7 | + * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Piwik\Plugins\CoreAdminHome\tests\Integration\Commands; |
| 11 | + |
| 12 | +use Piwik\CliMulti\ProcessSymfony; |
| 13 | +use Piwik\Plugins\CoreAdminHome\Tasks; |
| 14 | +use Piwik\Plugins\CoreAdminHome\tests\Fixtures\RunScheduledTasksProcessSignal as RunScheduledTasksProcessSignalFixture; |
| 15 | +use Piwik\Scheduler\Task; |
| 16 | +use Piwik\Tests\Framework\Fixture; |
| 17 | +use Piwik\Tests\Framework\TestCase\IntegrationTestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * @group Core |
| 21 | + * @group CoreAdminHome |
| 22 | + * @group RunScheduledTasksProcessSignal |
| 23 | + */ |
| 24 | +class RunScheduledTasksProcessSignalTest extends IntegrationTestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var RunScheduledTasksProcessSignalFixture |
| 28 | + */ |
| 29 | + public static $fixture; |
| 30 | + |
| 31 | + public function setUp(): void |
| 32 | + { |
| 33 | + if (!extension_loaded('pcntl') || !function_exists('pcntl_signal')) { |
| 34 | + $this->markTestSkipped('signal test cannot run without ext-pcntl'); |
| 35 | + } |
| 36 | + |
| 37 | + parent::setUp(); |
| 38 | + |
| 39 | + self::$fixture->stepControl->reset(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @dataProvider getScheduledTasksStoppedData |
| 44 | + */ |
| 45 | + public function testScheduledTasksStopped(int $signal): void |
| 46 | + { |
| 47 | + self::$fixture->stepControl->blockScheduledTasks(); |
| 48 | + |
| 49 | + $process = $this->startScheduledTasks(); |
| 50 | + |
| 51 | + // wait until scheduled tasks are running |
| 52 | + $result = self::$fixture->stepControl->waitForSuccess(static function () use ($process): bool { |
| 53 | + return false !== strpos($process->getOutput(), 'Scheduler: executing task'); |
| 54 | + }, $timeoutInSeconds = 30); |
| 55 | + |
| 56 | + self::assertTrue($result, 'Scheduled tasks did not start'); |
| 57 | + |
| 58 | + $this->sendSignalToProcess($process, $signal); |
| 59 | + |
| 60 | + self::$fixture->stepControl->unblockScheduledTasks(); |
| 61 | + |
| 62 | + $this->waitForProcessToStop($process); |
| 63 | + |
| 64 | + $processOutput = $process->getOutput(); |
| 65 | + $expectedExecutedTask = Task::getTaskName(Tasks::class, 'invalidateOutdatedArchives', null); |
| 66 | + $expectedSkippedTask = Task::getTaskName(Tasks::class, 'purgeOutdatedArchives', null); |
| 67 | + |
| 68 | + self::assertStringContainsString('executing task ' . $expectedExecutedTask, $processOutput); |
| 69 | + self::assertStringNotContainsString('executing task ' . $expectedSkippedTask, $processOutput); |
| 70 | + |
| 71 | + self::assertStringContainsString( |
| 72 | + 'Received system signal to stop scheduled tasks: ' . $signal, |
| 73 | + $processOutput |
| 74 | + ); |
| 75 | + |
| 76 | + self::assertStringContainsString('Scheduler: Aborting due to received signal', $processOutput); |
| 77 | + } |
| 78 | + |
| 79 | + public function getScheduledTasksStoppedData(): iterable |
| 80 | + { |
| 81 | + yield 'stop using sigint' => [\SIGINT]; |
| 82 | + yield 'stop using sigterm' => [\SIGTERM]; |
| 83 | + } |
| 84 | + |
| 85 | + private function sendSignalToProcess(ProcessSymfony $process, int $signal): void |
| 86 | + { |
| 87 | + $process->signal($signal); |
| 88 | + |
| 89 | + $result = self::$fixture->stepControl->waitForSuccess( |
| 90 | + static function () use ($process, $signal): bool { |
| 91 | + return false !== strpos( |
| 92 | + $process->getOutput(), |
| 93 | + 'Received system signal to stop scheduled tasks: ' . $signal |
| 94 | + ); |
| 95 | + } |
| 96 | + ); |
| 97 | + |
| 98 | + self::assertTrue($result, 'Process did not acknowledge signal'); |
| 99 | + } |
| 100 | + |
| 101 | + private function startScheduledTasks(): ProcessSymfony |
| 102 | + { |
| 103 | + // exec is mandatory to send signals to the process |
| 104 | + // not using array notation because "Fixture::getCliCommandBase" contains parameters |
| 105 | + $process = ProcessSymfony::fromShellCommandline(sprintf( |
| 106 | + 'exec %s scheduled-tasks:run -vvv --force', |
| 107 | + Fixture::getCliCommandBase() |
| 108 | + )); |
| 109 | + |
| 110 | + $process->setEnv([RunScheduledTasksProcessSignalFixture::ENV_TRIGGER => '1']); |
| 111 | + $process->setTimeout(null); |
| 112 | + $process->start(); |
| 113 | + |
| 114 | + self::assertTrue($process->isRunning()); |
| 115 | + self::assertNotNull($process->getPid()); |
| 116 | + |
| 117 | + return $process; |
| 118 | + } |
| 119 | + |
| 120 | + private function waitForProcessToStop(ProcessSymfony $process): void |
| 121 | + { |
| 122 | + $result = self::$fixture->stepControl->waitForSuccess(static function () use ($process): bool { |
| 123 | + return !$process->isRunning(); |
| 124 | + }); |
| 125 | + |
| 126 | + self::assertTrue($result, 'Archiving process did not stop'); |
| 127 | + self::assertSame(0, $process->getExitCode()); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +RunScheduledTasksProcessSignalTest::$fixture = new RunScheduledTasksProcessSignalFixture(); |
0 commit comments