0

I have a piece of code that takes a while to run and is low priority. I was wondering if in PHP I can do something like

public function put () {

  $comment = array('title' => 'my title', 'description' => 'my description');

  sendtoQueue($this->internalCall('controller' => 'Comment', 'data' => $comment);

  $object = $this->get('id' => $this->id);
  return $object;
}

Where the function inside sendToQueue wouldnt delay the $object being fetching and returned and would be run in the BG.

Possible? I know I can throw it to python but ideally I'd like it to be run within the current scope.

2
  • 1
    Why don't you just execute the script from AJAX? Commented Jul 22, 2013 at 9:58
  • This is an API project, the PUT request is doing many things but the response is being slowed down by some of them that are less important. Commented Jul 22, 2013 at 10:00

3 Answers 3

1

If you need it to run in the current scope than you could fork (pcntl_fork()) a process and let the child handle this whilst the parent carries on

Otherwise just run a script periodically that empties a queue of tasks.

Sign up to request clarification or add additional context in comments.

1 Comment

and, be prepared to struggle with opened connection issues. Also, as far as I know, the child processes die when the parent process dies. Our web process is going to die at the end of http request.
1

You could use exec to start a new php process that runs the script in the background and make sendToQueue return.

You could also use a solution like beanstalkD. Where sendtoQueue pushes data to Beanstalk and have workers empty your queue in the background

1 Comment

Exec doesn't keep the scope of the current user or action, which is why I wanted to avoid doing it, because I have to create a new script to redo whats already coded. BeanstalkD looks interesting, I feel this is probably the only way to go unfortunately.
0

This is something you could easily do with enqueue library. First, you can choose from a variety of transports, such as AMQP, STOMP, Redis, Amazon SQS, Filesystem and so on.

Secondly, That's super easy to use. Let's start from installation:

You have to install the enqueue/simple-client library and one of the transports. Assuming you choose the filesystem one, install enqueue/fs library. To summarize:

composer require enqueue/simple-client enqueue/fs 

Now let's see how you can send messages from your POST script:

<?php
// producer.php

use Enqueue\SimpleClient\SimpleClient;

include __DIR__.'/vendor/autoload.php';

$client = new SimpleClient('file://'); // the queue will store messages in tmp folder

$client->sendEvent('a_topic', 'aMessageData');

The consumption script:

<?php
// consumer.php

use Enqueue\SimpleClient\SimpleClient;
use Enqueue\Psr\PsrProcessor;
use Enqueue\Psr\PsrMessage;

include __DIR__.'/vendor/autoload.php';

$client = new SimpleClient('file://');

$client->bind('a_topic', 'a_processor_name', function(PsrMessage $psrMessage) {
   // processing logic here

   return PsrProcessor::ACK;
});

// this call is optional but it worth to mention it.
// it configures a broker, for example it can create queues and excanges on RabbitMQ side. 
$client->setupBroker();

$client->consume();

Run as many consumer.php processes as you by using supervisord or other process managers, on local computer you can run it without any extra libs or packages.

That's a basic example and enqueue has a lot of other features that might come in handy. If you are interested, check the enqueue documentation out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.