Skip to content

Commit e821bfd

Browse files
authored
[12.x] Ensure calling job within a group works as expected (#57224)
* init * cs * Update ScheduleGroupTest.php * Update ScheduleGroupTest.php
1 parent abeaa6f commit e821bfd

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/Illuminate/Console/Scheduling/Schedule.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,23 @@ public function job($job, $queue = null, $connection = null)
200200
: $job::class;
201201
}
202202

203-
return $this->name($jobName)->call(function () use ($job, $queue, $connection) {
204-
$job = is_string($job) ? Container::getInstance()->make($job) : $job;
203+
$this->events[] = $event = new CallbackEvent(
204+
$this->eventMutex, function () use ($job, $queue, $connection) {
205+
$job = is_string($job) ? Container::getInstance()->make($job) : $job;
206+
207+
if ($job instanceof ShouldQueue) {
208+
$this->dispatchToQueue($job, $queue ?? $job->queue, $connection ?? $job->connection);
209+
} else {
210+
$this->dispatchNow($job);
211+
}
212+
}, [], $this->timezone
213+
);
205214

206-
if ($job instanceof ShouldQueue) {
207-
$this->dispatchToQueue($job, $queue ?? $job->queue, $connection ?? $job->connection);
208-
} else {
209-
$this->dispatchNow($job);
210-
}
211-
});
215+
$event->name($jobName);
216+
217+
$this->mergePendingAttributes($event);
218+
219+
return $event;
212220
}
213221

214222
/**

tests/Integration/Console/Scheduling/ScheduleGroupTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Console\Scheduling\Schedule as ScheduleClass;
88
use Illuminate\Support\Carbon;
99
use Illuminate\Support\Facades\Schedule;
10+
use Illuminate\Tests\Console\Fixtures\JobToTestWithSchedule;
1011
use Orchestra\Testbench\TestCase;
1112
use PHPUnit\Framework\Attributes\DataProvider;
1213

@@ -195,4 +196,21 @@ public function testGroupedPendingEventAttribute()
195196
$this->assertSame('0 1 * * 1-5', $events[1]->expression);
196197
$this->assertSame('* * * * 1-5', $events[2]->expression);
197198
}
199+
200+
public function testGroupedPendingEventAttributesWithoutOverlapping()
201+
{
202+
$schedule = new ScheduleClass;
203+
$schedule->weekdays()->withoutOverlapping()->group(function ($schedule) {
204+
$schedule->command('inspire')->at('14:00'); // this is event, not pending attribute
205+
$schedule->at('03:00')->command('inspire'); // this is pending attribute
206+
$schedule->command('inspire'); // this goes back to group pending attribute
207+
$schedule->job(JobToTestWithSchedule::class)->at('04:00'); // this is pending attribute
208+
});
209+
210+
$events = $schedule->events();
211+
$this->assertSame('0 14 * * 1-5', $events[0]->expression);
212+
$this->assertSame('0 3 * * 1-5', $events[1]->expression);
213+
$this->assertSame('* * * * 1-5', $events[2]->expression);
214+
$this->assertSame('0 4 * * 1-5', $events[3]->expression);
215+
}
198216
}

0 commit comments

Comments
 (0)