zephyr_mirror: Run both processes within a single application.

(imported from commit 8f379a682ede7076ddcbe046dee5be6592a5bbd9)
This commit is contained in:
Tim Abbott 2012-10-12 17:19:49 -04:00
parent b87524bb8d
commit d22a77aac6
2 changed files with 17 additions and 10 deletions

View file

@ -61,7 +61,7 @@ class HumbugAPI():
request["streams"] = simplejson.dumps(streams) request["streams"] = simplejson.dumps(streams)
return self.do_api_query(request, "/api/v1/subscribe") 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 max_message_id = None
while True: while True:
if max_message_id is not None: 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"])): for message in sorted(res['messages'], key=lambda x: int(x["id"])):
max_message_id = max(max_message_id, int(message["id"])) max_message_id = max(max_message_id, int(message["id"]))
callback(message) callback(message)
if idle_call is not None:
idle_call()

View file

@ -11,6 +11,7 @@ import optparse
import os import os
import datetime import datetime
import textwrap import textwrap
import signal
from urllib2 import HTTPError from urllib2 import HTTPError
root_path = "/mit/tabbott/for_friends" root_path = "/mit/tabbott/for_friends"
@ -61,9 +62,6 @@ humbug_client = api.common.HumbugAPI(email=os.environ["USER"] + "@mit.edu",
site=options.site) site=options.site)
start_time = time.time() start_time = time.time()
import zephyr
zephyr.init()
subs = zephyr.Subscriptions()
def humbug_username(zephyr_username): def humbug_username(zephyr_username):
return zephyr_username.lower().split("@")[0] + "@mit.edu" 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): os.stat(root_path + "/tabbott_stamp").st_mtime > start_time):
print "%s: zephyr mirroring script has been updated; restarting..." % \ print "%s: zephyr mirroring script has been updated; restarting..." % \
(datetime.datetime.now()) (datetime.datetime.now())
os.kill(child_pid, signal.SIGKILL)
while True: while True:
try: try:
os.execvp(root_path + "/zephyr_mirror.py", sys.argv) os.execvp(root_path + "/zephyr_mirror.py", sys.argv)
@ -344,7 +343,6 @@ def humbug_to_zephyr(options):
# Sync messages from zephyr to humbug # Sync messages from zephyr to humbug
print "%s: humbug=>zephyr: Starting syncing messages." % (datetime.datetime.now(),) print "%s: humbug=>zephyr: Starting syncing messages." % (datetime.datetime.now(),)
humbug_client.call_on_each_message(maybe_forward_to_zephyr, humbug_client.call_on_each_message(maybe_forward_to_zephyr,
idle_call=maybe_restart_mirroring_script,
options={"mit_sync_bot": 'yes'}) options={"mit_sync_bot": 'yes'})
def subscribed_to_mail_messages(): def subscribed_to_mail_messages():
@ -389,6 +387,18 @@ def parse_zephyr_subs(verbose=False):
return zephyr_subscriptions return zephyr_subscriptions
if options.forward_from_humbug: 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) humbug_to_zephyr(options)
else: sys.exit(0)
import zephyr
zephyr.init()
subs = zephyr.Subscriptions()
zephyr_to_humbug(options) zephyr_to_humbug(options)