如何在 Laravel 中通知用户作业已完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35337007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:13:35  来源:igfitidea点击:

How to notify the user that a job has completed in Laravel?

phplaravelnotificationsjobs

提问by rrrafalsz

in Laravel, you can use jobsto execute tasks in a back-end queue while the rest of the application does other things. i have a job that is initiated by user input. immediately, through javascript, i give the user a notification that the job is being processed.

在 Laravel 中,您可以使用作业在后端队列中执行任务,而应用程序的其余部分则做其他事情。我有一个由用户输入启动的工作。立即,通过 javascript,我向用户通知正在处理作业。

i would like to be able to give a similar notification after the job has successfully completed.

我希望能够在工作成功完成后发出类似的通知。

i am calling my job from within a model like this:

我从这样的模型中调用我的工作:

public function doSomething() {
    $job = new \App\Jobs\MyJob();
    app('Illuminate\Contracts\Bus\Dispatcher')->dispatch($job);
}

and this is how my job headers look like:

这就是我的工作标题的样子:

class MyJob extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels, Queueable;
    ...
}

the model job call is actually triggered from a controller method:

模型作业调用实际上是从控制器方法触发的:

public function getDoSomething($id) {
    $item = Item::findOrFail($id);
    $item->doSomething();

    return response()->json(true);
}

which is handled by an AJAX call:

这是由 AJAX 调用处理的:

$.ajax({
    url: $(this).attr('href'),
    type: 'GET',
    dataType: 'json',
    success: $.proxy(function(result) {
        this.application.notification.showMessage('Job is being processed.');
    }, this),
    error: $.proxy(function(result) {
        console.error(result);
    }, this)
});

回答by linkmarine

Probably I'm late for the party guys, but there are several options i can think of. When user clicks button from the front-end you can give attribute disabled to it and some text like 'processing'. Then you can:

可能我参加派对迟到了,但我能想到几个选择。当用户从前端单击按钮时,您可以为其设置禁用属性和一些文本,如“处理”。然后你可以:

  1. Just ping your endpoint to check if what job is performing is finished
  2. Use websockets
  1. 只需 ping 您的端点即可检查正在执行的作业是否已完成
  2. 使用网络套接字

I think Forge is doing websockets using pusher the endpoint to see if server is active when you are trying to deploy new code. I can clearly see the communication if you open Devtools->Resources->Sockets.

我认为 Forge 正在使用 pusher 端点执行 websockets,以查看当您尝试部署新代码时服务器是否处于活动状态。如果你打开 Devtools->Resources->Sockets,我可以清楚地看到通信。

回答by Amirmasoud

You can use queue events, Laravel document explains it: https://laravel.com/docs/5.6/queues#job-events

你可以使用队列事件,Laravel 文档解释了它:https://laravel.com/docs/5.6/queues#job-events

回答by Leandro Castro

You can user Queue::after function on your AppServiceProvider

您可以在 AppServiceProvider 上使用 Queue::after 函数

Import this dependencies

导入这个依赖

use Illuminate\Support\Facades\Queue;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;

And on boot method you would use it

在引导方法中,您将使用它

public function boot()
    {
        Queue::before(function (JobProcessing $event) {
            // $event->connectionName
            // $event->job
            // $event->job->payload()
        });

        Queue::after(function (JobProcessed $event) {
            // $event->connectionName
            // $event->job
            // $event->job->payload()
        });
    }

回答by Jobin Jose

Job Completion Event

作业完成事件

The Queue::aftermethod allows you to register a callback to be executed when a queued job executes successfully. This callback is a great opportunity to perform additional logging, queue a subsequent job, or increment statistics for a dashboard.

Queue::after方法允许您注册在排队作业成功执行时要执行的回调。此回调是执行额外日志记录、将后续作业排队或增加仪表板统计信息的绝佳机会。

This already in the page link you shared from Laravel.

这已经在您从Laravel共享的页面链接中。