gpt4 book ai didi

php - Laravel Job::dispatch() 导致未定义方法错误

转载 作者:行者123 更新时间:2023-12-04 17:38:19 27 4
gpt4 key购买 nike

更新

Job::dispatchNow() 工作正常。


我已将我的 Laravel 应用程序从版本 5.7 更新到 5.8。

现在我的一些作业失败并显示一条消息,说 SyncJob::dispatch() 方法未定义。升级指南只提到在这个版本中删除了 fire() 方法。

您知道问题出在哪里吗?我试图创建导致相同错误的新工作。我已经删除并重新安装了完整的供应商目录、转储了自动加载、删除了缓存等。

作业由预定命令触发:

    protected function schedule(Schedule $schedule)
{
// ...
$schedule->command(RenewSubscriptionsCommand::class)->dailyAt('00:01');
// ...
}

This is the called command:

public function handle()
{
// ...
$subscriptions = SubscriptionService::collect()->getAllActiveSubscriptions();
foreach($subscriptions as $subscription){
dispatch(new RenewSubscriptionJob($subscription));
}
// ...
}

And finally the Job:

<?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!");
}
}
}

And the complete stack trace:

Symfony\Component\Debug\Exception\FatalThrowableError : Call to undefined method Illuminate\Queue\Jobs\SyncJob::dispatch()

at /home/vagrant/faaren/repositories/website-and-consumer-area/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php:42
38|
39| try {
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(Object(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

最佳答案

在 foreach 循环中像这样调度作业:

RenewSubscriptionJob::dispatch($subscription);

关于php - Laravel Job::dispatch() 导致未定义方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55645477/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com