entirely reworked the timer system to work with the event system, will eventually persist all timers to database until complete in a central way.

This commit is contained in:
jesopo 2016-04-19 12:21:27 +01:00
parent ce4f0f68b0
commit 9eff1b8931
No known key found for this signature in database
GPG key ID: 0BBDEB2AEFCFFCB3
4 changed files with 29 additions and 24 deletions

View file

@ -13,6 +13,7 @@ class Bot(object):
self.modules = ModuleManager.ModuleManager(self) self.modules = ModuleManager.ModuleManager(self)
self.events = EventManager.EventHook(self) self.events = EventManager.EventHook(self)
self.timers = [] self.timers = []
self.events.on("timer").on("reconnect").hook(self.reconnect)
def add_server(self, id, hostname, port, password, ipv4, tls, def add_server(self, id, hostname, port, password, ipv4, tls,
nickname, username, realname, connect=False): nickname, username, realname, connect=False):
@ -35,8 +36,8 @@ class Bot(object):
for server in self.servers.values(): for server in self.servers.values():
self.connect(server) self.connect(server)
def add_timer(self, function, delay, *args, **kwargs): def add_timer(self, event_name, delay, **kwargs):
timer = Timer.Timer(function, delay, *args, **kwargs) timer = Timer.Timer(self, event_name, delay, **kwargs)
timer.set_started_time() timer.set_started_time()
self.timers.append(timer) self.timers.append(timer)
def next_timer(self): def next_timer(self):
@ -49,6 +50,7 @@ class Bot(object):
def call_timers(self): def call_timers(self):
for timer in self.timers[:]: for timer in self.timers[:]:
if timer.due(): if timer.due():
print(timer.event_name)
timer.call() timer.call()
if timer.done(): if timer.done():
self.timers.remove(timer) self.timers.remove(timer)
@ -69,14 +71,16 @@ class Bot(object):
self.poll.unregister(server.fileno()) self.poll.unregister(server.fileno())
del self.servers[server.fileno()] del self.servers[server.fileno()]
def reconnect(self, timer, server): def reconnect(self, event):
server = event["server"]
IRCServer.Server.__init__(server, server.id, server.target_hostname, IRCServer.Server.__init__(server, server.id, server.target_hostname,
server.port, server.password, server.ipv4, server.tls, server.port, server.password, server.ipv4, server.tls,
server.nickname, server.username, server.realname, self) server.original_nickname, server.original_username,
server.original_realname, self)
if self.connect(server): if self.connect(server):
self.servers[server.fileno()] = server self.servers[server.fileno()] = server
else: else:
timer.redo() event["timer"].redo()
def set_setting(self, setting, value): def set_setting(self, setting, value):
self.database.set_bot_setting(setting, value) self.database.set_bot_setting(setting, value)
@ -120,7 +124,7 @@ class Bot(object):
self.disconnect(server) self.disconnect(server)
reconnect_delay = self.config.get("reconnect-delay", 10) reconnect_delay = self.config.get("reconnect-delay", 10)
self.add_timer(self.reconnect, reconnect_delay, server) self.add_timer("reconnect", reconnect_delay, server=server)
print("disconnected from %s, reconnecting in %d seconds" % ( print("disconnected from %s, reconnecting in %d seconds" % (
str(server), reconnect_delay)) str(server), reconnect_delay))

View file

@ -12,9 +12,9 @@ class Server(object):
self.tls = tls self.tls = tls
self.password = password self.password = password
self.ipv4 = ipv4 self.ipv4 = ipv4
self.nickname = nickname self.original_nickname = nickname
self.username = username or nickname self.original_username = username or nickname
self.realname = realname or nickname self.original_realname = realname or nickname
self.write_buffer = b"" self.write_buffer = b""
self.read_buffer = b"" self.read_buffer = b""
self.users = {} self.users = {}
@ -53,8 +53,8 @@ class Server(object):
self.socket.connect((self.target_hostname, self.port)) self.socket.connect((self.target_hostname, self.port))
if self.password: if self.password:
self.send_pass(self.password) self.send_pass(self.password)
self.send_user(self.username, self.realname) self.send_user(self.original_username, self.original_realname)
self.send_nick(self.nickname) self.send_nick(self.original_nickname)
self.connected = True self.connected = True
def disconnect(self): def disconnect(self):
self.connected = False self.connected = False

View file

@ -1,11 +1,11 @@
import time import time
class Timer(object): class Timer(object):
def __init__(self, function, delay, *args, **kwargs): def __init__(self, bot, event_name, delay, **kwargs):
self.function = function self.bot = bot
self.event_name = event_name
self.delay = delay self.delay = delay
self.kwargs = kwargs self.kwargs = kwargs
self.args = args
self._done = False self._done = False
self.call_count = 0 self.call_count = 0
@ -21,7 +21,8 @@ class Timer(object):
def call(self): def call(self):
self._done = True self._done = True
self.call_count +=1 self.call_count +=1
self.function(self, *self.args, **self.kwargs) self.bot.events.on("timer").on(self.event_name).call(
timer=self, **self.kwargs)
def redo(self): def redo(self):
self._done = False self._done = False

View file

@ -12,6 +12,7 @@ class Module(object):
help="Set a reminder", usage="<time> <message>") help="Set a reminder", usage="<time> <message>")
bot.events.on("received").on("numeric").on("001").hook( bot.events.on("received").on("numeric").on("001").hook(
self.on_connect) self.on_connect)
bot.events.on("timer").on("in").hook(self.timer_due)
def on_connect(self, event): def on_connect(self, event):
self.load_reminders(event["server"]) self.load_reminders(event["server"])
@ -36,7 +37,7 @@ class Module(object):
for target, due_time, server_id, nickname, message in user_reminders[1]: for target, due_time, server_id, nickname, message in user_reminders[1]:
time_left = due_time-time.time() time_left = due_time-time.time()
if time_left > 0: if time_left > 0:
self.bot.add_timer(self.timer_due, time_left, target=target, self.bot.add_timer("in", time_left, target=target,
due_time=due_time, server_id=server_id, nickname=nickname, due_time=due_time, server_id=server_id, nickname=nickname,
message=message) message=message)
else: else:
@ -56,7 +57,7 @@ class Module(object):
event["server"].id, event["user"].nickname, message]) event["server"].id, event["user"].nickname, message])
event["server"].set_setting(setting, reminders) event["server"].set_setting(setting, reminders)
self.bot.add_timer(self.timer_due, seconds, self.bot.add_timer("in", seconds,
target=event["target"].name, due_time=due_time, target=event["target"].name, due_time=due_time,
server_id=event["server"].id, nickname=event["user"].nickname, server_id=event["server"].id, nickname=event["user"].nickname,
message=message) message=message)
@ -69,13 +70,12 @@ class Module(object):
event["stderr"].write( event["stderr"].write(
"Please provided a valid time above 0 seconds") "Please provided a valid time above 0 seconds")
def timer_due(self, timer, **kwargs): def timer_due(self, event):
for server in self.bot.servers.values(): for server in self.bot.servers.values():
if kwargs["server_id"] == server.id: if event["server_id"] == server.id:
server.send_message(kwargs["target"], server.send_message(event["target"],
"%s, this is your reminder: %s" % ( "%s, this is your reminder: %s" % (
kwargs["nickname"], kwargs["message"])) event["nickname"], event["message"]))
break break
setting = "in-%s" % kwargs["nickname"] self.remove_timer(event["target"], event["due_time"],
self.remove_timer(kwargs["target"], kwargs["due_time"], event["server_id"], event["nickname"], event["message"])
kwargs["server_id"], kwargs["nickname"], kwargs["message"])