Created
May 9, 2015 17:03
-
-
Save singlerider/c4fb4bfd98cec9de1e7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from threading import Thread | |
def initialize(irc, config): | |
# start up the cron jobs. | |
# config should be in the structure of | |
# { | |
# "#channel": [ (period, enabled, callback),.... ] | |
# ... | |
# } | |
for channel, jobs in config.items(): | |
# jobs can be [], False, None... | |
if not jobs: | |
continue | |
for (delay, enabled, callback) in jobs: | |
if not enabled: | |
continue | |
CronJob(irc, channel, delay, callback).start() | |
class CronJob(Thread): | |
def __init__(self, irc, channel, delay, callback): | |
Thread.__init__(self) | |
self.daemon = True | |
self.delay = delay | |
self.callback = callback | |
self.irc = irc | |
self.channel = channel | |
def run(self): | |
while True: | |
time.sleep(self.delay) | |
# print(self.callback, self.channel) | |
self.irc.send_message(self.channel, self.callback(self.channel)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment