{"componentChunkName":"component---src-templates-post-jsx","path":"/en/queue-api","result":{"data":{"markdownRemark":{"html":"<h1>Queue API: Efficient Asynchronous Task Management</h1>\n<p>In today's digital world, web applications often need to perform long-running or intensive background tasks, such as processing large data volumes, sending bulk emails, or regularly updating content. To handle these tasks efficiently without impacting website performance, Drupal provides the Queue API. In this article, we will explore what the Queue API is in Drupal, how it works, and how it can be used to improve the performance and reliability of Drupal sites.</p>\n<h2>What is the Queue API in Drupal?</h2>\n<p>The Queue API is a mechanism built into Drupal that allows deferring the execution of certain tasks so they can be processed asynchronously, outside the main HTTP request processing flow. Rather than immediately executing a heavy or intensive task when triggered, Drupal adds it to a queue where it will be processed later by a separate or parallel process.</p>\n<h2>How the Queue API Works</h2>\n<p>The Queue API uses a system of \"workers\" to process queue items. These workers can be separate background processes, scheduled tasks run by Drupal's cron system, or even external workers integrated with other systems or services. When a worker processes a queue item, it performs the associated task and removes the item from the queue.</p>\n<h2>Advantages of the Queue API</h2>\n<p>The Queue API offers several important advantages for Drupal site development and operation:</p>\n<h3>1. Improved Performance</h3>\n<p>By moving heavy or long tasks to a queue, Drupal can continue responding quickly to user requests without being slowed down by processing those tasks.</p>\n<h3>2. Reliability and Fault Tolerance</h3>\n<p>By processing tasks asynchronously, Drupal becomes more resilient to failures and overloads. Even in the event of errors or malfunctions, tasks remain in the queue and can be retried later.</p>\n<h3>3. Extensibility</h3>\n<p>The Queue API is designed to be extensible and customizable, allowing developers to add custom workers for specific types of tasks or to integrate external services for queue processing.</p>\n<h2>Using the Queue API</h2>\n<p>Here is a simple example of how to use the Queue API in Drupal to add items to a queue and process them using a custom worker:</p>\n<p>Suppose we want to create a queue to handle sending emails in the background. Here is how we could do it:</p>\n<ul>\n<li>Define a custom queue in your module:</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token keyword\">use</span> <span class=\"token package\">Drupal<span class=\"token punctuation\">\\</span>Core<span class=\"token punctuation\">\\</span>Queue<span class=\"token punctuation\">\\</span>QueueInterface</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">use</span> <span class=\"token package\">Drupal<span class=\"token punctuation\">\\</span>Core<span class=\"token punctuation\">\\</span>Queue<span class=\"token punctuation\">\\</span>QueueWorkerBase</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">/**\n * Implements hook_queue_info().\n */</span>\n<span class=\"token keyword\">function</span> <span class=\"token function\">my_module_queue_info</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token variable\">$queues</span><span class=\"token punctuation\">[</span><span class=\"token single-quoted-string string\">'my_module_email_queue'</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">array</span><span class=\"token punctuation\">(</span>\n    <span class=\"token single-quoted-string string\">'title'</span> <span class=\"token operator\">=</span><span class=\"token operator\">></span> <span class=\"token function\">t</span><span class=\"token punctuation\">(</span><span class=\"token single-quoted-string string\">'Email queue for My Module'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n    <span class=\"token single-quoted-string string\">'worker callback'</span> <span class=\"token operator\">=</span><span class=\"token operator\">></span> <span class=\"token single-quoted-string string\">'my_module_process_email_queue'</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">return</span> <span class=\"token variable\">$queues</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">/**\n * Worker callback function to process email queue items.\n */</span>\n<span class=\"token keyword\">function</span> <span class=\"token function\">my_module_process_email_queue</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$data</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// Process email data here (send email, update database, etc.).</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<ul>\n<li>Add items to the queue at the appropriate point in your code:</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token keyword\">use</span> <span class=\"token package\">Drupal<span class=\"token punctuation\">\\</span>Core<span class=\"token punctuation\">\\</span>Queue<span class=\"token punctuation\">\\</span>QueueFactory</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// Get the queue service.</span>\n<span class=\"token variable\">$queue</span> <span class=\"token operator\">=</span> \\<span class=\"token package\">Drupal</span><span class=\"token punctuation\">:</span><span class=\"token punctuation\">:</span><span class=\"token function\">service</span><span class=\"token punctuation\">(</span><span class=\"token single-quoted-string string\">'queue'</span><span class=\"token punctuation\">)</span><span class=\"token operator\">-</span><span class=\"token operator\">></span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token single-quoted-string string\">'my_module_email_queue'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// Add an item to the queue.</span>\n<span class=\"token variable\">$queue</span><span class=\"token operator\">-</span><span class=\"token operator\">></span><span class=\"token function\">createItem</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$email_data</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<ul>\n<li>Make sure to start workers to process queue items. You can do this by running the following Drush command:</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">drush queue:run my_module_email_queue</code></pre></div>\n<ul>\n<li>To list all available queues:</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">drush queue:list</code></pre></div>\n<h2>Conclusion</h2>\n<p>The Queue API is a powerful tool for efficiently managing asynchronous tasks in Drupal, enabling websites to handle intensive processing reliably and performantly. By moving heavy tasks into a queue, Drupal can provide a better user experience while improving fault tolerance and scalability. Whether for one-off operations or complex workflows, the Queue API offers a flexible and robust solution for Drupal developers.</p>","excerpt":"Queue API: Efficient Asynchronous Task Management In today's digital world, web applications often need to perform long-running or intensive background tasks…","frontmatter":{"date":"2022-10-11","metaDate":"2022-10-11","title":"Queue API","tags":["Drupal 9","Drupal 10","backend","Module development"],"path":"/queue-api","cover":{"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAHABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAED/8QAFgEBAQEAAAAAAAAAAAAAAAAAAAME/9oADAMBAAIQAxAAAAHSGqUB/8QAGBAAAgMAAAAAAAAAAAAAAAAAAQIAESH/2gAIAQEAAQUCm0ykn//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABYQAAMAAAAAAAAAAAAAAAAAAAAQMf/aAAgBAQAGPwJU/8QAGRABAAIDAAAAAAAAAAAAAAAAAQARITFR/9oACAEBAAE/IWxN5e6lYDkvIJ//2gAMAwEAAgADAAAAEIw//8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQMBAT8QV//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EABkQAQADAQEAAAAAAAAAAAAAAAEAESFhcf/aAAgBAQABPxBDWg/CogLFqa92JiBDKn//2Q==","aspectRatio":2.995319812792512,"src":"/static/568b9a02f30f1dce6d5e0618a697a9c9/88110/picture.jpg","srcSet":"/static/568b9a02f30f1dce6d5e0618a697a9c9/0b320/picture.jpg 480w,\n/static/568b9a02f30f1dce6d5e0618a697a9c9/60b32/picture.jpg 960w,\n/static/568b9a02f30f1dce6d5e0618a697a9c9/88110/picture.jpg 1920w","srcWebp":"/static/568b9a02f30f1dce6d5e0618a697a9c9/d1a9d/picture.webp","srcSetWebp":"/static/568b9a02f30f1dce6d5e0618a697a9c9/bc3bf/picture.webp 480w,\n/static/568b9a02f30f1dce6d5e0618a697a9c9/39337/picture.webp 960w,\n/static/568b9a02f30f1dce6d5e0618a697a9c9/d1a9d/picture.webp 1920w","sizes":"(max-width: 1920px) 100vw, 1920px"},"resize":{"src":"/static/568b9a02f30f1dce6d5e0618a697a9c9/c4f3a/picture.jpg"}}}}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"pathSlug":"/queue-api","locale":"en","prev":{"fields":{"locale":"en"},"frontmatter":{"path":"/middleware-api","title":"Middleware API","tags":["Drupal 9","Core feature","Performance","Module development"]}},"next":{"fields":{"locale":"en"},"frontmatter":{"path":"/single-directory-component","title":"Drupal Single Directory Component (SDC)","tags":["Drupal 10","Drupal 11","News","UX","UI","Theming"]}}}}}