Search
Social Media Image Kit

Social Media Image Kit

Using a Job Queue

Social Media Image Kit can be configured to process image rendering using a job queue. The typical setup for this scenario will involve having a database available, or some other asynchronous method of processing jobs.

Before continuing, it is recommended that you are familiar with how Laravel's queue system works in general. For more information, you may consult their official documentation at https://laravel.com/docs/10.x/queues.

#Configuring the Queue

You may configure the queue Social Media Image Kit should use by modifying the queue.php configuration file:

1<?php
2 
3return [
4 
5 // ...
6 
7 /*
8 |--------------------------------------------------------------------------
9 | Queue Configuration for Image Jobs
10 |--------------------------------------------------------------------------
11 |
12 | The 'Queue' configuration tailors how social media image jobs are queued,
13 | enabling the choice of a specific driver or queue name for dispatching.
14 |
15 */
16 
17 'queue' => [
18 'connection' => config('queue.default', 'sync'),
19 'name' => 'default',
20 ],
21 
22 // ...
23];

You can adjust the following configuration values:

  • connection: The queue connection. This will use your project's default queue connection by default.

  • name: You may specify a custom queue name to adjust where jobs are dispatched and processed.

After you've setup your queue information you must enable events in order for jobs to be added to the queue. Social Media Image Kit's event listeners will create jobs to update your images whenever an entry in a configured collection is created or updated.

#Enabling Queue Event Listeners

To enable Social Media Image Kit's queue events you may update the events_enabled value within the queue.php configuration file:

1<?php
2 
3return [
4 
5 // ...
6 
7 /*
8 |--------------------------------------------------------------------------
9 | Auto-Generation of Images on Events
10 |--------------------------------------------------------------------------
11 |
12 | The events_enabled option determines if images should be auto-generated
13 | by the addon when entries are saved or created. Set to true to enable
14 | automatic creation upon these events. When disabled, you will need
15 | to manually generate images using the provided Artisan commands
16 | or some other process if data within the entry has changed and
17 | you wish to update the social media images for the entry.
18 |
19 */
20 
21 'events_enabled' => false,
22 
23 // ...
24];

Set this configuration value to true in order to have Social Media Image Kit response to Statamic's entry creation and update events.

#Preventing Duplicate Jobs

When alerts are enabled it is possible for many jobs to be created if content editors save their changes frequently while editing. To help reduce the total number of image generation jobs created, Social Media Image Kit provides a feature to prevent duplicate jobs.

This feature relies on a database connection in order to work. The database details can be updated within the queue.php configuration file:

1<?php
2 
3return [
4 
5 // ...
6 
7 /*
8 |--------------------------------------------------------------------------
9 | Database Configuration for Social Media Image Tasks
10 |--------------------------------------------------------------------------
11 |
12 | Some features, such as preventing duplicate jobs, requires a database
13 | connection. The following configuration can be used to change the
14 | database connection name, as well as the database table used.
15 |
16 */
17 
18 'database' => [
19 'connection' => config('database.default', 'mysql'),
20 'table' => 'social_media_image_tasks',
21 ],
22 
23 // ...
24];

After you have updated your database configuration, you will need to create the social_media_image_tasks database table. You can create a migration for this by running the following command from the root of your project:

1php artisan social-media-image-kit:image-tasks-table

Once the migration has been created you can add it to your database using the migration deployment process of your choice.

After both the queue and database configuration values have been updated, you can enable the prevent_duplicate_jobs feature within the queue.php configuration file:

1<?php
2 
3return [
4 
5 // ...
6 
7 
8 /*
9 |--------------------------------------------------------------------------
10 | Prevent Duplicate Jobs Configuration
11 |--------------------------------------------------------------------------
12 |
13 | Enabling 'Prevent Duplicate Jobs' stops the creation of multiple jobs for
14 | the identical entry, crucial when edits are frequent and saves are many.
15 | This setting relies on a database and an asynchronous queue system.
16 |
17 */
18 
19 'prevent_duplicate_jobs' => false,
20 
21 // ...
22];

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.