From 17dbb806ce34ca6479e42bd3529b86fa8d3be31d Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Fri, 21 Sep 2012 14:31:37 +0200 Subject: [PATCH] poll: be more resilient to restarts When restarting or reconnecting to servers, the channel objects may change, causing odd crashes when counting votes during poll closure. This is easily fixed by letting poll.channel hold a string rather than an object, but past polls reloaded from the registry will already have actual Channel object stored, so we need to convert them. Do this, and keep track of whether it has been done or not. --- data/rbot/plugins/poll.rb | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/data/rbot/plugins/poll.rb b/data/rbot/plugins/poll.rb index 4dc28e69..dc6a214d 100644 --- a/data/rbot/plugins/poll.rb +++ b/data/rbot/plugins/poll.rb @@ -19,7 +19,7 @@ class ::Poll def initialize(originating_message, question, answers, duration) @author = originating_message.sourcenick - @channel = originating_message.channel + @channel = originating_message.channel.name @question = question @running = false @duration = duration @@ -118,7 +118,33 @@ class PollPlugin < Plugin init_reg_entry :running, Hash.new init_reg_entry :archives, Hash.new init_reg_entry :last_poll_id, 0 - running = @registry[:running] + + # Maintenance: a registry to keep track of + # maintenance tasks that (should) have been done + init_reg_entry :maintenance, Hash.new + + maint = @registry[:maintenance] + unless maint[:channel_name_fix] + # Fix: poll.channel should be a String, not a Channel + + running = @registry[:running] + running.each do |id, poll| + # fix channel: it should be a string, not a Channel object + poll.channel = poll.channel.name unless String === poll.channel + end + @registry[:running] = running + + archives = @registry[:archives] + archives.each do |id, poll| + # fix channel: it should be a string, not a Channel object + poll.channel = poll.channel.name unless String === poll.channel + end + @registry[:archives] = archives + + main[:channel_name_fix] = true + end + + # Restart running polls, close expired polls now = Time.now running.each do |id, poll| duration = poll.ends_at - Time.now @@ -130,6 +156,7 @@ class PollPlugin < Plugin count_votes(poll.id) end end + end def authors_running_count(victim) @@ -144,7 +171,6 @@ class PollPlugin < Plugin def start(m, params) author = m.sourcenick - chan = m.channel max_concurrent = @bot.config['poll.max_concurrent_polls'] if authors_running_count(author) == max_concurrent -- 2.32.0.93.g670b81a890