0

I developed a custom module which will process my items in queue. I have some custom table and custom action buttons to Process queue items. When i click on "Process Queue" button it will call the below function which will work as expected. Here i am calling claimItem function so that i am getting item_id value in processItem function.

Another Scenario is , I installed queue_ui module which is displaying all the queue items perfectly but when i click on Batch process it is giving error as i am running sql query based on item_id.

My point is i need to run queue by using custom module as well as queue_ui module , why because my function will call only when button is clicked. Is there any way to call same function on batchprocess as well as custom action button? Or is there any way to get the item_id value in processItem function.

    public function processQueue() {

   /** @var QueueInterface $queue */
    $queue = $this->queueFactory->get('my_queue');
    /** @var QueueWorkerInterface $queue_worker */
    $queue_worker = $this->queueManager->createInstance('my_queue');

    while($item = $queue->claimItem()) {
      try {
        $queue_worker->processItem($item);

        $queue->deleteItem($item);

      }
      catch (SuspendQueueException $e) {
        $queue->releaseItem($item);
        break;
      }
      catch (\Exception $e) {
        watchdog_exception('my_queue', $e);
      }
    }

  }

In My Drupal\my_queue\Plugin\QueueWorker\MyWorker.php file processItem function will be like this.

  public function processItem($item) {
     try{
      $query = \Drupal::database()->select('my_queue', 'emp');
      $query->fields('emp', ['status'])->condition('emp.queue_id', $item->item_id, '=');
      $result = $query->execute()->fetchField();
      if($result=='failed' || $result=='')
         {
         //here do some operations
         }
         }
         }

I tried below method in processItem function like below

$myclaim=new DatabaseQueue;
      $myclaim->claimItem();

But i got error as follows..

TypeError: Argument 2 passed to Drupal\Core\Queue\DatabaseQueue::__construct() must be an instance of Drupal\Core\Database\Connection, none given, called in /var/www/html/d8/modules/custom/my_queue/src/Plugin/QueueWorker/QueueBase.php on line 49 in Drupal\Core\Queue\DatabaseQueue->__construct() (line 45 of /var/www/html/d8/core/lib/Drupal/Core/Queue/DatabaseQueue.php).

I tried another way as well like below

$equery = \Drupal::database()->select('queue', 'emp');
      $equery->fields('emp', ['data','item_id'])->condition('emp.expire', 0, '=')->condition('emp.name', 'my_queue', '=');
      $eresult = $equery->execute()->fetchObject();

when i print_r($eresult); it is printing empty but same query executed in phpmyadmin and got the result as well. I don't know whats wrong with my query here.

My my_queue.module file is like this. I am calling my controller function on cron run to process the queue.

<?php
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\my_queue\Controller\MyQueueController;

/**
 * Implements hook_help().
 */
function my_queue_cron() {

  // Remove 6 months old entries from custom table.
   MyQueueController::processQueue();


}

Here i am getting error like

Error: Using $this when not in object context in Drupal\my_queue\Controller\MyQueueController::processQueue()
  • You cannot, as when the queue items are handled, Drupal could not even be rendering a page, as during cron tasks. – kiamlaluno Oct 11 '19 at 12:38
  • I want to get item_id of queue item in processItem function , How can i get it? I want to update my custom table on processing of each item. – user3463733 Oct 12 '19 at 18:05
  • In processItem function i am calling third party API , if api is failed then i need to execute the same queue item. How can i do it? – user3463733 Oct 16 '19 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.