无法解决依赖关系 - Laravel

2024-03-19

照亮\合同\容器\BindingResolutionException 无法解析 App\Jobs\BudgetFetch 类中的依赖关系 [参数 #0 [ $customerId ]]

namespace App\Http\Controllers;
use App\Jobs\BudgetFetch;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V3\GoogleAdsClientBuilder;
use Illuminate\Support\Facades\DB;

class APIController extends Controller
{
    public function index() {
        $clients = DB::table('account_names')->pluck('code');

        foreach($clients as $customerId) {
            budgetFetch::dispatch($customerId);
        }
    }
}

上面是一个简单的控制器,我将其组合在一起以触发另一项工作:

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V3\GoogleAdsClientBuilder;

class BudgetFetch implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle($customerId)
    {
        $clientId = "xxxxxxxxxxxx";
        $clientSecret = "xxxxxxxxxxx";
        $refreshToken = "xxxxxxxxxxxxxxxxxx";
        $developerToken = "xxxxxxxxxxxxxxxxxx";
        $loginCustomerId = "xxxxxxxxxxx";

        $oAuth2Credential = (new OAuth2TokenBuilder())
            ->withClientId($clientId)
            ->withClientSecret($clientSecret)
            ->withRefreshToken($refreshToken)
            ->build()
        ;

        $googleAdsClient = (new GoogleAdsClientBuilder())
            ->withDeveloperToken($developerToken)
            ->withLoginCustomerId($loginCustomerId)
            ->withOAuth2Credential($oAuth2Credential)
            ->build()
        ;

        $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
        $query = 'SELECT campaign.id, campaign.name, metrics.cost_micros, campaign_budget.amount_micros FROM campaign ORDER BY campaign.id';
        /** @var GoogleAdsServerStreamDecorator $stream */
        $stream =
            $googleAdsServiceClient->searchStream($customerId, $query);

        foreach ($stream->iterateAllElements() as $googleAdsRow) {
            /** @var GoogleAdsRow $googleAdsRow */
            $metrics = $googleAdsRow->getMetrics();
            $id = $googleAdsRow->getCampaign()->getId()->getValue();
            $name = $googleAdsRow->getCampaign()->getName()->getValue();
            $spend = $googleAdsRow->getCampaignBudget()->getAmountMicros()->getValue();
            $budget = $metrics->getCostMicrosUnwrapped();

            if($spend >= $budget){
                DB::table('budget_alerts')->insert(
                    ['campaign' => $id, 'hitBudget' => 1]
                );
            };
        }
    }
}

问题顶部的错误是我从中得到的,如果我将变量放入 __construct 中,也会发生同样的事情。我已经运行了一个 php artisan 清晰编译的程序,我在另一个类似的问题中发现了它,但它似乎没有解决任何问题。


$customerId应该在构造函数中:

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V3\GoogleAdsClientBuilder;

class BudgetFetch implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $customerId;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($customerId)
    {
        $this->customerId = $customerId;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $customerId = $this->customerId;
        $clientId = "xxxxxxxxxxxx";
        $clientSecret = "xxxxxxxxxxx";
        $refreshToken = "xxxxxxxxxxxxxxxxxx";
        $developerToken = "xxxxxxxxxxxxxxxxxx";
        $loginCustomerId = "xxxxxxxxxxx";

        $oAuth2Credential = (new OAuth2TokenBuilder())
            ->withClientId($clientId)
            ->withClientSecret($clientSecret)
            ->withRefreshToken($refreshToken)
            ->build()
        ;

        $googleAdsClient = (new GoogleAdsClientBuilder())
            ->withDeveloperToken($developerToken)
            ->withLoginCustomerId($loginCustomerId)
            ->withOAuth2Credential($oAuth2Credential)
            ->build()
        ;

        $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
        $query = 'SELECT campaign.id, campaign.name, metrics.cost_micros, campaign_budget.amount_micros FROM campaign ORDER BY campaign.id';
        /** @var GoogleAdsServerStreamDecorator $stream */
        $stream =
            $googleAdsServiceClient->searchStream($customerId, $query);

        foreach ($stream->iterateAllElements() as $googleAdsRow) {
            /** @var GoogleAdsRow $googleAdsRow */
            $metrics = $googleAdsRow->getMetrics();
            $id = $googleAdsRow->getCampaign()->getId()->getValue();
            $name = $googleAdsRow->getCampaign()->getName()->getValue();
            $spend = $googleAdsRow->getCampaignBudget()->getAmountMicros()->getValue();
            $budget = $metrics->getCostMicrosUnwrapped();

            if($spend >= $budget){
                DB::table('budget_alerts')->insert(
                    ['campaign' => $id, 'hitBudget' => 1]
                );
            };
        }
    }
}

原因是作业没有立即调用。它首先被构造,然后放入队列中,它将(在某个时刻)被删除并“处理”。所以handle无法直接访问您传入的任何参数dispatch。文档确实表明句柄可以接受参数,但这些只是可以使用依赖项注入注入的参数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法解决依赖关系 - Laravel 的相关文章

随机推荐

  • 如何将 JSONString 解析为数据集?

    我正在使用 Web 服务创建 C 应用程序 在我的网络服务中我使用JSONString数据 但我无法将此字符串转换为DataSet My JSONString is Table DisplayVoucherNumber A101239Z A
  • 为什么在 Python 包中使用绝对导入而不是相对导入?

    我最近创建了一个 Python 包 在其中仅使用相对导入来访问存储在其他方法中的函数 现在 在 Numpy 中 我看到很多文件大量使用绝对导入 例如这个文件 https github com numpy numpy blob 8f547f2
  • io:ios应用程序开发选项变灰

    我刚刚签署并创建了一个具有钥匙串访问权限的证书 然后在开发人员门户中单击证书 gt 开发人员 gt 当系统提示我您需要什么类型的证书时 正在开发的ios应用程序开发是灰色的 有人知道为什么吗 我需要吊销证书吗 每个用户只能申请一份开发证书
  • 将 Facebook Connect 与 Authlogic 结合使用

    我正在努力使 Authlogic 和 Facebook Connect 使用 Facebook 发挥良好作用 以便您可以通过正常注册方式或使用 Facebook Connect 创建帐户 我已经能够以一种方式使连接正常工作 但注销仅在 Fa
  • 查询输入必须至少包含一个表或查询

    我在 access 中有一个查询 应该在插入之前检查该项目是否已存在于数据库中 INSERT INTO FinalizedPrintedStickers Values 0000846043 481 9 0 48IG 1F Straight
  • 装箱和拆箱,为什么输出不是都是“System.Object”?

    我得到以下代码 object var3 3 Console WriteLine var3 GetType ToString Console WriteLine typeof object ToString 输出是 System Int32
  • 动态加载.js文件时捕获onload事件?

    有没有捕获onload在 IE 中使用 JavaScript 动态添加脚本标签时会发生事件吗 下面的代码适用于 FireFox 和 Chrome 但不适用于 IE
  • 数列识别

    从另一个问题发展而来 识别R中重复数字的序列 https stackoverflow com questions 7509381 identifying sequences of repeated numbers in r 15328802
  • 根据运行时条件在 Azure Pipeline 中执行或不执行模板

    我已经运行了 Azure Pipeline 现在 我想仅当运行时某个条件成立时才执行一系列步骤 Example steps template steps checkout yml some more steps here bash if s
  • 如何使用Nodejs从上传的ppt文件中获取幻灯片数量?

    我们可以从文件的属性中看到文件详细信息 如下图所示 我需要使用 Nodejs 或 Angularjs 以编程方式获得相同的细节 我不认为 Angularjs 可以完成文件操作 是否可以在节点中获取文件的相同信息 我猜 shellsjs 会支
  • ruby 中的三向比较

    确保 ruby 中三个变量全部相等的最简洁方法是什么 例如 dog animal cat animal chicken animal shoe clothing Something like this which doesn t work
  • 创建一个闭包

    我想创建一个闭包 函数生成器 来计算数字的幂 而不使用特定的 Clojure 库来完成此任务 现在 我可以用循环 重复来做到这一点 defn exp1 in num in exp multi loop num in num exp mult
  • 在 Shiny 应用程序中包含一个 javascript 文件

    我需要将 js 库包含到我的 Shiny 应用程序中 目前我使用 includeHTML 将脚本直接包含到 html 代码中 例如 includeHTML URL js 如果我使用tags script 例如 当我尝试浏览js文件时 浏览器
  • 如何将 JointJS 与使用 Angular CLI 构建的应用程序一起使用?

    我已经通过 npm 安装了 jointjs 并安装了 types 并且代码编译 构建良好 Code import Component from angular core import as joint from node modules j
  • 辅音和元音 Swift

    我是 Swift 的新手 谁能向我解释一下为什么我总是遇到这个问题 我正在使用 Xcode 6 4 但这是我的问题 我希望我能解决它 但我需要我的函数接受大字符串 然后返回 Tuple numVowels numConsonants 计算以
  • Virtualenv 没有名为 zlib 的模块

    我正在尝试在 Python2 6 下创建 Python 2 7 虚拟环境 我只是运行 virtualenv python python27 python27 correctly leads to my python installation
  • 仅解压缩特定扩展名

    我有一个包含 jpg png gif 图像的 zip 存档目录 我想解压缩每个存档 仅获取图像并将它们放入具有存档名称的文件夹中 So files archive1 zip files archive2 zip files archive3
  • JSF 2.0 注入不同范围的托管 bean

    我有一个无状态的控制器 负责处理表单 这被定义为ApplicationScoped 在我的页面上 我有一个与支持 bean 关联的表单 定义为ViewScoped 当我想处理表单时出现错误 serverError class com sun
  • 如何在 Blazor 客户端应用程序中使用 Bootstrap 模式?

    我正在尝试显示引导模式然后绑定其按钮 但我无法通过显示模式的第一步 我正在使用 net core 3 1 的 Blazor 客户端模板 我有一个名为 Modal razor 的页面 其中包含我从 getbootstrap com 找到的引导
  • 无法解决依赖关系 - Laravel

    照亮 合同 容器 BindingResolutionException 无法解析 App Jobs BudgetFetch 类中的依赖关系 参数 0 customerId namespace App Http Controllers use