我想知道将周末这样的事情添加到可用的时间表约束中的最佳方法是什么:
Illuminate\Console\Scheduling\Event.php
public function weekdays()
{
return $this->spliceIntoPosition(5, '1-5');
}及其逻辑上的相反:
public function weekends()
{
return $this->days(array( '0','6'));
}我将把这些内容包括在哪里,这样它们就不会被框架更新覆盖?
发布于 2015-07-23 23:30:36
首先,如果您所缺少的是()方法,则可以通过在事件上调用days(6,7)来实现这一点。
如果您需要向调度程序添加更多的逻辑,请继续阅读。
我看了一下代码,虽然Laravel没有提供用其他方法扩展Scheduler,特别是其计划的Events的方法,但是仍然可以从您的中应用额外的约束
根据您的需要,有两种方法可以做到这一点。
如果要对多个事件重用逻辑,可以在Kernel.php中添加助手函数,例如:
protected function specialSchedule(\Illuminate\Console\Scheduling\Event $event) {
$scheduledAt = $event->getExpression();
...; // modify $scheduledAt expression
$event->cron($scheduledAt);
return $event;
}然后,您可以在定义计划时重用该逻辑:
protected function schedule(Schedule $schedule)
{
$this->specialSchedule($schedule->call(function () {
//scheduled code
}));
}更新:
还有一种方法--更复杂一点,因为它要求您提供自己的时间表和事件类,但也需要更灵活。
首先,实现您自己的事件类,并在那里添加新方法:
class CustomEvent extends \Illuminate\Console\Scheduling\CallbackEvent {
public function weekends() {
return $this->days(6,7);
}
}然后是您自己的调度类,以便创建CustomEvent对象:
class CustomSchedule extends \Illuminate\Console\Scheduling\Schedule
{
public function call($callback, array $parameters = [])
{
$this->events[] = $event = new CustomEvent($callback, $parameters);
return $event;
}
public function exec($command, array $parameters = [])
{
if (count($parameters)) {
$command .= ' '.$this->compileParameters($parameters);
}
$this->events[] = $event = new Event($command);
return $event;
}
}最后,在您的Kernel.php中,您还需要确保您的新调度类用于调度:
protected function defineConsoleSchedule()
{
$this->app->instance(
'Illuminate\Console\Scheduling\Schedule', $schedule = new Schedule
);
$this->schedule($schedule);
}发布于 2020-05-21 16:58:26
折叠@jedrzej.kurylo的回答,我是在laravel 5.8上这样做的:
php artisan make:model CustomCallbackEvent
php artisan make:model CustomEvent
php artisan make:model CustomSchedule
在CustomCallbackEvent中:
use Illuminate\Console\Scheduling\CallbackEvent;
use Illuminate\Console\Scheduling\EventMutex;
class CustomCallbackEvent extends CallbackEvent
{
public function __construct(EventMutex $mutex, $callback, array $parameters = [])
{
parent::__construct($mutex, $callback, $parameters);
}
}在CustomSchedule中:
use Illuminate\Console\Scheduling\Schedule;
class CustomSchedule extends Schedule
{
public function call($callback, array $parameters = [])
{
$this->events[] = $event = new CustomCallbackEvent(
$this->eventMutex,
$callback,
$parameters
);
return $event;
}
public function exec($command, array $parameters = [])
{
if (count($parameters)) {
$command .= ' '.$this->compileParameters($parameters);
}
$this->events[] = $event = new CustomEvent($this->eventMutex, $command, $this->timezone);
return $event;
}
}在CustomEvent中:
use Illuminate\Console\Scheduling\Event;
class CustomEvent extends Event
{
public function myFunction()
{
//your logic here
}
}在Kernel.php中:
protected function defineConsoleSchedule()
{
$this->app->instance(
'Illuminate\Console\Scheduling\Schedule', $schedule = new CustomSchedule
);
$this->schedule($schedule);
}https://stackoverflow.com/questions/31599351
复制相似问题