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
Related
I need to update a database that I have stored localy based on an online API. I need to make API calls periodically. What is a good strategy when it comes to periodical PHP script execution?
Use a cron job. There are tons of tutorials out there on how to do that. There is even a Wordpress Cron Job scheduler plugin (this link is only for reference, I'm not endorsing it specifically). You can find it here. Note that you will need administrative rights on the server to execute cron jobs.
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.
I'm looking for a job queue manager in node.js which can be invoked by php. This is for a web application which needs to send emails, create pdf files and so on which I'd like to perform asynchronous of the php process.
Example of the process:
User requests a php page
Php invokes the job queue manager and adds a task
Task is executed in node.js asynchronously of php, preferably when it's a bit more quiet
Task is to execute a php script
Why this "complex" system?
We write all our web-applications in php (Zend Framework)
We'd like to start learning node.js
We need a asynchronous process (fast response!)
The "real" task should be a php script as well, to utilize already written php classes, to have easy access to database connections and be as much DRY as possible
Use cases of this system:
User registers himself, system will send welcome email
User completes ecommerce order, system will send invoice
In the end, we'd like to use node-cron as well, to perform non-system wide cron tasks (very application specific). Node-cron will invoke the job queue manager, which will subsequently run a php script.
Is there such an application already in node?
In such a case I would prefer a message queue like RabbitMQ and client side libraries like node-amqp and php-amqp. Then simply send your job from your PHP script in the queue and let nodejs pick up the job from the queue. A big advantage is that it is extensible and it is widely used and tested in the enterprise market.
One possible options is node-jobs, which uses Redis.
I am creating a scheduler using PHP/MySQL where I have to allow the use to select date and time for publishing the content. The requirement is, the content should get posted at the exact scheduled time. If I create cron job, the notifications won't go out at exact time.
Running a cron job every minute is not really feasible in my case since I have to publish using an API and that itself is time consuming.
Is there any other way I can implement to make sure that the exact time provided in scheduler is followed. One of the best example that does this is Google Calendar which sends the reminders at the time you ask for.
Aditya
You should consider creating a daemon. The PEAR System_Daemon would be a great starting point. The daemon should essentially be a loop that queries the database, posts the content if necessary and sleeps.
Some sections of their documentation you might be interested in:
What is Daemon
Daemons vs Cronjobs
Installation
Example
YOU SHOULD CREATE CRONE JOB TO ACCOMPLISH THIS.
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