From 367f6c0a56873766abdb3fc1ba3db9c96614a842 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Fri, 12 Oct 2012 17:19:49 -0400 Subject: [PATCH] zephyr_mirror: Run both processes within a single application. (imported from commit 8f379a682ede7076ddcbe046dee5be6592a5bbd9) --- api/common.py | 5 +---- api/zephyr_mirror.py | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/api/common.py b/api/common.py index 294f875e73..425a7e17a1 100644 --- a/api/common.py +++ b/api/common.py @@ -61,7 +61,7 @@ class HumbugAPI(): request["streams"] = simplejson.dumps(streams) return self.do_api_query(request, "/api/v1/subscribe") - def call_on_each_message(self, callback, idle_call = None, options = {}): + def call_on_each_message(self, callback, options = {}): max_message_id = None while True: if max_message_id is not None: @@ -82,6 +82,3 @@ class HumbugAPI(): for message in sorted(res['messages'], key=lambda x: int(x["id"])): max_message_id = max(max_message_id, int(message["id"])) callback(message) - - if idle_call is not None: - idle_call() diff --git a/api/zephyr_mirror.py b/api/zephyr_mirror.py index fd0711db8c..d088b6be87 100755 --- a/api/zephyr_mirror.py +++ b/api/zephyr_mirror.py @@ -11,6 +11,7 @@ import optparse import os import datetime import textwrap +import signal from urllib2 import HTTPError root_path = "/mit/tabbott/for_friends" @@ -61,9 +62,6 @@ humbug_client = api.common.HumbugAPI(email=os.environ["USER"] + "@mit.edu", site=options.site) start_time = time.time() -import zephyr -zephyr.init() -subs = zephyr.Subscriptions() def humbug_username(zephyr_username): return zephyr_username.lower().split("@")[0] + "@mit.edu" @@ -142,6 +140,7 @@ def maybe_restart_mirroring_script(): os.stat(root_path + "/tabbott_stamp").st_mtime > start_time): print "%s: zephyr mirroring script has been updated; restarting..." % \ (datetime.datetime.now()) + os.kill(child_pid, signal.SIGKILL) while True: try: os.execvp(root_path + "/zephyr_mirror.py", sys.argv) @@ -344,7 +343,6 @@ def humbug_to_zephyr(options): # Sync messages from zephyr to humbug print "%s: humbug=>zephyr: Starting syncing messages." % (datetime.datetime.now(),) humbug_client.call_on_each_message(maybe_forward_to_zephyr, - idle_call=maybe_restart_mirroring_script, options={"mit_sync_bot": 'yes'}) def subscribed_to_mail_messages(): @@ -389,6 +387,18 @@ def parse_zephyr_subs(verbose=False): return zephyr_subscriptions if options.forward_from_humbug: + print "This option is obsolete." + sys.exit(0) + +child_pid = os.fork() +if child_pid == 0: + # Run the humbug => zephyr mirror in the child + import zephyr + zephyr.init() humbug_to_zephyr(options) -else: - zephyr_to_humbug(options) + sys.exit(0) + +import zephyr +zephyr.init() +subs = zephyr.Subscriptions() +zephyr_to_humbug(options)