Browse Source

Add long private job notifer

pull/1/head
Nafies Luthfi 5 years ago
parent
commit
f914d57042
  1. 39
      app/Events/LongRunPrivateJobDone.php
  2. 35
      app/Jobs/LongRunPrivateJob.php
  3. 2
      config/app.php
  4. 3
      resources/views/home.blade.php
  5. 9
      resources/views/layouts/app.blade.php
  6. 4
      routes/channels.php
  7. 8
      routes/web.php

39
app/Events/LongRunPrivateJobDone.php

@ -0,0 +1,39 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class LongRunPrivateJobDone implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $id;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($id, $message)
{
$this->id = $id;
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('App.User.'.$this->id);
}
}

35
app/Jobs/LongRunPrivateJob.php

@ -0,0 +1,35 @@
<?php
namespace App\Jobs;
use App\Events\LongRunPrivateJobDone;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class LongRunPrivateJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $id;
public function __construct($id)
{
$this->id = $id;
}
public function handle()
{
$seconds = 5;
sleep($seconds);
$link = '<a href="'.route('home').'">here</a>';
event(new LongRunPrivateJobDone(
$this->id,
'Long run private job (for '.$this->id.') done after '.$seconds.' seconds. Please check '.$link.'.')
);
info('Long run private job (for '.$this->id.') done after '.$seconds.' seconds. Please check '.$link.'.');
}
}

2
config/app.php

@ -171,7 +171,7 @@ return [
*/ */
App\Providers\AppServiceProvider::class, App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class, App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class, App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class, App\Providers\RouteServiceProvider::class,

3
resources/views/home.blade.php

@ -5,7 +5,7 @@
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="col-md-8"> <div class="col-md-8">
<div class="card"> <div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-header">{{ __('Dashboard') }} auth user: {{ auth()->id() }}</div>
<div class="card-body"> <div class="card-body">
@if (session('status')) @if (session('status'))
@ -17,6 +17,7 @@
{{ __('You are logged in!') }} {{ __('You are logged in!') }}
<hr> <hr>
<a href="{{ route('long-run-job') }}" class="btn btn-success">Run a long job</a> <a href="{{ route('long-run-job') }}" class="btn btn-success">Run a long job</a>
<a href="{{ route('private-long-run-job') }}" class="btn btn-success">Run a long private job</a>
</div> </div>
</div> </div>
</div> </div>

9
resources/views/layouts/app.blade.php

@ -93,6 +93,15 @@
timeout: false timeout: false
}); });
}); });
Echo.private('App.User.{{ auth()->id() }}')
.listen('LongRunPrivateJobDone', (e) => {
noty({
type: 'success',
layout: 'bottomRight',
text: e.message,
timeout: false
});
});
</script> </script>
@if (Session::has('flash_notification.message')) @if (Session::has('flash_notification.message'))
<script> <script>

4
routes/channels.php

@ -16,7 +16,3 @@ use Illuminate\Support\Facades\Broadcast;
Broadcast::channel('App.User.{id}', function ($user, $id) { Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id; return (int) $user->id === (int) $id;
}); });
Broadcast::channel('queue-notifier', function ($user, $id) {
return true;
});

8
routes/web.php

@ -1,6 +1,7 @@
<?php <?php
use App\Jobs\LongRunJob; use App\Jobs\LongRunJob;
use App\Jobs\LongRunPrivateJob;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
/* /*
@ -29,6 +30,13 @@ Route::get('/long-run-job', function () {
return redirect()->home(); return redirect()->home();
})->name('long-run-job'); })->name('long-run-job');
Route::get('/private-long-run-job', function () {
dispatch(new LongRunPrivateJob(auth()->id()));
flash('Please wait, your request is processing...');
return redirect()->home();
})->name('private-long-run-job');
Auth::routes(); Auth::routes();
Route::get('/home', 'HomeController@index')->name('home'); Route::get('/home', 'HomeController@index')->name('home');
Loading…
Cancel
Save