Spaces:
Sleeping
Sleeping
Create policy/kill_switch.py
Browse files- policy/kill_switch.py +26 -0
policy/kill_switch.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
|
| 3 |
+
class KillSwitch:
|
| 4 |
+
def __init__(self):
|
| 5 |
+
self.disabled = {}
|
| 6 |
+
self.cooldown = 3600 # 1 hour
|
| 7 |
+
|
| 8 |
+
def disable(self, agency, reason):
|
| 9 |
+
self.disabled[agency] = {
|
| 10 |
+
"reason": reason,
|
| 11 |
+
"time": time.time()
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
def is_enabled(self, agency):
|
| 15 |
+
if agency not in self.disabled:
|
| 16 |
+
return True
|
| 17 |
+
|
| 18 |
+
elapsed = time.time() - self.disabled[agency]["time"]
|
| 19 |
+
if elapsed > self.cooldown:
|
| 20 |
+
del self.disabled[agency]
|
| 21 |
+
return True
|
| 22 |
+
|
| 23 |
+
return False
|
| 24 |
+
|
| 25 |
+
def reason(self, agency):
|
| 26 |
+
return self.disabled.get(agency, {}).get("reason", "")
|