Skip to content

Commit daecbaf

Browse files
Merge pull request #7 from hammerstonedev/af-fix-mutex
Fix mutexes
2 parents 7a04e27 + 39762ff commit daecbaf

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

src/FlakyCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Illuminate\Console\Command;
99
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Env;
1011
use Illuminate\Support\Traits\Macroable;
1112

1213
/**
@@ -57,7 +58,7 @@ protected function generateCommandId()
5758
protected function isScheduledCommand()
5859
{
5960
// See the FlakyServiceProvider to see where this is coming from.
60-
return Arr::get($_ENV, 'IS_SCHEDULED', 0) === '1';
61+
return Env::get('IS_SCHEDULED') === '1';
6162
}
6263

6364
protected function hashInput()

src/Providers/FlakyServiceProvider.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
namespace Hammerstone\Flaky\Providers;
77

8-
use Illuminate\Console\Events\ScheduledTaskStarting;
8+
use Illuminate\Console\Events\CommandStarting;
9+
use Illuminate\Support\Env;
910
use Illuminate\Support\Facades\Event;
1011
use Illuminate\Support\ServiceProvider;
1112

@@ -15,11 +16,12 @@ public function register()
1516
{
1617
// $this->mergeConfigFrom(__DIR__ . '/../../config/flaky.php', 'flaky');
1718

18-
Event::listen(function (ScheduledTaskStarting $event) {
19-
if ($event->task->command) {
20-
// Laravel provides no way to tell if a command is running via the
21-
// scheduler, so we just add our own environment variable here.
22-
$event->task->command = 'IS_SCHEDULED=1 ' . $event->task->command;
19+
Event::listen(function (CommandStarting $event) {
20+
// When the schedule is starting we add an ENV variable. That
21+
// variable will get propagated down to all spawned commands
22+
// via the Symfony Process `getDefaultEnv` method.
23+
if ($event->command === 'schedule:run') {
24+
Env::getRepository()->set('IS_SCHEDULED', 1);
2325
}
2426
});
2527
}

tests/Unit/CommandTest.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
namespace Hammerstone\Flaky\Tests\Unit;
77

8-
use Illuminate\Console\Events\ScheduledTaskStarting;
9-
use Illuminate\Console\Scheduling\CacheEventMutex;
10-
use Illuminate\Console\Scheduling\Event as SchedulingEvent;
8+
use Illuminate\Support\Env;
119
use Illuminate\Support\Facades\Artisan;
1210
use Illuminate\Support\Facades\Event;
1311

@@ -44,23 +42,27 @@ public function scheduled_only()
4442
$this->assertEquals('true', $disabled);
4543

4644
// This gets added by our event listener, which is tested elsewhere.
47-
$_ENV['IS_SCHEDULED'] = '1';
45+
Env::getRepository()->set('IS_SCHEDULED', 1);
4846

4947
Artisan::call('flaky:scheduled');
5048
$disabled = trim(Artisan::output());
5149

5250
$this->assertEquals('false', $disabled);
5351

54-
unset($_ENV['IS_SCHEDULED']);
52+
Env::getRepository()->set('IS_SCHEDULED', 0);
5553
}
5654

5755
/** @test */
58-
public function scheduled_command_gets_modified()
56+
public function env_var_gets_set()
5957
{
60-
Event::dispatch(new ScheduledTaskStarting(
61-
$task = new SchedulingEvent(app(CacheEventMutex::class), 'command')
62-
));
58+
$repo = Env::getRepository();
6359

64-
$this->assertEquals('IS_SCHEDULED=1 command', $task->command);
60+
$repo->set('IS_SCHEDULED', 0);
61+
62+
$this->assertEquals('0', $repo->get('IS_SCHEDULED'));
63+
64+
Artisan::call('schedule:run');
65+
66+
$this->assertEquals('1', $repo->get('IS_SCHEDULED'));
6567
}
6668
}

0 commit comments

Comments
 (0)