{"id":57485,"date":"2023-05-29T13:57:18","date_gmt":"2023-05-29T08:27:18","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=57485"},"modified":"2023-05-29T13:59:31","modified_gmt":"2023-05-29T08:29:31","slug":"drupal-9-4-queue-worker-with-batch-processing","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/drupal-9-4-queue-worker-with-batch-processing\/","title":{"rendered":"Drupal 9.4 Queue Worker with Batch Processing"},"content":{"rendered":"<p>In Drupal 9.4, the Queue API has been enhanced to support batch processing. This allows developers to efficiently process a large number of queued items in smaller batches, improving performance and resource utilization. In this blog post, we&#8217;ll explore how to implement a Queue Worker with batch processing in Drupal 9.4 using a practical example.<\/p>\n<h2>Getting Started<\/h2>\n<p>Before we dive into the implementation, let&#8217;s set up a basic module called &#8220;customqueue&#8221; that will contain our queue worker and batch processing code.<\/p>\n<ol>\n<li>Create a new directory called customqueue\u00a0in the <code>modules\/custom<\/code> directory of your Drupal installation.<\/li>\n<li>Inside the customqueue directory, create a new file called customqueue<code>.info.yml<\/code> with the following contents:<\/li>\n<\/ol>\n<p>CODE:<\/p>\n<div>\n<pre>name: 'customqueue'\r\n\r\ntype: module\r\n\r\ndescription: 'Example module for Queue Worker with Batch Processing'\r\n\r\ncore_version_requirement: ^9 || ^10\r\n\r\npackage: Custom\r\n\r\ndependencies:\r\n\r\n- php: ^7.3<\/pre>\n<\/div>\n<div>\n<ol start=\"3\">\n<li>Create another file called customqueue.services.yml with the following contents:<\/li>\n<\/ol>\n<div class=\"bg-black rounded-md mb-4\">\n<pre class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\">services:\r\ncustomqueue.queue_worker:\r\nclass: Drupal\\customqueue\\QueueWorker\\MyCustomQueue\r\narguments: ['@entity_type.manager']\r\ntags:\r\n- { name: queue_worker }<\/pre>\n<\/div>\n<\/div>\n<div>\n<ol start=\"4\">\n<li>Create the necessary directory structure and an empty file for the queue worker class:<\/li>\n<\/ol>\n<ul>\n<li>Create a directory called <code>src<\/code> inside the customqueue\u00a0directory.<\/li>\n<li>Inside the <code>src<\/code> directory, create a directory called <code>QueueWorker<\/code>.<\/li>\n<li>Inside the <code>QueueWorker<\/code> directory, create a file called MyCustomQueue.php<\/li>\n<\/ul>\n<\/div>\n<div>\n<h2>Implementing the Queue Worker<\/h2>\n<p>Now that we have our module set up, let&#8217;s implement the Queue Worker class.<\/p>\n<p>Open the MyCustomQueue.php file and add the following code:<\/p>\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\">\n<pre>&lt;?php\r\n\r\nnamespace Drupal\\customqueue\\QueueWorker;\r\n\r\nuse Drupal\\Core\\Entity\\EntityTypeManagerInterface;\r\nuse Drupal\\Core\\Queue\\QueueWorkerBase;\r\n\r\n\/**\r\n* Processes queued items using batch processing.\r\n*\r\n* @QueueWorker(\r\n* id = \"customqueue_queue_worker\",\r\n* title = @Translation(\"customqueue Queue Worker\"),\r\n* cron = {\"time\" = 60}\r\n* )\r\n*\/\r\nclass MyCustomQueue extends QueueWorkerBase {\r\n\r\n\/**\r\n* The entity type manager.\r\n*\r\n* @var \\Drupal\\Core\\Entity\\EntityTypeManagerInterface\r\n*\/\r\nprotected $entityTypeManager;\r\n\r\n\/**\r\n* Constructs a new MyCustomQueue object.\r\n*\r\n* @param \\Drupal\\Core\\Entity\\EntityTypeManagerInterface $entity_type_manager\r\n* The entity type manager.\r\n*\/\r\npublic function __construct(EntityTypeManagerInterface $entity_type_manager) {\r\n$this-&gt;entityTypeManager = $entity_type_manager;\r\n}\r\n\r\n\/**\r\n* {@inheritdoc}\r\n*\/\r\npublic function processItem($data) {\r\n\/\/ Implement your custom processing logic here.\r\n\/\/ This method will be called for each item in the queue.\r\n\r\n\/\/ Example: Updating an entity field value.\r\n$entity = $this-&gt;entityTypeManager-&gt;getStorage('node')-&gt;load($data['nid']);\r\n$entity-&gt;set('field_status', 'processed');\r\n$entity-&gt;save();\r\n}\r\n\r\n}<\/pre>\n<\/div>\n<p>In this example, we have defined a Queue Worker class called MyCustomQueue. The class extends the <code>QueueWorkerBase<\/code> class provided by Drupal core.<\/p>\n<p>The <code>@QueueWorker<\/code> annotation is used to define the queue worker ID, title, and cron timing. Make sure to update the <code>id<\/code> and <code>title<\/code> values as per your requirements.<\/p>\n<p>The <code>processItem()<\/code> method is called for each item in the queue. Inside this method, you can implement your custom processing logic. In this example, we load a node entity based on the provided <code>nid<\/code> and update a field value (<code>field_status<\/code> in this case).<\/p>\n<h2>Implementing Batch Processing<\/h2>\n<p>To add batch processing to our queue worker, we need to modify the <code>processItem()<\/code> method to utilize Drupal&#8217;s Batch API. The Batch API allows us to process items in smaller batches, reducing the strain on server resources.<\/p>\n<p>Update the <code>processItem()<\/code> method in the MyCustomQueue\u00a0class as follows:<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\">\n<pre>\/**\r\n* {@inheritdoc}\r\n*\/\r\npublic function processItem($data) {\r\n\/\/ Implement your custom processing logic here.\r\n\/\/ This method will be called for each item in the queue.\r\n\r\n\/\/ Example: Updating an entity field value.\r\n$entity = $this-&gt;entityTypeManager-&gt;getStorage('node')-&gt;load($data['nid']);\r\n$entity-&gt;set('field_status', 'processed');\r\n$entity-&gt;save();\r\n\r\n\/\/ Batch processing.\r\n$batch = [\r\n'operations' =&gt; [\r\n[[get_class($this), 'processItem'], [$data]],\r\n],\r\n'finished' =&gt; [get_class($this), 'finishedCallback'],\r\n'title' =&gt; t('Processing queue items...'),\r\n'init_message' =&gt; t('Starting processing...'),\r\n'progress_message' =&gt; t('Processed @current out of @total.'),\r\n];\r\n\r\nbatch_set($batch);\r\n}\r\n\r\n\/**\r\n* Batch processing finished callback.\r\n*\/\r\npublic static function finishedCallback($success, $results, $operations) {\r\nif ($success) {\r\n\/\/ Batch processing completed successfully.\r\n\/\/ Perform any additional actions here.\r\n}\r\nelse {\r\n\/\/ Batch processing failed.\r\n\/\/ Log or handle errors here.\r\n}\r\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\">\n<p>In this updated code, we have added the necessary code for batch processing:<\/p>\n<ul>\n<li>After updating the entity field value, we define a $batch array that contains information about the batch process.<\/li>\n<li>The operations key holds an array of operations to be executed in each batch. In our case, we include the processItem() method itself as the operation and pass the $data parameter to it.<\/li>\n<li>The finished key specifies the callback method to be called when the batch processing is finished. We have defined a finished Callback() method that handles any post-processing actions.<\/li>\n<li>The title, init_message, and progress_message keys define the messages displayed during the batch processing.<\/li>\n<\/ul>\n<p>Finally, we call batch_set() and pass the $batch array to initiate the batch processing.<\/p>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>By utilizing the Queue API and Batch API, you can improve performance and resource utilization when working with large datasets or time-consuming tasks in Drupal. It provides a scalable and efficient way to handle background processing.<\/p>\n<p>Let us know in case of any queries , please feel free to reach out via comments.<\/p>\n<\/div>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In Drupal 9.4, the Queue API has been enhanced to support batch processing. This allows developers to efficiently process a large number of queued items in smaller batches, improving performance and resource utilization. In this blog post, we&#8217;ll explore how to implement a Queue Worker with batch processing in Drupal 9.4 using a practical example. [&hellip;]<\/p>\n","protected":false},"author":1513,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":385},"categories":[3602],"tags":[5245,4862,5152,5246],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57485"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1513"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=57485"}],"version-history":[{"count":5,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57485\/revisions"}],"predecessor-version":[{"id":57515,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57485\/revisions\/57515"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=57485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=57485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=57485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}