0

I’m new to Laravel, and tried to create my first background task.

Used docs: https://laravel.com/docs/master/queues

Job: (ProcessDatabaseImport.php)

<?php

namespace AppJobs;

use AppContact;
use IlluminateBusQueueable;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateSupportFacadesLog;
use IlluminateSupportFacadesFile;

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);
    }
}
?>

JobController.php:

<?php

namespace AppHttpControllers;

use AppJobsProcessDatabaseImport;
use CarbonCarbon;
use IlluminateContractsQueueJob;
use IlluminateSupportFacadesQueue;

class JobController extends Controller
{
    /**
     * Handle Queue Process
     */
    public function processQueue()
    {
        ProcessDatabaseImport::dispatch('database/data/contacts.json')->delay(now()->addMinutes(2));

        return view('home');
    }
}

Jobs table is created, php artisan queue:work is running.

Now, when i go to the controller action in my browser, the code in “handle()” is exectuted twice, directly:

Log:

[2019-07-14 13:39:17] local.INFO: Hello world! file: B:hellodialogsollicitatieopdrachtdatabase/data/contacts.json  
[2019-07-14 13:39:18] local.INFO: Hello world! file: B:hellodialogsollicitatieopdrachtdatabase/data/contacts.json  

The database table for jobs is always empty.

In my .env file:

DB_CONNECTION=mysql
QUEUE_CONNECTION=database

queue.php config file:

return [


    'default' => env('QUEUE_CONNECTION', 'database'),

    'connections' => [

        'sync' => [
            'driver' => 'sync',
        ],

        'database' => [
            'driver' => 'database',
            'table' => 'jobs',
            'queue' => 'default',
            'retry_after' => 90,
        ],

        'beanstalkd' => [
            'driver' => 'beanstalkd',
            'host' => 'localhost',
            'queue' => 'default',
            'retry_after' => 90,
            'block_for' => 0,
        ],

        'sqs' => [
            'driver' => 'sqs',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
            'queue' => env('SQS_QUEUE', 'your-queue-name'),
            'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => env('REDIS_QUEUE', 'default'),
            'retry_after' => 90,
            'block_for' => null,
        ],

    ],

    'failed' => [
        'database' => env('DB_CONNECTION', 'mysql'),
        'table' => 'failed_jobs',
    ],

Is something missing?

Update: The job is added to the queue when I changed the default in queue.php

The job is now added twice to the database. I run this script by visiting the URL in my browser.