Awight has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/346833 )

Change subject: [WIP] Record job state and history in a file
......................................................................

[WIP] Record job state and history in a file

Change-Id: I893579da632fde8df2a1ea6c2e0e564c859dc950
---
A bin/check-job-icinga
M examples/process-control.example.yaml
M processcontrol/config.py
M processcontrol/crontab.py
A processcontrol/job_state.py
5 files changed, 81 insertions(+), 4 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/process-control 
refs/changes/33/346833/1

diff --git a/bin/check-job-icinga b/bin/check-job-icinga
new file mode 100755
index 0000000..f23a0ac
--- /dev/null
+++ b/bin/check-job-icinga
@@ -0,0 +1,18 @@
+#!/usr/bin/python3
+#
+# Report job status in a format that can be consumed by Icinga.
+
+import argparse
+
+
+from processcontrol import job_state
+
+
+if __name__ == "__main__":
+       parser = argparse.ArgumentParser(description="Report the status of a 
`process-control` job.")
+       parser.add_argument("-j", "--job", dest="job", help="Query this job.", 
type=str, required=True)
+       args = parser.parse_args()
+
+       if args.job is not None:
+               state = job_state.load_state(args.job)
+               print(state)
diff --git a/examples/process-control.example.yaml 
b/examples/process-control.example.yaml
index 746af0a..2f43e63 100644
--- a/examples/process-control.example.yaml
+++ b/examples/process-control.example.yaml
@@ -77,6 +77,9 @@
 #
 # TODO: The deb install should create this directory and do something about
 # permissions.
+# TODO: rename to `lock_directory`
 #run_directory: /var/run/process-control
 #
 run_directory: /tmp
+
+state_directory: /var/cache/process-control
diff --git a/processcontrol/config.py b/processcontrol/config.py
index 9934806..806bf3d 100644
--- a/processcontrol/config.py
+++ b/processcontrol/config.py
@@ -63,8 +63,7 @@
         raw value if it's already a list."""
         value = self.get(path)
         if hasattr(value, "encode"):
-            # Is stringlike, so cast to a list and handle along with the plural
-            # case below.
+            # Is stringlike, so cast to a list.
             return [value]
 
         # Otherwise, it's already a list.
diff --git a/processcontrol/crontab.py b/processcontrol/crontab.py
index 82266ca..e22ba0a 100644
--- a/processcontrol/crontab.py
+++ b/processcontrol/crontab.py
@@ -1,5 +1,3 @@
-from __future__ import print_function
-
 from . import config
 from . import job_spec
 
diff --git a/processcontrol/job_state.py b/processcontrol/job_state.py
new file mode 100644
index 0000000..b12c6ea
--- /dev/null
+++ b/processcontrol/job_state.py
@@ -0,0 +1,59 @@
+import yaml
+
+
+from . import config
+
+
+def load_state(slug):
+    state = JobState(slug)
+    state.load()
+
+
+def statefile_path(slug):
+    global_config = config.GlobalConfiguration()
+    path = "{root}/{job}.yaml".format(
+        root=global_config.get("state_directory"),
+        job=slug)
+    return path
+
+
+class JobState(object):
+    """Manage a statefile for each job, with information about recent run
+    history."""
+
+    def __init__(self, slug):
+        self.slug = slug
+        self.path = statefile_path(slug)
+        self.history = []
+
+    def load(self):
+        try:
+            with open(self.path, "r") as f:
+                storage = yaml.safe_load(f)
+        except IOError:
+            # TODO: Might want to remove the file and stuff.
+            return
+
+        self.history = storage["history"]
+
+    def write(self):
+        with open(self.path, "w") as f:
+            yaml.dump({
+                    "history": self.history,
+                }, stream=f)
+
+    def record_started(self, start_time):
+        self.history.append({
+            "status": "started",
+            "time": start_time,
+        })
+        self.write()
+
+    # TODO: We want job duration, etc.
+    def record_success(self):
+        # TODO
+        self.write()
+
+    def record_failure(self):
+        # TODO
+        self.write()

-- 
To view, visit https://gerrit.wikimedia.org/r/346833
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I893579da632fde8df2a1ea6c2e0e564c859dc950
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/process-control
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to