1

I have Drupal server with a REST API using services and rest_server (that's just background, I don't THINK that applies to this problem at all.) I'd like particular API calls to only report back that the request was received, but then go on to do further work in PHP.

To me, this seems like a thread/fork solution, but I can't seem to find a viable solution using PHP/Drupal. The first (and from the looks of it, easiest) thing I came across was the pcntl_fork() function. But, as it looks like that's not viable:

It is not possible to use the function 'pcntl_fork' when PHP is used as Apache module. You can only use pcntl_fork in CGI mode or from command-line.

Using this function will result in: 'Fatal error: Call to undefined function: pcntl_fork()'

(And, indeed, when I try using function_exists() through command line, it's there, but when run in PHP through Drupal it says function is undefined. I'm reading some stuff on how to install it/recompile PHP with it, but would prefer if there were a method that didn't involve changes to the server.)

Other than that, I'm coming across solutions such as creating a queue to run with cron jobs, creating convoluted menu items, or a whole background processes... thing. All I want is for some arbitrary code to be run off on its own. Does anyone have a suggestion that they feel works best with with Drupal?

(Also, I don't know what to tag this as--I tried thread or asynchronous but those don't seem to exist--so feel free to tag.)

1
  • 1
    PHP is not designed for working in an application-server model. Drupal has batch processing mechanisms that help to obviate this, but if you try to impose an architecture that is not really compatible with the underlying language, you are going to struggle. Apr 21, 2015 at 16:18

2 Answers 2

2

A request to a REST API should just return required data and end. If you want to do asynchronous tasks do not reuse request threads, use cron tasks.

If you want to get independent from Durpal cron use your own drush command executed by a system cron. So, you declare a drush command that makes whatever asyncrhonous tasks and then declare a system cron that runs drush on that command.

If you need to prepare data for that command (for example, if some REST API functions generates data that must be processed asynchronously) you can use Drupal Queues. When the API is called add a new element to a certain queue that will be later processed by the drush command.

4
  • I'm effectively going for: 1.) Site A tells Site B "refresh yourself" using RestAPI call 2.) Site B responds "okay, request received", then goes off to gather it's information 3.) When finished, Site B makes API call to Site A with all that information. I don't want to wait for Site B on the initial call because it can take some time, and there are going to be Sites C,D,...N that Site A will be doing the same thing with. But, I also don't want Sites B,C,D...N to have to wait for an eventual CRON run. Apr 21, 2015 at 17:21
  • I gather you're suggesting: 1.) When Site B gets API call to refresh, it adds an item to a Drupal Queue, then it can return. 2.) Create a drush command for a system cron that runs, say, once a minute to process said queue. Apr 21, 2015 at 17:21
  • Yes, that's what I did when I faced a similar problem.
    – sanzante
    Apr 21, 2015 at 17:42
  • So, that's more or less what I did. Create Drush commands to do the actions I wanted to occur, and used the apache users crontab to fire those off at the interval(s) I wanted (using Drush aliases.) Jan 7, 2016 at 19:51
0

You could use the background process module or HTTPRL module to run external http requests somewhat asynchronously.

https://www.drupal.org/project/background_process

Or you could use a nodejs backend, and the Drupal Node.js Integration module, to achieve grater decoupling and asynchronous workload management.

https://www.drupal.org/project/nodejs

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.