summaryrefslogtreecommitdiffstats
path: root/library/Director/Deployment/DeploymentGracePeriod.php
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--library/Director/Deployment/DeploymentGracePeriod.php61
1 files changed, 61 insertions, 0 deletions
diff --git a/library/Director/Deployment/DeploymentGracePeriod.php b/library/Director/Deployment/DeploymentGracePeriod.php
new file mode 100644
index 0000000..6cde25a
--- /dev/null
+++ b/library/Director/Deployment/DeploymentGracePeriod.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace Icinga\Module\Director\Deployment;
+
+use Icinga\Module\Director\Db;
+use Icinga\Module\Director\Objects\DirectorDeploymentLog;
+
+class DeploymentGracePeriod
+{
+ /** @var int */
+ protected $graceTimeSeconds;
+
+ /** @var Db */
+ protected $db;
+
+ /**
+ * @param int $graceTimeSeconds
+ * @param Db $db
+ */
+ public function __construct($graceTimeSeconds, Db $db)
+ {
+ $this->graceTimeSeconds = $graceTimeSeconds;
+ $this->db = $db;
+ }
+
+ /**
+ * Whether we're still within a grace period
+ * @return bool
+ */
+ public function isActive()
+ {
+ if ($deployment = $this->lastDeployment()) {
+ return $deployment->getDeploymentTimestamp() > $this->getGracePeriodStart();
+ }
+
+ return false;
+ }
+
+ protected function getGracePeriodStart()
+ {
+ return time() - $this->graceTimeSeconds;
+ }
+
+ public function getRemainingGraceTime()
+ {
+ if ($this->isActive()) {
+ if ($deployment = $this->lastDeployment()) {
+ return $deployment->getDeploymentTimestamp() - $this->getGracePeriodStart();
+ } else {
+ return null;
+ }
+ }
+
+ return 0;
+ }
+
+ protected function lastDeployment()
+ {
+ return DirectorDeploymentLog::optionalLatest($this->db);
+ }
+}