Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/management/bin/start_services.py

Issue 8963083: sitescripts: Script to keep services alive (Closed)
Patch Set: Created Nov. 29, 2012, 2:47 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/management/bin/start_services.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/sitescripts/management/bin/start_services.py
@@ -0,0 +1,63 @@
+# coding: utf-8
+
+# This file is part of the Adblock Plus web scripts,
+# Copyright (C) 2006-2012 Eyeo GmbH
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+
+import os
+from sitescripts.utils import get_config
+
+def _get_services():
+ config = get_config()
+ section_name = "keep_alive_services"
+ section_keys = config.options(section_name)
+ default_keys = config.defaults().keys()
+ keys = set(section_keys) - set(default_keys)
+
+ services = {}
+ for key in keys:
+ services[key] = config.get(section_name, key)
+ return services
+
+def _files_exist(files):
+ for file in files:
+ if not os.path.exists(file):
+ print "'%s' does not exist." % file
+ return False
+ return True
+
+def _process_running(pid):
+ return True
Wladimir Palant 2012/11/29 15:01:46 That still needs to be written, right?
Felix Dahlke 2012/11/30 09:49:32 Um, yes. That's sort of important :) Done.
+
+if __name__ == "__main__":
+ services = _get_services()
+ for service in services.keys():
+ init_file = "/etc/init.d/%s" % service
+ pid_file = "/var/run/%s" % services[service]
Wladimir Palant 2012/11/29 15:01:46 Use os.path.join() here?
Felix Dahlke 2012/11/30 09:49:32 Done.
+
+ if not _files_exist([init_file, pid_file]):
Wladimir Palant 2012/11/29 15:01:46 Actually, if the pid file doesn't exist it means t
Felix Dahlke 2012/11/30 09:49:32 Done.
+ continue
+
+ pid_file_content = open(pid_file).read().rstrip()
Wladimir Palant 2012/11/29 15:01:46 I generally prefer to close files explicitly...
Felix Dahlke 2012/11/30 09:49:32 Done.
+ try:
+ pid = int(pid_file_content)
+ except exceptions.ValueError:
+ print "'%s' is not a PID." % pid_file_content
+ continue
+
+ if _process_running(pid):
+ continue
+
+ print "%s is not running, starting ..." % service
Wladimir Palant 2012/11/29 15:01:46 You are aware that any output from a cron job resu
Felix Dahlke 2012/11/30 09:49:32 Yes, that's what I intended. A crashing service is
+ subprocess.call([init_file, "start"])
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld