1

maybe anybody knows how to best tackle this problem.

I have a ZF2 application in which a client can upload a file. The file contains orders and then needs to be processed. If I trigger an event that starts processing this file right away, the client cannot move on (to do other things). So I would like to trigger an event in the background that starts processing this file while my action returns the next page to my client so he or she can go on and fill in other stuff. Now of course I can solve this with cron jobs... But maybe there is another way now that ZF2 is so much more event driven? Is it possible to trigger an event (or service) in the background like so:

public function csvUploadAction()
{
    $id = (int) $this->params()->fromRoute('id', 0); 

    $form = new CsvForm($id);
    // do some validating and stuff...

    if ($form->isValid()) {
    // more stuff..

    $this->getEventManager()->trigger('readCsvInBackground', $this, $parameters);
        return $this->redirect()->toRoute('publications', array(
                        'action' => 'edit', 'id' => $id
                    ));
// etc..
}

I have searched arround for a solution like this but can't find anything (other then using cron jobs). Anybody an idea? Thank you very very much for your time!

Isolde
  • 626
  • 6
  • 12

2 Answers2

2

Bram is correct, you want a work queue.

You might look at SlmQueue, which is a ZF2 module that is something of a queue-abstraction layer (with back-ends for Beanstalkd, Amazon SQS, and DoctrineORM, currently), if not to use it, at least for some inspiration. I'm using it with beanstalkd, with good results.

You could, of course, simplify, and just use beanstalkd directly, probably via the Pheanstalk client library for PHP.

timdev
  • 61,857
  • 6
  • 82
  • 92
1

You should have a look into a jobqueue solution. The general idea is you just put a message/job in the queue containing your csv (base64 encoded) or write your csv to disk and pass the path in your message. You can just send the message to the queue (which is very lightweight action) and return response to the user directly.

Next you have to write a worker which takes messages from your queue and processes them in FIFO order. If this work is very heavy and your waiting jobs keep accumulating you can add in more workers so work is distributed among many workers (this is called fanout).

Some open source solutions which have a PHP client api and work great for your use case are:

Bram Gerritsen
  • 7,178
  • 4
  • 35
  • 45
  • Thank you Bram, that is a solution I didn't think of! I am going to go for Gearman. Thank you very much for you suggestion :-) – Isolde Jul 31 '13 at 10:45