5
votes

I am using Laravel 5.4 and MySQL in my project.

Remote Server is PHP Server 7

When a user save a record to database, it's also creating a Queue Job to notificate related company officials. My code is in controller file

    foreach($audit->mailusers AS $mailuser){
        $this->dispatch(new SendAuditEmail($audit->id, $mailuser->name, $mailuser->email));
    }

I'm using database for QUEUE DRIVER. I mean it's written in .env file like that:

QUEUE_DRIVER=database

As you know, if you are using "database" instead of "redis" or other queue drivers, there must be "jobs" table in your database:

CREATE TABLE `jobs` (
   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
   `queue` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
   `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
   `attempts` tinyint(3) unsigned NOT NULL,
   `reserved_at` int(10) unsigned DEFAULT NULL,
   `available_at` int(10) unsigned NOT NULL,
   `created_at` int(10) unsigned NOT NULL,
 PRIMARY KEY (`id`),
 KEY `jobs_queue_reserved_at_index` (`queue`,`reserved_at`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 
COLLATE=utf8mb4_unicode_ci;

Also as you know, if you want to handle failed jobs, there should be one more table in your database named "failed_jobs":

CREATE TABLE `failed_jobs` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `connection` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `queue` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `exception` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `failed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

To create "failed_jobs" table (source), firstly you have to run artisan command:

php artisan queue:failed-table

This command will create a migration php file in database\migrations folder

Then by running migrate command, you can create a failed_jobs table:

php artisan migrate

To process jobs in jobs table:

php artisan queue:work

Queue worker act like that:

1- if a job in the "jobs" table is failed, save it to "failed_jobs" table and delete from "jobs" table

2- if a job is successfully completed, just delete from "jobs" table. Not save it again anywhere, just delete this job.

FINALLY, my question is that: If queue worker successfully complete a job, is there anyway to save this job record to any other table in database named like: "completed_jobs".

Because, it is important for me to be able to say like: "Mail to Mr. Anderson is sent by Queue Worker at 25.01.2018 15:42:20"

1
you need to be way more specific in your question. Otherwise you'll get downvotes. Specify data structure and what do you want, take your time to create the post. Additionally you are mixing concepts between FWK commands for creating tables AND domain behaviour for the 'automatically save' (can be code, can be DB triggers)Joako-stackoverflow
is that ok now?AndroCoder
yes!, indeed it isJoako-stackoverflow
If you are asking for behaviour within queue, I'm afraid cannot help you. But as for conventional development is just a database trigger between jobs and the new 'completed_jobs' table. Sorry I cannot be of more assiantaceJoako-stackoverflow

1 Answers

3
votes

Laravel fires job events for before, after, and failing jobs. For your need, you can register a listener on after events and store records of jobs that have successfully ran.

In a service provider's boot method:

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