Skip to content

Instantly share code, notes, and snippets.

@singlerider
Created May 9, 2015 17:03
Show Gist options
  • Save singlerider/c4fb4bfd98cec9de1e7e to your computer and use it in GitHub Desktop.
Save singlerider/c4fb4bfd98cec9de1e7e to your computer and use it in GitHub Desktop.
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