调度系统 - 如何完成? PHP与Node.js

Consider the notification system in Google calendar. It sends you a push/email notification user specified time (like 10 minutes/10 hours etc). How is this implemented on the server side? Assuming Google calendar is on a PHP/Apache platform, is there a script which runs continuously and checks the current time against the time in database and then push notifications when the times match? Basic question - how does the server know when to push the notifications?

Is it easier to do this on Apache/PHP or Node.js?

You are asking about black magic. Are you sure you want to know the answer?

In answer your hypothetical question, "If Google uses PHP for scheduled notifications how do they do it?" the best answer would be cron. It really has nothing to do with PHP, it is a service run by the operating system (at least on *nix-based systems) which wakes up every minute, checks a to-do list, runs things it needs to run, and then goes back to sleep. The "minute" thing is a very important detail: it doesn't run constantly, it doesn't run tasks with a by-the-second precision, only every minute.

Cron can run any executable script, and is designed to run with the permissions of the person who scheduled the cron task, so it can run shell scripts out of your home directory and it can run php scripts if they are correctly configured.

As an alternative to cron, one could also write a new service that continually runs as a daemon, which wakes up every so often to check if notifications need to be sent. You would lose the standardized behavior of cron, but you could do by-the-second scheduling, and anything else your heart desires.

As for knowing when to send notifications, you create a database containing a list of tasks and when they should run, and then find a nicely optimized query to grab the current set and process them. Cron, or your custom service (which you have written in Node.js because all the cool kids are using Node.js), runs this query every minute / every so many seconds. And if you are Google, you then figure out how to scale this out to a thousand machines.

First of all, PHP and Node.js are two different laugnages. For the purpose of scheduler, different solutions are implemented accordingly.


PHP

Because the nature of PHP, from its name, PHP is a HyperText Processor. Everytime a visitor access an URL on your website, this processor will run once to process the request, then stop. There is no system-wide control for PHP, which means it don't have the ability to schedule a long term task by itself. So PHP developers has developed two methods to trigger the scheduler. Here I take the Wordpress(WP) for example:

  1. The first approcah is implementing a PHP script that will be called every time a browser access your server. (included by index.php for example) WP has a WP_CRON.PHP file playing this role. It will check the scheduled tasks see if their scheduled time has been passed. If the answer is yes, the scheduled task will be executed immidiately. Once you know how it works, you will wondering how unreliable it was. Yes, on one hand, if your website is not that busy, the WP_CRON.PHP might not be triggered if no body visit your site. On the other hand, if your website is too busy, the WP_CRON.PHP will be called everytime and increase the overhead.

In short, a script tiggered by remote user, not relaible

  1. The second approach is using the system CRON JOB that most *inx server offered. You can adde a cron job into the cron table of your server. Task scheduled by cron table will be executed on time every time. This is the perfered solution for PHP if accurace and relaibility is crucial. However, it requires you have certain permissions on your server.

In short, a script tiggered by server system according to the accurate schedule, very relaible


Node.js

For Node.js, it is a different story. By design, Node.js runs on a single process, this process will keep running until it was terminated by system administrator. This process will also handling all the requests from all the clients globally. Unlike the PHP which only process the PHP file, Node.js is a complete application container that has the abliity to do more things than PHP. There are scheduler modules already implemented for Node.js. Check node-schedule for more detail.