Skip to content

Commit b194985

Browse files
committed
Merge branch 'mihaliak/9.x' into 9.x
2 parents cfc3433 + 6dbc25c commit b194985

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/Illuminate/Console/Scheduling/Event.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ class Event
149149
*/
150150
public $mutex;
151151

152+
/**
153+
* The mutex name resolver callback.
154+
*
155+
* @var \Closure|null
156+
*/
157+
public $mutexNameResolver;
158+
152159
/**
153160
* The exit status code of the command.
154161
*
@@ -935,9 +942,28 @@ public function preventOverlapsUsing(EventMutex $mutex)
935942
*/
936943
public function mutexName()
937944
{
945+
$mutexNameResolver = $this->mutexNameResolver;
946+
947+
if (! is_null($mutexNameResolver) && is_callable($mutexNameResolver)) {
948+
return $mutexNameResolver($this);
949+
}
950+
938951
return 'framework'.DIRECTORY_SEPARATOR.'schedule-'.sha1($this->expression.$this->command);
939952
}
940953

954+
/**
955+
* Set the mutex name or name resolver callback.
956+
*
957+
* @param \Closure|string $mutexName
958+
* @return $this
959+
*/
960+
public function createMutexNameUsing(Closure|string $mutexName)
961+
{
962+
$this->mutexNameResolver = is_string($mutexName) ? fn () => $mutexName : $mutexName;
963+
964+
return $this;
965+
}
966+
941967
/**
942968
* Delete the mutex for the event.
943969
*

tests/Console/Scheduling/EventTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Scheduling\Event;
66
use Illuminate\Console\Scheduling\EventMutex;
7+
use Illuminate\Support\Str;
78
use Mockery as m;
89
use PHPUnit\Framework\TestCase;
910

@@ -94,4 +95,18 @@ public function testNextRunDate()
9495

9596
$this->assertSame('10:15:00', $event->nextRunDate()->toTimeString());
9697
}
98+
99+
public function testCustomMutexName()
100+
{
101+
$event = new Event(m::mock(EventMutex::class), 'php -i');
102+
$event->description('Fancy command description');
103+
104+
$this->assertSame('framework'.DIRECTORY_SEPARATOR.'schedule-eeb46c93d45e928d62aaf684d727e213b7094822', $event->mutexName());
105+
106+
$event->createMutexNameUsing(function (Event $event) {
107+
return Str::slug($event->description);
108+
});
109+
110+
$this->assertSame('fancy-command-description', $event->mutexName());
111+
}
97112
}

0 commit comments

Comments
 (0)