【Drupal】QueueWorkerのキューに蓄積されているキューアイテムを一度のCronですべて実行する

こちらのサイトではDRUPAL10に関連する記事を掲載しています。

Manage The Number of Items Processed by a QueueWorker
I have a queueWorker plugin in a custom module. QueueWorker is using the content of queue items to send emails. One item = one email. It's very convinient and i...

DrupalのQueueWorkerの仕様は以下のようになっています。

  • queueWorker は一度に 1 つのキュー項目のみを処理します。
  • queueWorker は、パラメータで設定した制限時間まで、何度でも実行されます。

現状、一度のCronで実行されるキュー項目は一つだけです。複数の項目をすべて実行することはできますか。

はい、特定の QueueWorker が各 cron で処理するアイテムの数を設定することができます。
ようやくこの方法で成功しました。 とてもシンプルです:

Drupal Core では、cron キューの処理は、と呼ばれるネイティブ コア サービスによって管理されます。 cron.
このサービスのクラスは次のとおりです core/lib/Drupal/Core/Cron.phpファイル。
モジュール内で代替を作成し、ニーズに合わせて調整するだけです。
」を少し 「 Drupal alter core servicesグーグルで調べれば簡単です。

ステップ 1 : モジュールにカスタム サービスを追加する必要があります。 mymodule.services.ymlこのような。

cron:
   class: 'Drupal\mymodule\MymoduleServices\MyModuleCron'
   arguments: ['@module_handler', '@lock', '@queue', '@state', '@account_switcher', '@logger.channel.cron', '@plugin.manager.queue_worker']
   lazy: true

このサービスは「」と呼ばれているので、 cron「ネイティブを完全にオーバーライドします」 cron「cronプロセス内で

ステップ 2:独自のカスタム クラスを埋め込むファイルを追加する

mymodule/src/MyModuleServices/ファイルを追加します MyModuleCron.php.
このファイルでは、ネイティブのコンテンツをコピーします。 core/lib/Drupal/Core/Cron.phpファイル。

ステップ 3:ファイルの内容を次のように調整します。

<?php
/**
* Content is a nearly a copy of the native file core/lib/Drupal/Core/Cron.php
* see notes
*
*/
// Native line: namespace Drupal\Core;
namespace Drupal\node2notifs\Node2NotifsCron; //my new namespace

// ADDED LINE
use Drupal\Core\CronInterface; 

// NATIVE LINES
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\Timer;
// ......
// NATIVE CONTENT
// .....

/**
* Processes cron queues. Here you can adapt and set the number of items per cron session for your queue
*/
protected function processQueues() {

\Drupal::logger('mymodule - Cron')->notice('Témoin PAssage dans function processQueues()');//DEBUG

// Grab the defined cron queues.
foreach ($this->queueManager->getDefinitions() as $queue_name => $info) {

\Drupal::logger('Mymodule - Cron')->notice('Function processQueues(): Queue_name = '.$queue_name);//DEBUG

$numberOfItemsOfMyQueue = 0; // ADDED LINE

if (isset($info['cron'])) {
// Make sure every queue exists. There is no harm in trying to recreate
// an existing queue.
$this->queueFactory->get($queue_name)->createQueue();

$queue_worker = $this->queueManager->createInstance($queue_name);
$end = time() + ($info['cron']['time'] ?? 15);
$queue = $this->queueFactory->get($queue_name);
$lease_time = isset($info['cron']['time']) ?: NULL;
while (time() < $end && ($item = $queue->claimItem($lease_time))) {
try {

if($queue_name =='my_queue') { $numberOfItemsOfMyQueue++;} //ADDED LINE

//MODIFIED SEQUENCE FOR MAXI 3 ITEMS of my-queue per CRON SESSION 
if (($queue_name =='my_queue' && $numberOfItemsOfMyQueue <= 3) || $queue_name !='my_queue' ){
\Drupal::logger('MyModule - Cron')->notice('Function processQueues(): Queue_name = '.$queue_name);//DEBUG
$queue_worker->processItem($item->data);
$queue->deleteItem($item);
}

// ......
// NNATIVE CONTENT
// .....
// .....

これはhook_cron()を使わなくても動作しますが、結局のところ、それが良い習慣であるかどうかが心配になります。

  • メンテナンス性。 この観点からは、古き良きhook_cron()の方が優れている可能性があります。
  • cron サービスが変更された Drupal リリースの場合は、結果的にカスタム cron サービスも変更する必要があります。
  • 優先度の高い別のモジュール内の別のカスタム cron サービスが現在のカスタム cron をオーバーライドし、何らかの問題が発生する可能性があります。
  • 他にも望ましくない「副作用」がある可能性があります。 テストが必要です…
このサイトに関するご意見・ご質問はこちらまで

この記事またはDrupalに関するご質問がございましたら、お気軽にお問い合わせください。

タイトルとURLをコピーしました