我是 Laravel 的新手,并尝试创建我的第一个后台任务。
使用的文档:https : //laravel.com/docs/master/queues
工作:(ProcessDatabaseImport.php)
<?php
namespace App\Jobs;
use App\Contact;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\File;
class ProcessDatabaseImport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $file;
/**
* Create a new job instance.
*
* @param String $filePath
* @return void
*/
public function __construct($filePath)
{
// init File object "database/data/contacts.json"
$this->file = base_path($filePath);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info('Hello world! file: '.$this->file);
}
/**
* Determine the time at which the job should timeout.
*
* @return \DateTime
*/
public function retryUntil()
{
return now()->addSeconds(30);
}
}
?>
作业控制器.php:
<?php
namespace App\Http\Controllers;
use App\Jobs\ProcessDatabaseImport;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Support\Facades\Queue;
class JobController extends Controller
{
/**
* Handle Queue Process
*/
public function processQueue()
{
ProcessDatabaseImport::dispatch('database/data/contacts.json')->delay(now()->addMinutes(2));
return view('home');
}
}
作业表已创建,php artisan queue:work正在运行。
幕布斯7119047