diff options
author | Christian Hesse <mail@eworm.de> | 2021-06-27 23:31:25 +0200 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2021-06-30 21:28:27 +0200 |
commit | 5d30886e597aa3ea0b5bc4ccbf7f62713df0de3d (patch) | |
tree | 04038bcd42bad5d6a27e5dd27b9323277290fd7c /global-functions | |
parent | b2d0ed1240e5ae5a0e48dfd706e8496e6492fd10 (diff) |
global-functions: $ScriptLock: rework with tickets
Getting the order right is not easy... We use a global variable to store
"tickets" in an array. Based on that scripts know their order.
Diffstat (limited to 'global-functions')
-rw-r--r-- | global-functions | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/global-functions b/global-functions index 255c30a..6c11258 100644 --- a/global-functions +++ b/global-functions @@ -907,23 +907,57 @@ :local Script [ :tostr $1 ]; :local DoReturn $2; + :global GetRandomNumber; :global IfThenElse; :global LogPrintExit2; + :global ScriptLockOrder; + :if ([ :typeof $ScriptLockOrder ] = "nothing") do={ + :set ScriptLockOrder [ :toarray "" ]; + } + + :local AddTicket do={ + :return ($1, $2); + } + + :local RemoveTicket do={ + :local Return [ :toarray "" ]; + :foreach Ticket in=$1 do={ + :if ($Ticket != $2) do={ + :set Return ($Return, $Ticket); + } + } + :return $Return; + } + :if ([ :len [ / system script find where name=$Script ] ] = 0) do={ $LogPrintExit2 error $0 ("A script named '" . $Script . "' does not exist!") true; } - :if ([ :len [ / system script job find where script=$Script ] ] = 0) do={ + :local JobCount [ :len [ / system script job find where script=$Script ] ]; + + :if ($JobCount = 0) do={ $LogPrintExit2 error $0 ("No script '" . $Script . "' is running!") true; } - :if ([ :len [ / system script job find where script=$Script ] ] > 1) do={ - $LogPrintExit2 info $0 ("Script " . $Script . " started more than once... Aborting.") \ - [ $IfThenElse ($DoReturn = true) false true ]; - :return true; + :if ([ :len ($ScriptLockOrder->$Script) ] > $JobCount) do={ + $LogPrintExit2 error $0 ("More tickets than running scripts '" . $Script . "', resetting!") false; + :set ($ScriptLockOrder->$Script); + / system script job remove [ find where script=$Script ]; + } + + :local MyTicket [ $GetRandomNumber ]; + :set ($ScriptLockOrder->$Script) [ $AddTicket ($ScriptLockOrder->$Script) $MyTicket ]; + + :if ([ :len ($ScriptLockOrder->$Script) ] = $JobCount && ($ScriptLockOrder->$Script->0) = $MyTicket) do={ + :set ($ScriptLockOrder->$Script) [ $RemoveTicket ($ScriptLockOrder->$Script) $MyTicket ]; + :return false; } - :return false; + + :set ($ScriptLockOrder->$Script) [ $RemoveTicket ($ScriptLockOrder->$Script) $MyTicket ]; + $LogPrintExit2 info $0 ("Script '" . $Script . "' started more than once... Aborting.") \ + [ $IfThenElse ($DoReturn = true) false true ]; + :return true; } # send notification via e-mail - expects at lease two string arguments |