更新
Job::dispatchNow()
工作正常。
我已经将Laravel应用程序从5.7版本更新到5.8。
现在我的某些作业失败,并显示一条消息,指出SyncJob :: dispatch()方法未定义。升级指南仅提及在此版本中删除了fire()方法。
有什么想法可以解决问题吗?我试图创建新的Jobs导致相同的错误。我已经删除了重新安装的完整供应商目录,转储了自动加载,删除了缓存等。
作业是通过预定命令触发的:
protected function schedule(Schedule $schedule)
{
// ...
$schedule->command(RenewSubscriptionsCommand::class)->dailyAt('00:01');
// ...
}
这是被调用的命令:
public function handle()
{
// ...
$subscriptions = SubscriptionService::collect()->getAllActiveSubscriptions();
foreach($subscriptions as $subscription){
dispatch(new RenewSubscriptionJob($subscription));
}
// ...
}
最后是工作:
<?php
namespace Subscription\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Subscription\Database\Entities\Subscription;
use Subscription\Exceptions\SubscriptionException;
use Subscription\SubscriptionService;
class RenewSubscriptionJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $subscription = NULL;
/**
* RenewSubscriptionJob constructor.
* @param Subscription $subscription
*/
public function __construct(Subscription $subscription)
{
$this->subscription = $subscription;
}
/**
* @throws SubscriptionException
*/
public function handle(): void
{
SubscriptionService::init($this->getSubscription())->renew();
}
protected function getSubscription(): Subscription
{
if($this->subscription instanceof Subscription){
return $this->subscription;
} else {
throw new SubscriptionException("Could not renew Subscription! No Subscription-Entity given!");
}
}
}
以及完整的堆栈跟踪:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError:调用未定义的方法Illuminate \ Queue \ Jobs \ SyncJob :: dispatch()
在/home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php:42中的38 | 39 |尝试{ 40 | $ this-> raiseBeforeJobEvent($ queueJob); 41 |
42 | $ queueJob-> dispatch(); 43 | 44 | $ this-> raiseAfterJobEvent($ queueJob); 45 | } catch(Exception $ e){ 46 | $ this-> handleException($ queueJob,$ e);
异常跟踪:
1 Illuminate \ Queue \ SyncQueue :: push(Object(Subscription \ Jobs \ RenewSubscriptionJob)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:184
2 Illuminate \ Bus \ Dispatcher :: pushCommandToQueue(Object(Illuminate \ Queue \ SyncQueue),Object(Subscription \ Jobs \ RenewSubscriptionJob)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:160
3 Illuminate \ Bus \ Dispatcher :: dispatchToQueue(Object(Subscription \ Jobs \ RenewSubscriptionJob)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:73
4 Illuminate \ Bus \ Dispatcher :: dispatch(Object(Subscription \ Jobs \ RenewSubscriptionJob)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Foundation/Bus/PendingDispatch.php:112
5 Illuminate \ Foundation \ Bus \ PendingDispatch :: __ destruct() /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:399
6个dispatch(Object(Subscription \ Jobs \ RenewSubscriptionJob)) /home/vagrant/faaren/repositories/website-and-consumer-area/packages/subscription/Commands/RenewSubscriptionsCommand.php:45
7个Subscription \ Commands \ RenewSubscriptionsCommand :: handle() /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32
8个call_user_func_array([]) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32
9 Illuminate \ Container \ BoundMethod :: Illuminate \ Container {closure}() /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:90
10 Illuminate \ Container \ BoundMethod :: callBoundMethod(Object(Illuminate \ Foundation \ Application),Object(Closure)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:34
11 Illuminate \ Container \ BoundMethod :: call(Object(Illuminate \ Foundation \ Application),[]) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Container/Container.php:580
12 Illuminate \ Container \ Container :: call() /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Console/Command.php:183
13 Illuminate \ Console \ Command :: execute(对象(Symfony \ Component \ Console \ Input \ ArgvInput),Object(Illuminate \ Console \ OutputStyle)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/symfony/console/Command/Command.php:255
14 Symfony \ Component \ Console \ Command \ Command :: run(Object(Symfony \ Component \ Console \ Input \ ArgvInput),Object(Illuminate \ Console \ OutputStyle)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Console/Command.php:170
15 Illuminate \ Console \ Command :: run(Object(Symfony \ Component \ Console \ Input \ ArgvInput),Object(Symfony \ Component \ Console \ Output \ ConsoleOutput)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/symfony/console/Application.php:908
16 Symfony \ Component \ Console \ Application :: doRunCommand(Object(Subscription \ Commands \ RenewSubscriptionsCommand),Object(Symfony \ Component \ Console \ Input \ ArgvInput),Object(Symfony \ Component \ Console \ Output \ ConsoleOutput)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/symfony/console/Application.php:269
17 Symfony \ Component \ Console \ Application :: doRun(Object(Symfony \ Component \ Console \ Input \ ArgvInput),Object(Symfony \ Component \ Console \ Output \ ConsoleOutput)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/symfony/console/Application.php:145
18 Symfony \ Component \ Console \ Application :: run(Object(Symfony \ Component \ Console \ Input \ ArgvInput),Object(Symfony \ Component \ Console \ Output \ ConsoleOutput)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Console/Application.php:90
19 Illuminate \ Console \ Application :: run(Object(Symfony \ Component \ Console \ Input \ ArgvInput),Object(Symfony \ Component \ Console \ Output \ ConsoleOutput)) /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:122
20 Illuminate \ Foundation \ Console \ Kernel :: handle(Object(Symfony \ Component \ Console \ Input \ ArgvInput),Object(Symfony \ Component \ Console \ Output \ ConsoleOutput)) / home / vagrant / faaren / repositories / website-and-consumer-area / artisan:37
答案 0 :(得分:1)
在foreach循环中像这样分发作业:
RenewSubscriptionJob::dispatch($subscription);