21 Sep 2017 20:22:36 UTC
- Distribution: Sys-RunAlone
- Module version: 0.13
- Source (raw)
- Browse (raw)
- How to Contribute
- Issues (3)
- Testers (848 / 16 / 0)
- KwaliteeBus factor: 2
- % Coverage
- License: unknown
- Download (5.76KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest version++ed by:2 non-PAUSE users
- Ben Tilly (firstname.lastname@example.org)
- THEORY OF OPERATION
- TRYING MORE THAN ONCE
- OVERRIDING CHECK
- REQUIRED MODULES
- SEE ALSO
Sys::RunAlone - make sure only one invocation of a script is active at a time
use Sys::RunAlone; # code of which there may only be on instance running on system use Sys::RunAlone silent => 1; # be silent if other running instance detected use Sys::RunAlone retry => 50; # retry execution 50 times with wait time of 1 second in between use Sys::RunAlone retry => '55,60'; # retry execution 55 times with wait time of 60 seconds in between use Sys::RunAlone 'silent'; # obsolete form of silent => 1
Provide a simple way to make sure the script from which this module is loaded, is only running once on the server. Optionally allow for retrying execution until the other instance of the script has finished.
This documentation describes version 0.12.
There are no methods.
The functionality of this module depends on the availability of the DATA handle in the script from which this module is called (more specifically: in the "main" namespace).
At INIT time, it is checked whethere there is a DATA handle: if not, it exits with an error message on STDERR and an exit value of 2.
If the DATA handle is available, and it cannot be
flocked, it exits with an error message on STDERR and an exit value of 1. The error message will be surpressed when
silent =1> was specified in the
usestatement. This can be overridden with the environment variable
If there is a DATA handle, and it could be
flocked, execution continues without any further interference.
Optionally, it is possibly to specify a number of retries to be done if the first
flockfails. This can be done by either specifying the retry value in the
usestatement as e.g.
retry =55>, or with the environment variable
RETRY_SYS_RUNALONE. There are two forms of the retry value:
use Sys::RunAlone retry => 55; # retry 55 times, with 1 second intervals
Specify the number of times to retry, with 1 second intervals.
use Sys::RunAlone retry => '55,60'; # retry 55 times, with 60 second intervals
Specify both the number of retries as well as the number of seconds interval between tries.
This is particularly useful for minutely and hourly scripts that run a long and sometimes run into the next period. Instead of then not doing anything for the next period, it will start processing again as soon as it is possible. This makes the chance of catching up so that the period after the next period everything is in sync again.
In some cases, the same script may need to be run simultaneously with another incarnation (but possibly with different parameters). In order to simplify this type of usage, it is possible to specify the environment variable
SKIP_SYS_RUNALONEwith a true value.
will run the script always.
will actually be verbose about this and say:
Skipping Sys::RunAlone check for 'yourscript.pl'
Execution of scripts that are (sym)linked to another script, will all be seen as execution of the same script, even though the error message will only show the specified script name. This could be considered a bug or a feature.
If you change the script while it is running, the script will effectively lose its lock on the file. Causing any subsequent run of the same script to be successful, causing two instances of the same script to run at the same time (which is what you wanted to prevent by using Sys::RunAlone in the first place). Therefore, make sure that no instances of the script are running (and won't be started by cronjobs while making changes) if you really want to be 100% sure that only one instance of the script is running at the same time.
Inspired by Randal Schwartz's mention of using the DATA handle as a semaphore on the London PM mailing list.
Booking.com for using this heavily in production and allowing me to improve this module.
Copyright (c) 2005, 2006, 2008, 2009, 2011, 2012 Elizabeth Mattijsen <email@example.com>. Copyright (c) 2017 Ben Tilly <firstname.lastname@example.org>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Module Install Instructions
To install Sys::RunAlone, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell install Sys::RunAlone
For more information on module installation, please visit the detailed CPAN module installation guide.
|s||Focus search bar|
|?||Bring up this help dialog|
|g p||Go to pull requests|
|g i||go to github issues (only if github is preferred repository)|
|g a||Go to author|
|g c||Go to changes|
|g i||Go to issues|
|g d||Go to dist|
|g r||Go to repository/SCM|
|g s||Go to source|
|g b||Go to file browse|