mirror of
https://git.eworm.de/cgit/routeros-scripts
synced 2025-07-09 09:34:29 +02:00
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.
This commit is contained in:
parent
b2d0ed1240
commit
5d30886e59
1 changed files with 40 additions and 6 deletions
|
@ -907,23 +907,57 @@
|
||||||
:local Script [ :tostr $1 ];
|
:local Script [ :tostr $1 ];
|
||||||
:local DoReturn $2;
|
:local DoReturn $2;
|
||||||
|
|
||||||
|
:global GetRandomNumber;
|
||||||
:global IfThenElse;
|
:global IfThenElse;
|
||||||
:global LogPrintExit2;
|
: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={
|
:if ([ :len [ / system script find where name=$Script ] ] = 0) do={
|
||||||
$LogPrintExit2 error $0 ("A script named '" . $Script . "' does not exist!") true;
|
$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;
|
$LogPrintExit2 error $0 ("No script '" . $Script . "' is running!") true;
|
||||||
}
|
}
|
||||||
|
|
||||||
:if ([ :len [ / system script job find where script=$Script ] ] > 1) do={
|
:if ([ :len ($ScriptLockOrder->$Script) ] > $JobCount) do={
|
||||||
$LogPrintExit2 info $0 ("Script " . $Script . " started more than once... Aborting.") \
|
$LogPrintExit2 error $0 ("More tickets than running scripts '" . $Script . "', resetting!") false;
|
||||||
[ $IfThenElse ($DoReturn = true) false true ];
|
:set ($ScriptLockOrder->$Script);
|
||||||
:return true;
|
/ system script job remove [ find where script=$Script ];
|
||||||
}
|
}
|
||||||
:return false;
|
|
||||||
|
: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;
|
||||||
|
}
|
||||||
|
|
||||||
|
: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
|
# send notification via e-mail - expects at lease two string arguments
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue