Use a service program to implement the PHP scheduled task execution function

Source: Internet
Author: User

Recently, I have made several PHP game projects, including card and board games and RPG games, all of which require more or less timed information update mechanisms. For example, player timeout detection for card and board games. More are used in RPG games, such as monster refresh, automatic blood returning, task expiration, and ranking refresh. Because PHP does not have memory residentProgram, So there are some difficulties in processing.

I have referred to some implementation methods in the same industry. The usual practice is to write an auxiliary program in C ++, Python, Java, etc. based on the needs of a specific project to regularly update the database. However, this is very troublesome. First, these helper programs require the intervention of programmers who understand another language, which will inevitably increase development costs and risks. Second, codebugging between programmers in different languages is very troublesome and the progress is very slow. because the relationship between the auxiliary program and the foreground is very close, it is basically necessary to develop and debug the program at the same time.

I used a regular task execution method in the project. I feel that this solution is good.CodeAll are sent to the PHP side.

First, define a table named task in the database, which contains two fields: exectime and URL. Exectime is a UNIX-type time, and URL is string-type. Each piece of data represents a task. The specific meaning is "this task is executed at exectime and the execution address is URL ". The Helper monitors the table every second and compares the current time to the time of each task in the table. If the time reaches, the Helper requests the URL. Then the task is completed and the task is deleted. This is cyclical.

The advantage of doing so is that PHP developers can freely execute the web pages they want to execute at the time they want. This program only needs to be written once and can be used in any similar project.

I made this program into a daemon for Windows Service and archlinux, thus implementing cross-platform for the entire project.

Additional content:

The task is enabled in this way. We have a server switch interface similar to a large online game. After logging on to the game background, go to the server control page to view the running status of the current server, you can enable or disable the server. When the server is enabled, related tasks are inserted into the task list. If the server is disabled, the task list is cleared. It is in the artificial form.

The task is enabled repeatedly, because these tasks are inserted into the task table by PHP, and each task in the task table is deleted by the auxiliary program once it is executed, therefore, each task can only be executed once. If a task needs to be executed cyclically, you can only insert the task itself into the task list by executing the PHP code of the task (that is, the task URL.

Task Timeout: There are two types of task Timeout: In the data table, task execution times out, and in the request for this task page times out. The first case does not occur because the Helper executes all tasks less than or equal to the current time each time. In the second case, the Helper Program automatically determines whether the access to the page is successful. If a server error is returned or cannot be connected, the task will be retained and not deleted, wait until the next loop.

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.