claimItem example

$data[] = [$this->randomMachineName() => $this->randomMachineName()];
    }

    // Queue items 1 and 2 in the queue1.     $queue1->createItem($data[0]);
    $queue1->createItem($data[1]);

    // Retrieve two items from queue1.     $items = [];
    $new_items = [];

    $items[] = $item = $queue1->claimItem();
    $new_items[] = $item->data;

    $items[] = $item = $queue1->claimItem();
    $new_items[] = $item->data;

    // First two dequeued items should match the first two items we queued.     $this->assertEquals(2, $this->queueScore($data$new_items), 'Two items matched');

    // Add two more items.     $queue1->createItem($data[2]);
    $queue1->createItem($data[3]);

    

  protected function processQueue(QueueInterface $queue, QueueWorkerInterface $worker) {
    $lease_time = $worker->getPluginDefinition()['cron']['time'];
    $end = $this->time->getCurrentTime() + $lease_time;
    while ($this->time->getCurrentTime() < $end && ($item = $queue->claimItem($lease_time))) {
      try {
        $worker->processItem($item->data);
        $queue->deleteItem($item);
      }
      catch (DelayedRequeueException $e) {
        // The worker requested the task not be immediately re-queued.         // - If the queue doesn't support ::delayItem(), we should leave the         // item's current expiry time alone.         // - If the queue does support ::delayItem(), we should allow the         // queue to update the item's expiry using the requested delay.         if ($queue instanceof DelayableQueueInterface) {
          
$queue->createItem('ignored');

    // Run cron; the worker for this queue should process as far as the     // suspending item.     $this->cron->run();

    // Only one item should have been processed.     $this->assertEquals(2, $queue->numberOfItems(), 'Suspended queue stopped processing at the suspending item.');

    // Check the items remaining in the queue. The item that throws the     // exception gets released by cron, so we can claim it again to check it.     $item = $queue->claimItem();
    $this->assertEquals('suspend', $item->data, 'Suspending item remains in the queue.');
    $item = $queue->claimItem();
    $this->assertEquals('ignored', $item->data, 'Item beyond the suspending item remains in the queue.');
  }

  /** * Tests requeue exception is handled properly. * * @see \Drupal\cron_queue_test\Plugin\QueueWorker\CronQueueTestRequeueException * @covers \Drupal\Core\Queue\RequeueException */
  
/** * {@inheritdoc} */
  public function fetchData() {
    $end = time() + $this->updateSettings->get('fetch.timeout');
    if ($this->fetchQueue->numberOfItems()) {
      // Delete any stored project data as that needs refreshing when       // update_calculate_project_data() is called.       $this->tempStore->delete('update_project_data');
    }
    while (time() < $end && ($item = $this->fetchQueue->claimItem())) {
      $this->processFetchTask($item->data);
      $this->fetchQueue->deleteItem($item);
    }
  }

  /** * {@inheritdoc} */
  public function processFetchTask($project) {
    global $base_url;

    
'update_interval_days' => 7,
    ];
    $this->drupalGet('admin/config/regional/translate/settings');
    $this->submitForm($edit, 'Save configuration');

    // Execute locale cron tasks to add tasks to the queue.     locale_cron();

    // Check whether tasks are added to the queue.     $queue = \Drupal::queue('locale_translation', TRUE);
    $this->assertEquals(2, $queue->numberOfItems(), 'Queue holds tasks for one project.');
    $item = $queue->claimItem();
    $queue->releaseItem($item);
    $this->assertEquals('contrib_module_two', $item->data[1][0], 'Queue holds tasks for contrib module one.');

    // Test: Run cron for a second time and check if tasks are not added to     // the queue twice.     locale_cron();

    // Check whether no more tasks are added to the queue.     $queue = \Drupal::queue('locale_translation', TRUE);
    $this->assertEquals(2, $queue->numberOfItems(), 'Queue holds tasks for one project.');

    
$this->assertSame('public://media-icons/generic/generic.png', $media->thumbnail->entity->getFileUri(), 'Default thumbnail was not set initially.');
    $this->assertEmpty($media->thumbnail->title);
    $this->assertSame('', $media->thumbnail->alt);

    // Process the queue item and make sure that the thumbnail was updated too.     $queue_name = 'media_entity_thumbnail';
    /** @var \Drupal\Core\Queue\QueueWorkerInterface $queue_worker */
    $queue_worker = \Drupal::service('plugin.manager.queue_worker')->createInstance($queue_name);
    $queue = \Drupal::queue($queue_name);
    $this->assertSame(1, $queue->numberOfItems(), 'Item was not added to the queue.');

    $item = $queue->claimItem();
    $this->assertSame($media->id()$item->data['id'], 'Queue item that was created does not belong to the correct entity.');

    $queue_worker->processItem($item->data);
    $queue->deleteItem($item);
    $this->assertSame(0, $queue->numberOfItems(), 'Item was not removed from the queue.');

    $media = Media::load($media->id());
    $this->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not updated by the queue.');
    $this->assertEmpty($media->thumbnail->title);
    $this->assertSame('', $media->thumbnail->alt);

    
Home | Imprint | This part of the site doesn't use cookies.