如何从Laravel中的作业负载命令中获取特定数据?

huangapple go评论64阅读模式
英文:

How to get the specific data from jobs payload command in Laravel?

问题

我可以从作业表的负载中获取命令数据:

$job = DB::table('jobs')->first();
$payload = json_decode($job->payload);
$raw_command = json_decode($job->payload)->data->command;
$command = unserialize($raw_command);
dd($command->notification);

结果是:

App\Notifications\TenderInProgress {#1527 ▼
  +id: "0788ee9a-62c2-4280-9954-fc1d679228a8"
  +locale: null
  #tender_id: 5
  +connection: null
  +queue: null
  +chainConnection: null
  +chainQueue: null
  +chainCatchCallbacks: null
  +delay: Carbon\Carbon @1678173780 {#1528 ▶}
  +afterCommit: null
  +middleware: []
  +chained: []
}

我可以打印id的值:

dd($command->notification->id)

但无法打印tender_id的值:

无法访问受保护的属性App\Notifications\TenderInProgress::$tender_id

如何获取tender_id的值?

英文:

I can get the command data from payload on jobs table:

$job = DB::table('jobs')->first();
$payload = json_decode($job->payload);
$raw_command = json_decode($job->payload)->data->command;
$command = unserialize($raw_command);
dd($command->notification);

The result is:

App\Notifications\TenderInProgress {#1527 ▼
  +id: "0788ee9a-62c2-4280-9954-fc1d679228a8"
  +locale: null
  #tender_id: 5
  +connection: null
  +queue: null
  +chainConnection: null
  +chainQueue: null
  +chainCatchCallbacks: null
  +delay: Carbon\Carbon @1678173780 {#1528 ▶}
  +afterCommit: null
  +middleware: []
  +chained: []
}

I can dump the value of id:
dd($command->notification->id)

But cannot dump the value of tender_id:
Cannot access protected property App\Notifications\TenderInProgress::$tender_id

How can I get the value of tender_id?

答案1

得分: 0

好的,以下是翻译好的部分:

所以,正如我在评论中所述,你必须使用作业中间件,而不是直接读取作业的内容。

让我们在 app/Jobs/Middleware 中创建中间件,然后将其命名为 TenderChecker.php

<?php

namespace App\Jobs\Middleware;

class TenderChecker
{
    public function handle($job, $next)
    {
        if (Tender::findOrFail($job->tender_id)->status !== 'cancelled') {
            return $next($job); 
        }

        $job->delete();
    }
}

确保设置正确的检查,我只是提供了一个示例。思路是检查作业是否应该执行,在你的情况下,检查 tender_id 模型是否被取消。我不知道你是否使用了 SoftDeletesstatus 列,但进行逻辑检查,如果检查通过,执行 return $next($job);,以便它继续检查中间件,直到没有更多中间件并达到正常执行作业。

现在,你的作业需要具有 middleware 方法:

use App\Jobs\Middleware\TenderChecker;

class YourJob
{
    // ...
    public function middleware()
    {
        return [new TenderChecker];
    }
}

如果中间件通过,那么作业就可以执行,会自动执行。如果中间件 "失败",那么它将删除作业,因为这是你想要检查/执行的操作。


这是 Laravel 官方中间件的示例(这样你可以看到它们是如何使用/构建的):https://github.com/laravel/framework/tree/10.x/src/Illuminate/Queue/Middleware

这是官方文档:https://laravel.com/docs/10.x/queues#job-middleware

英文:

So, as I have stated on the comments, you have to use a Job Middleware, not read the content of a job (not directly).

Lets create the middleware in app/Jobs/Middleware and lets call it TenderChecker.php:

&lt;?php
 
namespace App\Jobs\Middleware;
 
class TenderChecker
{
    public function handle($job, $next)
    {
        if (Tender::findOrFail($job-&gt;tender_id)-&gt;status !== &#39;cancelled&#39;) {
            return $next($job); 
        }

        $job-&gt;delete();
    }
}

Do set the right check, I am just giving an example here. The idea is to check if the job should be executed, in your case, if tender_id model is not cancelled. I have no idea if you are using SoftDeletes or a status column, but do the logical check and if it passes the check, do execute return $next($job); so it continues checking middlewares until there are no more middlewares and reaches the job for normal execution.

Now, your job would need to have the middleware method:

use App\Jos\Middleware\TenderChecker;

class YourJob
{
    // ...
    public function middleware()
    {
        return [new TenderChecker];
    }
}

If the middleware passes, then the job is good to go and automatically executes . If the middleware "fails", then it will delete the job, because that is what you want to check/do.


Here is an example of Laravel's officials middlewares (so you can see how they are used/built): https://github.com/laravel/framework/tree/10.x/src/Illuminate/Queue/Middleware

And here is the official docs: https://laravel.com/docs/10.x/queues#job-middleware

huangapple
  • 本文由 发表于 2023年3月7日 11:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定