We have a large web application built on PHP. This application allows scheduling tweets and wall posts and there are scheduled emails that go out from the server.
By 'scheduled', I mean that these are PHP scripts scheduled to run at particular time using cron. There are about 7 PHP files that do the above jobs.
I have been hearing about Message Queues. Can anyone explain if Message Queues are the best fit in this scenario? Do Message Queues execute PHP scripts? or do we need to configure this entirely differently? What are the advantages / disadvantages?
Using Crontab to make asynchronous tasks (asynchronous from your PHP code) is a basic approach where using a job/task queue manager is an elaborate one and give you more control, power and scalability/elasticity.
Crontab are very easy to deal with but does not offer a lot of functionalities. It is best for scheduled jobs rather than for asynchronous tasks.
On the other hand, deploying a Task queue (and its message broker) require more time. You have to choose the right tools first then learn how to implement them in your PHP code. But this is the way to go in 2011.
Thank God, I don't do PHP but have played around with Celery (coupled with RabbitMQ) on Python projects ; I am sure you can find something similar in the PHP world.
Related
Would appreciate some help understanding typical best practices in carrying out a series of tasks using Gearman in conjunction with PHP (among other things).
Here is the basic scenario:
A user uploads a set of image files through a web-based interface. The php code responding to the POST request generates an entry in a database for each file, mostly with null entries in the columns, queues a job for each to do analysis using Gearman, generates a status page and exits.
The Gearman worker gets a job for a file and starts a relatively long-running analysis. The result of that analysis is a set of parameters that need to be inserted back into the database record for that file.
My question is, what is the generally accepted method of doing this? Should I use a callback that will ultimately kick off a different php script that is going to do the modification, or should the worker function itself do the database modification?
Everything is currently running on the same machine; I'm planning on using Gearman for background scheduling, rather than for scaling by farming out to different machines, but in any case any of the functions could connect to the database wherever it is.
Any thoughts appreciated; just looking for some insights on how this typically gets structured and what might be considered best practice.
Are you sure you want to use Gearman? I only ask because it was the defacto PHP job server about 15 years ago but hasn't been a reliable solution for quite some time. I am not sure if things have drastically improved in the last 12 months, but last time I evaluated Gearman, it wasn't production capable.
Now, on to the questions.
what is the generally accepted method of doing this? Should I use a callback that will ultimately kick off a different php script that is going to do the modification, or should the worker function itself do the database modification?
You are going to follow this general pattern with any job queue:
Collect a unit of work. In your case, it will be 1 of the images and any information about who that image belongs to, user id, etc.
Submit the work to the job queue with this information.
Job Queue's worker process picks up the work, and starts processing it. This is where I would create records in the database as you can opt to not create them on job failure.
The job queue is going to track which jobs have completed and usually the status of completion. If you are using gearman, this is the gearmand process. You also need something pickup work and process that work, I will refer to this as the job worker. The job worker is where the concurrency happens which is what i think you were referring to when you said "kick off a different php script." You can just kick off a PHP script at an interval (with supervisord or a cronjob) for a kind of poll & fork approach. It's not the most efficient approach, but it doesn't sound like it will really matter for your applications use case. You could also use pcntl_fork or pthreads in PHP to get more control over your concurrent processes and implement a worker pool pattern, but it is much more complicated than just firing off a script. If you are interested in trying to implement some concurrency in PHP, I have a proof-of-concept job worker for beanstalkd available on GitHub that implements a worker pool with both fork and pthreads. I have also include a couple of other resources on the subject of concurrency.
Job Worker (pthreads)
Job Worker (fork)
PHP Daemon Example
PHP IPC Example
I'm currently looking for a way that I can build a scheduling system that could run scripts at a certain date and/or time, and do it in such a way that it doesn't care what operating system it's living on. I know with linux/unix, I can add enteries using cron, but with other OS's? NOt so much.
Also if there's a way to have it execute without relying on external software such as cron, I'd be interested to learn about those options as well, although I realize that's probably a stretch with PHP.
Thanks for your help in this matter!
If you write a persistent server in PHP that runs 24 hours a day, then scheduling tasks is easy; loop forever and check if there are any tasks scheduled for the current time each second and run them... run the code or spawn a process to do so with exec.
Assuming you did not intend to write a persistent server in PHP, then no, there's no platform agnostic way to schedule tasks with the operating system.
I am handling a project which contain message queue concept. Now the project is in PHP, and it's making more delay in message sending or mail sending. So I suggest to develop a message queue in Perl or Python script. Could you please suggest which is best either PHP or Perl or Python?
A possible solution could be to use Gearman as a queue :
Your PHP project would send messages to Gearman, as background jobs ; and finish
Gearman would dispatch those messages to workers
Workers will deal with the jobs -- doing the stuff that might take time
One additional advantage : the day you need several servers to handle a larger amount of jobs, you'll already have what's needed : Gearman will deal with load-balancing for you.
PHP is perfectly adequate to implement a simple message queue. So, if your current code is causing delays then it is because of your design, not because of some limitation with PHP. Switching to a different language isn't going to help you. Bad code is bad code regardless of language.
The best thing you can probably do is going with an existing message queue. Pascal recommended Gearman. I have worked with (and quite liked) Beanstalkd. If you need a metric ton of features, have a look at ApacheMQ or RabbitMQ.
That said, if you insist on implementing your own message queue, I would suggest sticking with PHP. That way you can re-use code from your existing application (e.g. re-use your models and database API for example).
Here are two alternative for gearman
a. Beanstalkd
b. MemcacheQ
MemcacheQ http://memcachedb.org/memcacheq/
Adding and fetching from queue needs to be done manually using code.
Its not like you send it to queue and MemcacheQ will execute it one by one.
but its very very fast.
Beanstalkd
http://kr.github.com/beanstalkd/download.html
It supports many languages.
I am working on windows. I have built a twitter application using the twitter API which periodically checks for new tweets as well as allows users to update their status. I have written separate PHP files for reading (reader.php) and writing tweets (writer.php). The only problem is how do I periodically read the tweets. There are a few ways which I can think of -
1) Use a time-based job scheduler (like Cron) to periodically run the reader.php.
How do I do this?
2) Use multithreading to run both reader and writer.php and use a timer function in reader.php
Suggestions?
Since you are working within Windows, you probably won't be able to use cron very easily. You can however, use the task scheduler. See this link for step-by-step instructions.
Another option is to use some kind of job queuing system. The Zend Server Job Queue has the ability to schedule recurring tasks. I use it on my site for an awful lot of stuff. Actually, a lot of what you are trying to do. It does periodic Twitter searches, processes relationships and such. I have a posting about how to use it at Do you queue? Introduction to the Zend Server Job Queue
I am creating a web application using zend, here I create an interface from where user-A can send email to more than one user(s) & it works excellent but it slow the execution time because of which user-A wait too much for the "acknowledged response" ( which will show after the emails have sent. )
In Java there are "Threads" by which we can perform that task (send emails) & it does not slow the rest application.
Is there any technique in PHP/Zend just like in Java by which we can divide our tasks which could take much time eg: sending emails.
EDIT (thanks #Efazati, there seems to be new development in this direction)
http://php.net/manual/en/book.pthreads.php
Caution: (from here on the bottom):
pthreads was, and is, an experiment with pretty good results. Any of its limitations or features may change at any time; [...]
/EDIT
No threads in PHP!
The workaround is to store jobs in a queue (say rows in a table with the emails) and have a cronjob call your php script at a given interval (say 2 minutes) and poll for jobs. When jobs present fetch a few (depending on your php's install timeout) and send emails.
The main idea to defer execution:
main script adds jobs in the queue
cron script sends them in tiny slices
Gotchas:
make sure u don't send an email without deleting from queue (worst case would be if a user rescieves some spam at 2 mins interval ...)
make sure you don't delete a job without executing it first ...
handle bouncing email using a score algorithm
You could look into using multiple processes, such as with fork. The communication between them wouldn't be as simple as with threads (but then, it won't come with all of its pitfalls either), but if you're just sending emails, it might not be necessary to communicate much, if at all.
Watch out for doing forks on an Apache process. You may get some behaviors that you are not expecting. If you are looking to do any kind of asynchronous execution it should be via some kind of queuing mechanism. Gearman is one. Zend Server Job Queue is another. I have some demo code at Do you queue? Introduction to the Zend Server Job Queue. Cron can be used, but you'll have the problem of depending on your cron scheduler to run tasks whereas asynchronous computing often needs to be run immediately. Using a queuing system allows you to do that without threading.
There is a Threading extension being developed based on PThreads that looks promising at https://github.com/krakjoe/pthreads
There is pcntl, which allows you to create sub-processes, but php doesn't work very well for this kind of architecture. You're probably better off creating a long-running script (a daemon) and spawning multiple of them.
As of PHP there are no threads in it. However for php, you can have a look at this roundabout way
http://www.alternateinterior.com/2007/05/multi-threading-strategies-in-php.html
You may want to use a queue system for your email sending and send the email from another system which supports threads. PHP is just a tool and you should the tool that is best fitted for the job.
PHP doesn't include threading as part of the language, there are some methods that can emulate it but they aren't foolproof.
This Google search shows a few potential workarounds