Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Lorna Mitchell:
Beanstalk, Pheanstalk and Priorities
May 08, 2014 @ 14:47:08

Lorna Mitchell has a quick post showing you how to use the "priority" option that the Pheanstalk library provides when working with a Beanstalk queue.

I've got an application that uses Beanstalkd to queue up messages, and some PHP worker scripts that grab messages from the queue and process them. Messages get added by the web application, but can also be added by cron - and when I add a bunch of messages via cron, I don't want to swamp what the web application is doing! Those cron-added jobs are mostly pretty low priority, generating reports, sending weekly update emails, that kind of thing. Beanstalkd has a concept of priority, so I can create lower priority jobs.

She includes a three line example showing the use of the "LOW_PRIORITY" constant to tell Beanstalk how and when it should handle this particular job. In her situation, where there are multiple smaller jobs rather than larger ones it makes more sense to shift some of the smaller, less important tasks to be executed later.

tagged: beanstalk pheanstalk priority tutorial example

Link: http://www.lornajane.net/posts/2014/beanstalk-pheanstalk-and-priorities

Lorna Mitchell:
Working with PHP and Beanstalkd
Mar 06, 2014 @ 16:36:53

Lorna Mitchell has posted a new tutorial to her site today walking you through using Beanstalkd with PHP for a simple queuing setup in your application. Beanstalkd is "a simple, fast work queue. Its interface is generic, but was originally designed for reducing the latency of page views in high-volume web applications by running time-consuming tasks asynchronously."

I have an API backend and a web frontend on this project (there may be apps later. It's a startup, there could be anything later). Both front and back ends are PHP Slim Framework applications, and there's a sort of JSON-RPC going on in between the two. The job queue will handle a few things we don't want to do in real time on the application, such as: updating counts of things like comments, [...] cleaning up, [...] other periodic things like updating incoming data/content feeds or talking to some of the 3rd party APIs we use like Mailchimp and Bit.ly.

She starts with a look at how to add jobs to the queue (she assumes that you've already set up the Beanstalkd instance at this point). She uses the Pheanstalk library for the job handling and includes a sample call to configure the connection and create an instance to make the connection. The sample job contains an array of data including an "action" and "data" for it to use when processing. She also includes an example of a basic PHP-based Beanstalkd worker that will go through currently pending jobs and execute them based on the action/data combination. In the sample worker script, she defines the action as a method in the class to be executed directly on the worker instance. She finishes off the post with a few "things to remember" about working with workers and long-running PHP scripts.

tagged: beanstalkd tutorial introduction pheanstalk worker

Link: http://www.lornajane.net/posts/2014/working-with-php-and-beanstalkd


Trending Topics: