The patch in #1226040: Beanstalkd lacks options to control TTR of queued jobs allows us to define a default "delay" for a queued item.

I'd like to be able to define the delay on a per-item basis, however the Drupal API for "createItem" doesn't seem to support any way of setting this other than looking for a parameter in $data.

Any thoughts on this appreciated! I'm happy to do the work/roll the patch.

Nick

Comments

gordon’s picture

We can't change the BeanstalkdQueue::createItem() or it is going to break the interface and it will not run.

However the actual Pheanstalk object is "super loaded" onto the standard DrupalQueue object so that you can treat the BeanstalkdQueue as a Pheanstalk object and call put directly.

BeanstalkdQueue::put()

The problem is that the put command the payload will not get converted into the standard format that createItem() will do.

If you want you could add in the functionality to convert the payload to the correct format in the __call() to convert the payload correctly.

Also change the createItem() to call $this->put() so the actual put is in single place, and not duplicated.

nicholasthompson’s picture

I see - that would work quite nicely in terms of code cleanup... so basically:

public function createItem($data) {
  if (!$this->beanstalkd_queue) {
    return FALSE;
  }

  $record = new stdClass();
  $record->name = $this->tube;
  $record->data = $data;
  $record = serialize($record);

  return $this->beanstalkd_queue->put($record, $this->beanstalkd_params['priority'], $this->beanstalkd_params['delay'], $this->beanstalkd_params['ttr']) ? TRUE : FALSE;
}

Would become:

public function createItem($data) {
  if (!$this->beanstalkd_queue) {
    return FALSE;
  }

  return (bool)$this->put($data);
}

Then, in the __call, add a handler for the put command which rebuilds the $record.

The Put handler can expect the parameters in the same order as defined here:
https://github.com/pda/pheanstalk/blob/master/classes/Pheanstalk.php#L261

And if not present, fall back to the $this->beanstalkd_params.

I'll get on making a patch for this.

nicholasthompson’s picture

Status: Needs work » Needs review
StatusFileSize
new5.3 KB

How's about this for a "phase 1" patch?

I moved the "put" handling into the _call handler. This also now supports putInTube too.

This seems to pass the test file too. I will test it with our D7 codebase tomorrow.

Nick

EDIT: This patch also cleans up the way the command groups are handled. There is no longer one for an array of tubes and another for a single tube - it assumes the same functionality for both and builds the $tubes array based in the parameters (if a single item was passed in, $tubes becomes a single item array - otherwise it becomes the array that was passed in as arg 0).

gordon’s picture

Status: Needs review » Needs work
+++ b/beanstalkd.queue.inc
@@ -64,35 +64,61 @@ class BeanstalkdQueue implements DrupalReliableQueueInterface {
+        // Now rebuild argument 1 (which should be $data) into a serialized object
+        $record = (object)array(
+          'name' => $tubes[0], // Tube name has to be the first one
+          'data' => $arguments[1],
+        );
+        $arguments[1] = serialize($record);

I don't really like this method of casting arrays into objects.

Also can you also split up the clean up and the addition of the put(), putInTube() into separate patches so that they can been easier to review.

Thanks.

gordon’s picture

Issue tags: +Next release

bump

gordon’s picture

Status: Needs work » Fixed

Fix in dev

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