Question

All,

I'm looking for a good way to do some job backgrounding through either of these two services.

I see PHPFog supports IronWorks, but i need something more realtime. Through these cloud based PaaS services, I'm not able to use popen(background.php --token=1234). So I'm thinking the best solution, might be to try to kick off a gearman worker to handle the job. (Actually my preferred method would be to use websockets to keep a connection open and receive feedback from the job, rather than long polling a db table through AJAX, but none of these guys support websockets)

Question 1 is, is there a better solution than using gearman to offload the job?

Question 2 is, http://help.pagodabox.com/customer/portal/articles/430779 I see pagodabox supports 'worker listeners' ... has anybody set this up with gearman? Would it work?

Thanks

Was it helpful?

Solution

I am using PagodaBox with a background worker in an application I am building right now. Basically, PagodaBox daemonizes a PHP process for you (meaning it will continually run in the background), so all you really have to do is create a script that checks a database table for tasks to run, runs them, and then sleeps a bit so it's not running too many queries against your database.

This is a simplified version of what I have running:

// Remove time limit
set_time_limit(0);

// Show ALL errors
error_reporting(-1);

// Run daemon
echo "--- Starting Daemon ---\n";
while(true) {

    // Query 'work_queue' table for new tasks
    // Loop over items and do whatever tasks are associated with them
    // Update row to mark task as completed

    // Wait a bit
    sleep(30);
}

A benefit to this approach is that it's easy to test via CLI:

php tasks.php

You will see all the echo statements come through in console as it's running, and of course it's much easier to do than a more complicated setup with other dependencies like Gearman.

So whenever you add a new task to the table, the maximum amount of time you'll wait for that task to be started in a batch is 30 seconds (or whatever your sleep time is). This is better and preferable to cron jobs, because if you setup a cron job to run every minute (the lowest possible interval) and the work you have to do takes longer than a minute, another cron process will start working on the same queue and you could end up with quite a lot of duplicated task work that could cause a lot of issues that are hard to debug and troubleshoot. So if you instead have either only one background worker that runs all tasks, or multiple background workers that work on different task types, you will never run into this issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top