douxian0008 2019-06-17 07:00 采纳率: 0%
浏览 471
已采纳

从命令调度作业时无法实例化接口

I wrote a custom artisan command in my Laravel app, which dispatches a Job. The job has a service dependency in it's handle() method which is resolved via Laravel's DI mechanism. The dependency is injected properly when running the app normally, however, when I try to run php artisan my:command from terminal, I get an error like this:

Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Cannot instantiate interface App\Services\ABCInterface at /Users/john/code/laravel-project/app/Providers/ABCServiceProvider.php:28)

Here is my the register() method of ABCServiceProvider (Please read the comments also):

public function register()
{
    $this->app->bind(ABCInterface::class, function ($app) {
        if ($app->environment(['testing', 'local'])) {

            // The following log statement is executed when the app runs normally.
            // But isn't executed when the artisan command is run from the console.
            \Log::debug('Instantiating FakeABCService'); 

            // The following class implements ABCInterface.
            return new FakeABCService;

        } else {

            // The following class implements ABCInterface.
            return new ABCService;
        }
    });
}

The handle() method of the Job being dispatched:

public function handle(ABCInterface $abcService)
{
    //
}

Finally, the handle() method of the artisan command class:

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $post = Post::first();

    if ($this->confirm("Are you sure?")) {
        MyJob::dispatch($post);
        $this->comment('Done!');
        return;
    }
}

Also, If I inject the dependency (type-hint) to The handle() method of the command class, laravel can resolve it. It's just the Job class' handle() method that's failing to resolve the dependency.

Why is it that the bound class is being resolved in normal application flow, but failing to be resolved when running the command? How can I fix this?

  • 写回答

1条回答 默认 最新

  • douqiao6563 2019-06-17 08:10
    关注

    Since the Job's handle function is resolved in the queue, try restarting your queue worker.

    Once you have made changes to your code, you have to restart the queue worker for your changes to take effect on the queue.

    Remember, queue workers are long-lived processes and store the booted application state in memory. As a result, they will not notice changes in your code base after they have been started. So, during your deployment process, be sure to restart your queue workers.

    Check the documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。