Problem/Motivation

Follow-up to #2704543: Implement cron queue processing.

The issue added a function in the module as a quick solution. It would be preferable to move it into a service instead, but that means we need support for calling services.

We need something like \Drupal\Core\Controller\ControllerResolver::createController for that, then specify it as service.name:methodName.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

Berdir created an issue. See original summary.

abhishek-anand’s picture

Assigned: Unassigned » abhishek-anand
abhishek-anand’s picture

Added a service for queueCallback

Modified CronJob to detect a callback as service and resolve it into a callable

TODO: inject ClassResolverInterface into CronJob

berdir’s picture

Status: Active » Needs work

Looks like you're on track.

  1. +++ b/src/CronJobDiscovery.php
    @@ -68,7 +68,7 @@ class CronJobDiscovery {
    -          'callback' => 'ultimate_cron_queue_callback',
    +          'callback' => 'ultimate_cron.queue_manager.createController',
               'scheduler' => [
    

    should be :, not .?

  2. +++ b/src/QueueManagerInterface.php
    @@ -0,0 +1,19 @@
    + * @package Drupal\ultimate_cron
    + */
    +interface QueueManagerInterface {
    +
    +  public function queueCallback(CronJobInterface $job);
    +
    

    not sure if we really need an interface for this.

    Naming should be QueueWorkerInterface or so, not Manager.

abhishek-anand’s picture

Status: Needs work » Needs review
StatusFileSize
new10.55 KB
new11.72 KB

Status: Needs review » Needs work

The last submitted patch, 5: 2728873-cron_job_service_as_callback-5.patch, failed testing.

abhishek-anand’s picture

Status: Needs work » Needs review
StatusFileSize
new10.54 KB
new560 bytes
berdir’s picture

Status: Needs review » Needs work

Thanks, functionality looks good, needs a bit of cleanup/docs.

  1. +++ b/src/Entity/CronJob.php
    @@ -7,6 +7,8 @@
     use Drupal\Core\Config\Entity\ConfigEntityBase;
    +use Drupal\Core\DependencyInjection\ClassResolver;
    +use Drupal\Core\DependencyInjection\ClassResolverInterface;
     use Drupal\Core\Entity\EntityStorageInterface;
     use Drupal\Core\Logger\RfcLogLevel;
    

    Are those actually used?

  2. +++ b/src/QueueWorker.php
    @@ -0,0 +1,131 @@
    +/**
    + * @file
    + * Contains \Drupal\ultimate_cron\QueueWorker.
    + */
    

    not needed anymore.

  3. +++ b/src/QueueWorker.php
    @@ -0,0 +1,131 @@
    +/**
    + * Class QueueWorker.
    + *
    + * @package Drupal\ultimate_cron
    + */
    +class QueueWorker {
    

    We don't use @package, should have an actual description.

  4. +++ b/src/QueueWorker.php
    @@ -0,0 +1,131 @@
    +  /**
    +   * Drupal\Core\Queue\QueueWorkerManager definition.
    +   *
    +   * @var Drupal\Core\Queue\QueueWorkerManager
    +   */
    +  protected $plugin_manager_queue_worker;
    

    the first line seems a bit pointless, repeates the class name and it's not a definition. I personally am OK with just leaving out the description, just keep the @var. Or add a proper description like "Queue worker plugin manager". Same for those below.

  5. +++ b/src/QueueWorker.php
    @@ -0,0 +1,131 @@
    +  protected $config_factory;
    +  /**
    +   * Constructor.
    +   */
    +  public function __construct(QueueWorkerManager $plugin_manager_queue_worker, QueueFactory $queue, ConfigFactory $config_factory) {
    

    missing empty line, constructurs usually have @param docs for their arguments, but I can live with them not fully defined.

  6. +++ b/src/QueueWorker.php
    @@ -0,0 +1,131 @@
    +  }
    +  ¶
    +  public function queueCallback(CronJobInterface $job) {
    

    missing docblock.

  7. +++ b/ultimate_cron.module
    @@ -188,89 +188,6 @@ function ultimate_cron_cron() {
     /**
    - * Cron callback for queue worker cron jobs.
    - */
    -function ultimate_cron_queue_callback(CronJobInterface $job) {
    -  $queue_name = str_replace(CronJobInterface::QUEUE_ID_PREFIX, '', $job->id());
    

    There should be some unused use statements now in this file.

berdir’s picture

abhishek-anand’s picture

Status: Needs work » Needs review
StatusFileSize
new14.01 KB
new7.22 KB

Fixed the above issues.

berdir’s picture

Status: Needs review » Needs work

Needs a reroll

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new13.94 KB

Rerolled.

  • Berdir committed 21ef742 on 8.x-2.x authored by abhishek-anand
    Issue #2728873 by abhishek-anand, Berdir: Ensure that cron jobs can have...
berdir’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

jatinkumar1989’s picture

Hi,

I updated my module, but getting this error

drupal core: drupal 8.4.2
ultimate_cron version: '8.x-2.0-alpha2'

Warning: call_user_func() expects parameter 1 to be a valid callback, function 'ultimate_cron_queue_callback' not found or invalid function name in Drupal\ultimate_cron\Entity\CronJob->invokeCallback() (line 316 of /var/www/XXXXX/docroot/modules/contrib/ultimate_cron/src/Entity/CronJob.php)

any idea why i am getting this warning in logs.
Thanks in advance

sokru’s picture

I'll get same error with latest -dev version. Warning: call_user_func() expects parameter 1 to be a valid callback, function 'ultimate_cron_queue_callback' not found or invalid function name in Drupal\ultimate_cron\Entity\CronJob->invokeCallback() (line 316

sokru’s picture

In my case it was missing module.

Way to reproduce:
1) Install update module and ultimate_cron, creates ultimate_cron.job.update_cron
2) Uninstall update module, but do not manually remove this cron job from UI.
3) In 15 minutes you'll get error mentioned.

fdverwoerd’s picture

I got the same error as in #17, but somehow when I added depencies in .info.yml to the module containing the ultimate cron callback it started pickin it up (so it was already installed)? So adding a decency like `custom_module:custom_module` made sure the callback was found. Something when callback is a service perhaps? I don't understand it.

rahul_’s picture

#17 Same issue I am facing in cron job log,

I have just disable the cron job for a moment and once again enable, and this error facing in log.

Could you please suggest me the solution for it.

Thanks in advance.