summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2021-03-17 10:49:20 +0100
committerSimon Rettberg2021-03-17 10:49:20 +0100
commit778cb1bbe477c7703c5c7a4d112e362091409c0f (patch)
treeb376138138f534550c7594f87b9fa4a0a2a60a22
parent[rebootcontrol] Handle scheduler overlaps (diff)
downloadslx-admin-778cb1bbe477c7703c5c7a4d112e362091409c0f.tar.gz
slx-admin-778cb1bbe477c7703c5c7a4d112e362091409c0f.tar.xz
slx-admin-778cb1bbe477c7703c5c7a4d112e362091409c0f.zip
[locations/rebootcontrol] Formatting
-rw-r--r--modules-available/locations/pages/details.inc.php30
-rw-r--r--modules-available/rebootcontrol/hooks/cron.inc.php19
-rw-r--r--modules-available/rebootcontrol/inc/scheduler.inc.php72
3 files changed, 76 insertions, 45 deletions
diff --git a/modules-available/locations/pages/details.inc.php b/modules-available/locations/pages/details.inc.php
index fd45646b..e400d492 100644
--- a/modules-available/locations/pages/details.inc.php
+++ b/modules-available/locations/pages/details.inc.php
@@ -93,10 +93,16 @@ class SubPage
$options = array();
// Sanity checks
- if ($woloffset > 15) $woloffset = 15;
- else if ($woloffset < 0) $woloffset = 0;
- if ($sdoffset > 15) $sdoffset = 15;
- else if ($sdoffset < 0) $sdoffset = 0;
+ if ($woloffset > 15) {
+ $woloffset = 15;
+ } elseif ($woloffset < 0) {
+ $woloffset = 0;
+ }
+ if ($sdoffset > 15) {
+ $sdoffset = 15;
+ } elseif ($sdoffset < 0) {
+ $sdoffset = 0;
+ }
// Set options
$options['wol'] = $wol;
@@ -115,8 +121,10 @@ class SubPage
private static function getTime($str)
{
$str = explode(':', $str);
- if (count($str) !== 2) return false;
- if ($str[0] < 0 || $str[0] > 23 || $str[1] < 0 || $str[1] > 59) return false;
+ if (count($str) !== 2)
+ return false;
+ if ($str[0] < 0 || $str[0] > 23 || $str[1] < 0 || $str[1] > 59)
+ return false;
return $str[0] * 60 + $str[1];
}
@@ -407,7 +415,8 @@ class SubPage
echo Render::parse('location-subnets', $data);
}
- private static function ajaxOpeningTimes($id) {
+ private static function ajaxOpeningTimes($id)
+ {
User::assertPermission('location.edit', $id);
$openTimes = Database::queryFirst("SELECT openingtime FROM `location` WHERE locationid = :id", array('id' => $id));
if ($openTimes !== false) {
@@ -424,13 +433,16 @@ class SubPage
$data['rebootcontrol'] = $rebootcontrol;
if ($rebootcontrol) {
$res = Database::queryFirst("SELECT * FROM `reboot_scheduler` WHERE locationid = :id", array('id' => $id));
- if ($res !== false) $data['scheduler-options'] = json_decode($res['options']);
+ if ($res !== false) {
+ $data['scheduler-options'] = json_decode($res['options']);
+ }
}
echo Render::parse('ajax-opening-location', $data);
}
- private static function isSimpleMode(&$array) {
+ private static function isSimpleMode(&$array)
+ {
if (empty($array))
return true;
// Decompose by day
diff --git a/modules-available/rebootcontrol/hooks/cron.inc.php b/modules-available/rebootcontrol/hooks/cron.inc.php
index 9c6cbaaf..5ae3b1c8 100644
--- a/modules-available/rebootcontrol/hooks/cron.inc.php
+++ b/modules-available/rebootcontrol/hooks/cron.inc.php
@@ -17,20 +17,27 @@ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
$options = json_decode($row['options'], true);
// Calculate next_execution for the event.
- $location = Database::queryFirst("SELECT openingtime FROM `location` WHERE locationid = :lid", array('lid' => $row['locationid']));
+ $location = Database::queryFirst("SELECT openingtime FROM `location`
+ WHERE locationid = :lid", ['lid' => $row['locationid']]);
Scheduler::updateSchedule($row['locationid'], $options, $location['openingtime']);
- if ($row['nextexecution'] + 1200 < $now) continue;
+ if ($row['nextexecution'] + 1200 < $now)
+ continue;
- $machinedb = Database::simpleQuery("SELECT machineuuid, clientip, macaddr, locationid FROM machine WHERE locationid = :locid", ['locid' => $row['locationid']]);
+ $machinedb = Database::simpleQuery("SELECT machineuuid, clientip, macaddr, locationid FROM machine
+ WHERE locationid = :locid", ['locid' => $row['locationid']]);
$machines = [];
while ($machine = $machinedb->fetch(PDO::FETCH_ASSOC)) {
settype($machine['locationid'], 'int');
$machines[] = $machine;
}
- if ($row['action'] === 'sd') RebootControl::execute($machines, RebootControl::SHUTDOWN, 0);
- else if ($row['action'] === 'wol') RebootControl::wakeMachines($machines);
- else if ($row['action'] === 'rb') RebootControl::execute($machines, RebootControl::REBOOT, 0);
+ if ($row['action'] === 'sd') {
+ RebootControl::execute($machines, RebootControl::SHUTDOWN, 0);
+ } elseif ($row['action'] === 'wol') {
+ RebootControl::wakeMachines($machines);
+ } elseif ($row['action'] === 'rb') {
+ RebootControl::execute($machines, RebootControl::REBOOT, 0);
+ }
}
/*
diff --git a/modules-available/rebootcontrol/inc/scheduler.inc.php b/modules-available/rebootcontrol/inc/scheduler.inc.php
index 0928d20f..7a5c08dc 100644
--- a/modules-available/rebootcontrol/inc/scheduler.inc.php
+++ b/modules-available/rebootcontrol/inc/scheduler.inc.php
@@ -3,33 +3,38 @@
class Scheduler
{
- public static function updateSchedule($locationid, $options, $openingTimes) {
+ public static function updateSchedule($locationid, $options, $openingTimes)
+ {
if ($openingTimes == '') {
self::deleteSchedule($locationid);
return false;
}
$nextexec = self::calculateNext($options, $openingTimes);
$json_options = json_encode($options);
- if ($nextexec !== false) self::upsert($locationid, $nextexec['action'], $nextexec['time'], $json_options);
- else {
+ if ($nextexec !== false) {
+ self::upsert($locationid, $nextexec['action'], $nextexec['time'], $json_options);
+ } else {
// All times are getting ignored because they are within 5 minutes of each other, delete possible db entries.
self::deleteSchedule($locationid);
}
return true;
}
- public static function deleteSchedule($locationid) {
+ public static function deleteSchedule($locationid)
+ {
Database::exec("DELETE FROM `reboot_scheduler`
- WHERE locationid = :lid", array('lid' => $locationid));
+ WHERE locationid = :lid", ['lid' => $locationid]);
}
- private static function calculateNext($options, $openingTimes) {
+ private static function calculateNext($options, $openingTimes)
+ {
$openingTimes = json_decode($openingTimes, true);
$now = time();
$openTimes = [];
$closeTimes = [];
foreach ($openingTimes as $row) {
- if (!$options['wol'] && !$options['sd']) continue;
+ if (!$options['wol'] && !$options['sd'])
+ continue;
if ($options['wol']) {
$open = explode(':', $row['openingtime']);
$openTime = $open[0] * 60 + $open[1] - $options['wol-offset'];
@@ -41,20 +46,26 @@ class Scheduler
foreach ($row['days'] as $day) {
if ($options['wol']) {
$nextOpen = strtotime(date('Y-m-d H:i', strtotime($day . ' ' . $openTime . ' minutes')));
- if ($nextOpen < $now) $openTimes[] = strtotime(date('Y-m-d H:i', strtotime('next '.$day . ' ' . $openTime . ' minutes')));
- else $openTimes[] = $nextOpen;
+ if ($nextOpen < $now) {
+ $openTimes[] = strtotime(date('Y-m-d H:i', strtotime('next ' . $day . ' ' . $openTime . ' minutes')));
+ } else {
+ $openTimes[] = $nextOpen;
+ }
}
if ($options['sd']) {
$nextClose = strtotime(date('Y-m-d H:i', strtotime($day . ' ' . $closeTime . ' minutes')));
- if ($nextClose < $now) $closeTimes[] = strtotime(date('Y-m-d H:i', strtotime('next '.$day . ' ' . $closeTime . ' minutes')));
- else $closeTimes[] = $nextClose;
+ if ($nextClose < $now) {
+ $closeTimes[] = strtotime(date('Y-m-d H:i', strtotime('next ' . $day . ' ' . $closeTime . ' minutes')));
+ } else {
+ $closeTimes[] = $nextClose;
+ }
}
}
}
sort($openTimes);
sort($closeTimes);
- $res = array();
+ $res = [];
if ($options['wol'] && !$options['sd']) {
$res['action'] = 'wol';
@@ -70,7 +81,7 @@ class Scheduler
if (abs($openTimes[$i] - $closeTimes[$i]) < 300) {
// If difference is < 5 min, ignore both events.
continue;
- } else if (abs($openTimes[$i] - $closeTimes[$i]) < 900) {
+ } elseif (abs($openTimes[$i] - $closeTimes[$i]) < 900) {
// If difference is < 15 min, reboot at the earlier time.
$res['action'] = 'rb';
$res['time'] = $openTimes[$i] < $closeTimes[$i] ? $openTimes[$i] : $closeTimes[$i];
@@ -81,7 +92,7 @@ class Scheduler
$res['time'] = $openTimes[$i];
} else {
$res['action'] = 'sd';
- $res['time'] =$closeTimes[$i];
+ $res['time'] = $closeTimes[$i];
}
}
return $res;
@@ -89,29 +100,30 @@ class Scheduler
return false;
}
- private static function upsert($locationid, $action, $nextexec, $options) {
+ private static function upsert($locationid, $action, $nextexec, $options)
+ {
$schedule = Database::queryFirst("SELECT locationid
FROM `reboot_scheduler`
- WHERE locationid = :lid", array(
- 'lid' => $locationid
- ));
+ WHERE locationid = :lid", [
+ 'lid' => $locationid
+ ]);
if ($schedule === false) {
Database::exec("INSERT INTO `reboot_scheduler` (locationid, action, nextexecution, options)
- VALUES (:lid, :act, :next, :opt)", array(
- 'lid' => $locationid,
- 'act' => $action,
- 'next' => $nextexec,
- 'opt' => $options
- ));
+ VALUES (:lid, :act, :next, :opt)", [
+ 'lid' => $locationid,
+ 'act' => $action,
+ 'next' => $nextexec,
+ 'opt' => $options
+ ]);
} else {
Database::exec("UPDATE `reboot_scheduler`
SET action = :act, nextexecution = :next, options = :opt
- WHERE locationid = :lid", array(
- 'act' => $action,
- 'next' => $nextexec,
- 'opt' => $options,
- 'lid' => $locationid
- ));
+ WHERE locationid = :lid", [
+ 'act' => $action,
+ 'next' => $nextexec,
+ 'opt' => $options,
+ 'lid' => $locationid
+ ]);
}
return true;
}