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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配