Created
October 24, 2016 15:06
-
-
Save appkr/c95be939b61d5186579c904e871bc593 to your computer and use it in GitHub Desktop.
Queue demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # $ touch database/database.sqlite | |
| # $ php artisan queue:table | |
| # $ php artisan queue:failed-table | |
| # $ php artisan migrate | |
| # $ php artisan make:job DemoJob | |
| # $ php artisan tinker | |
| # >>> factory(App\User::class)->create(); | |
| <?php | |
| // routes/web.php | |
| Route::get('/', function () { | |
| $user = App\User::find(1); | |
| dispatch(new \App\Jobs\DemoJob($user)); | |
| return; | |
| }); | |
| // app/Jobs/DemoJob.php | |
| namespace App\Jobs; | |
| use App\User; | |
| use Illuminate\Bus\Queueable; | |
| use Illuminate\Queue\SerializesModels; | |
| use Illuminate\Queue\InteractsWithQueue; | |
| use Illuminate\Contracts\Queue\ShouldQueue; | |
| class DemoJob implements ShouldQueue | |
| { | |
| use InteractsWithQueue, Queueable, SerializesModels; | |
| /** | |
| * @var User | |
| */ | |
| private $user; | |
| /** | |
| * Create a new job instance. | |
| * | |
| * @return void | |
| */ | |
| public function __construct(User $user) | |
| { | |
| $this->user = $user; | |
| } | |
| /** | |
| * Execute the job. | |
| * | |
| * @return void | |
| */ | |
| public function handle() | |
| { | |
| \Log::info([__METHOD__, $this->user->name]); | |
| } | |
| } | |
| //[2016-10-24 13:48:58] local.INFO: array ( | |
| // 0 => 'App\\Jobs\\DemoJob::handle', | |
| // 1 => 'Maiya Romaguera V', | |
| //) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment