Batch Processing in PHP Cron

Source: Internet
Author: User

There is a big problem with large chain stores. Each day, thousands of transactions occur in each store. The company's executives want to mine the data. Which products are selling well? What's worse? Where Do organic products sell well? How is ice cream sold?

To capture this data, the Organization must load all transactional data into a data model to make it more suitable for generating the types of reports required by the Company. However, this takes a long time, and as the chain grows, it may take more than one day to process data for one day. Therefore, this is a big problem.

Now, your web application Program There may be no need to process so much data, but the processing time of any site may exceed the time the customer is willing to wait. Generally, the customer is willing to wait for 200 ms. If the time exceeds this period, the customer will feel the process is "slow ". This number is based on desktop applications, and Web makes us more patient. However, in any case, the customer should not wait for more than a few seconds. Therefore, some policies should be used to process Batch jobs in PHP.

Distributed mode and Cron

On Unix machines, the core program for batch processing is the cron daemon. This daemon reads a configuration file, which tells it which command lines to run and how often to run them. Then, the daemon executes them according to the configuration. When an error occurs, it can even send an error output to the specified email address to help debug the problem.

I know some engineers strongly advocate thread technology. "Thread! The thread is the real method for background processing. The cron daemon is too outdated ."

I don't think so.

I have used both methods. I think cron has the advantages of "Keep it simple, stupid (Kiss, simple is beautiful. It makes background processing easy. Instead of writing a running multi-threaded job processing application (so there is no memory leakage), cron starts a simple batch processing script. This script determines whether a job is to be processed, executes the job, and then exits. No need to worry about memory leakage. There is no need to worry about stopping the thread or getting into an infinite loop.

So how does cron work? This depends on your system environment. I will only discuss the old-fashioned simple cron Unix Command Line version. You can ask the system administrator how to implement it in your web application.

The following is a simple cron configuration. It runs a PHP script at every night:

0 23 *** Jack/usr/bin/PHP/users/home/Jack/myscript. php

The first five fields define the time when the script should be started. Then the username used to run the script. Other commands are the command lines to be executed. The time fields are minute, hour, day in month, month, and day in week. The following are examples.

Command:

15 * Jack/usr/bin/PHP/users/home/Jack/myscript. php

Run the script 15th minutes each hour.

Command:

15, 45 * Jack/usr/bin/PHP/users/home/Jack/myscript. php

Run the script 15th and 45th minutes each hour.

Command:

*/1 3-23 *** Jack/usr/bin/PHP/users/home/Jack/myscript. php

Run the script every minute from AM to AM.

Command

30 23 ** 6 Jack/usr/bin/PHP/users/home/Jack/myscript. php

Run the script at every Saturday evening (set to 6 on Saturday ).

As you can see, the number of combinations is infinite. You can control the script running time as needed. You can also specify multiple scripts to run. In this way, some scripts can be run every minute, while other scripts (such as backup scripts) can only run once a day.

To specify the email address to which the reported error is sent, use the mailto Command, as shown below:

Mailto = jherr@pobox.com

Note: For Microsoft Windows users, an equivalent scheduled tasks system can be used to regularly start command line processes (such as PHP scripts ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.