Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Added onOneServer option to short run commands #8

Merged
merged 7 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ Commands won't run whilst Laravel is in maintenance mode. If you would like to f
$shortSchedule->command('artisan-command')->everySecond()->runInMaintenanceMode();
```

### Running Tasks On One Server

Limit commands to only run on one server at a time.

```php
$shortSchedule->command('artisan-command')->everySecond()->onOneServer();
```

## Events

Executing any code when responding to these events is blocking. If your code takes a long time to execute, all short scheduled jobs will be delayed. We highly recommend to put any code you wish to execute in response to these events on a queue.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"require": {
"php": "^7.4",
"illuminate/cache": "^7.0",
"react/event-loop": "^1.1",
"spatie/temporary-directory": "^1.2"
},
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php>
<env name="CACHE_DRIVER" value="array"/>
</php>
</phpunit>
19 changes: 19 additions & 0 deletions src/PendingShortScheduleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class PendingShortScheduleCommand

protected bool $allowOverlaps = true;

protected bool $onOneServer = false;

protected bool $evenInMaintenanceMode = false;

protected array $constraints = [];
Expand Down Expand Up @@ -71,6 +73,13 @@ public function shouldRun(): bool
return ! $shouldNotRun;
}

public function onOneServer(): self
{
$this->onOneServer = true;

return $this;
}

public function between(string $startTime, string $endTime): self
{
$this->constraints[] = new BetweenConstraint($startTime, $endTime);
Expand Down Expand Up @@ -98,4 +107,14 @@ public function when(Closure $closure): self

return $this;
}

public function getOnOneServer(): bool
{
return $this->onOneServer;
}

public function cacheName(): string
{
return 'framework'.DIRECTORY_SEPARATOR.'schedule-'.sha1($this->frequencyInSeconds.$this->command);
}
}
28 changes: 27 additions & 1 deletion src/ShortScheduleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\ShortSchedule;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Spatie\ShortSchedule\Events\ShortScheduledTaskStarted;
use Spatie\ShortSchedule\Events\ShortScheduledTaskStarting;
use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -50,12 +51,37 @@ public function isRunning(): bool
}

public function run(): void
{
$this->pendingShortScheduleCommand->getOnOneServer() ? $this->processOnOneServer() : $this->processCommand() ;
}

protected function processOnOneServer(): void
{
if (Cache::has($this->pendingShortScheduleCommand->cacheName())) {
return;
}

Cache::add($this->pendingShortScheduleCommand->cacheName(), true, 60);

$this->processCommand();
$this->waitForProcessToFinish();

Cache::forget($this->pendingShortScheduleCommand->cacheName());
}

private function processCommand(): void
{
$commandString = $this->pendingShortScheduleCommand->command;
$this->process = Process::fromShellCommandline($this->pendingShortScheduleCommand->command);
$this->process = Process::fromShellCommandline($commandString);

event(new ShortScheduledTaskStarting($commandString, $this->process));
$this->process->start();
event(new ShortScheduledTaskStarted($commandString, $this->process));
}

private function waitForProcessToFinish(): void
{
while ($this->process->isRunning()) {
}
}
}
19 changes: 19 additions & 0 deletions tests/Feature/ShortScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\ShortSchedule\Tests\Feature;

use Illuminate\Support\Facades\Cache;
use Spatie\ShortSchedule\ShortSchedule;
use Spatie\ShortSchedule\Tests\TestCase;
use Spatie\ShortSchedule\Tests\TestClasses\TestKernel;
Expand Down Expand Up @@ -113,4 +114,22 @@ public function it_will_run_whilst_in_maintenance_mode()

$this->artisan('up')->expectsOutput('Application is now live.')->assertExitCode(0);
}

/** @test **/
public function do_not_run_if_already_running_on_another_server()
{
$key = 'framework'.DIRECTORY_SEPARATOR.'schedule-'.sha1('0.05'."echo 'called' >> '{$this->getTempFilePath()}'");
Cache::add($key, true, 60);

TestKernel::registerShortScheduleCommand(
fn (ShortSchedule $shortSchedule) => $shortSchedule
->exec("echo 'called' >> '{$this->getTempFilePath()}'")
->everySeconds(0.05)
->onOneServer()
);

$this
->runShortScheduleForSeconds(0.14)
->assertTempFileContains('called', 0);
}
}