📜 ⬆️ ⬇️

Organizing unique processes - ScriptAlone to the rescue


The most popular class design pattern has certainly been and remains the Singleton , a pattern that guarantees the uniqueness of a class object within a single process.

But what if the need arises to unify the process itself? In this article we will talk about the tasks in which it may be needed and how to achieve this.

Introduction

There are a number of tasks in which it is fundamentally important that a particular script be executed exclusively in a single version since parallel start of it by several processes can lead to various kinds of overlays.
')
There are many examples of this, but for the most part these will be various kind of background handlers, such as: email / sms background messengers, cache utilizers, monitoring of active users, background backup, etc.

Two ways to implement

There are two main ways to implement such tasks:
  1. Write scripts that will exclude all possible overlays of their parallel launch.
  2. Use the system to control the uniqueness of the launch of the script.
The first option is often much more complicated, and sometimes even not at all feasible. The second option seems simple to implement, you just need to use the "controller" of the uniqueness of the process. From experience, I would say that developers are more inclined to write just such “controllers”, but the problem is that these “controllers” are extremely far from perfect, and this leads to very bad consequences.

ScriptAlone - the “controller” of the process uniqueness

ScriptAlone is a class whose objects serve as “controllers” of the process uniqueness. It has been repeatedly optimized and refined, and also 100% tested in combat conditions, so I can safely recommend you to use it.

In order not to be wordy when describing all his charms, I suggest to get acquainted with how he works on a concrete example.

Example

You have a live queue of letters that should consistently recover in the shortest possible time.

Requirements
  1. You need a script that will be constantly running and every second to check the presence in the queue of letters to send.
  2. Only one copy of the script can be run to avoid duplicate email sending situations.
  3. If the script is interrupted due to an error, it should be restarted as soon as possible.
  4. If the script hangs on executing one of the iterations, then it should be restarted as soon as possible.
  5. The script must be restarted every 5-10 hours to prevent possible memory leaks. Stopping the script in this case should be done safely (when the tasks of the current iteration have been completed).
  6. You need some indicator that the script is currently running
  7. You must have the ability to forcibly stop the execution of the script.
What needs to be done
  1. Take example.php - a simple script that uses ScriptAlone.
  2. Configure CRON on your server so that example.php is called every 5 minutes.
What happens
  1. The script will be constantly running. Check for new letters will be made every second.
  2. Only one copy of the script will be launched at any time.
  3. If the script is interrupted or the time limit for the execution of one iteration is exhausted, the script will be restarted.
  4. Every 5 hours the script will be restarted in a safe manner.
  5. Reloading the script means restarting it no later than 5 minutes after stopping - this is due to the CRON setting to attempt to launch another copy of the script every 5 minutes.
  6. You can make sure that the script is currently running by the presence of the ./example.php.works file. Also from its contents you can learn additional information about the status of the script:
    PID: 12656471471193231407
    Started: 2010-02-08 19:39:07
    Worked: 2010-02-08 19:39:10
    Expire: 2010-02-08 19:39:26
  7. You can stop the execution of the script by simply creating a file with the name ./example.php.works.stop

Use on health :)


Download the latest version, subscribe to updates via RSS or participate in the development can be on the project page .

Thanks in advance for constructive criticism and feedback :)

PS In the process of writing the article, the strange behavior of the Habr was revealed: all words beginning with Script * are translated to lower case - corrected by self-replacement of Script with S ript. All links in which * script * is found are replaced with * s & # 99; ript * which makes them inoperable - corrected by replacing * script * with * s% 63ript *. Unsubscribed to the support, but have not yet responded.

Source: https://habr.com/ru/post/83506/


All Articles