Rewrite bot to avoid using two fibers and to handle errors in network.

This commit is contained in:
Danila Fedorin 2019-04-14 22:39:15 -07:00
parent 259bed9823
commit 8c8f7beb5c

View File

@ -81,30 +81,19 @@ module Telepathy
end end
end end
private def spawn_workers private def spawn_worker
spawn do spawn do
loop do while @poll_running
action = @poll_channel.receive begin
case action updates = get_updates 10
when Int64? process_updates updates
@last_update_id = action.nil? ? @last_update_id : action + 1 @last_update_id = updates.last?.try &.update_id.+(1) || @last_update_id
@update_channel.send get_updates 10 rescue
when Control
break
end end
end end
end
spawn do @poll_end_hooks.each do |hook|
loop do hook.call
item = @update_channel.receive
case item
when Array(Update)
process_updates(item)
@poll_channel.send item.last?.try &.update_id
when Control
@poll_channel.send item
break
end
end end
end end
end end
@ -112,18 +101,15 @@ module Telepathy
def poll def poll
if !@poll_running if !@poll_running
@poll_running = true @poll_running = true
spawn_workers @poll_start_hooks.each do |hook|
@poll_channel.send nil hook.call
@poll_start_hooks.each &.call end
spawn_worker
end end
end end
def end_poll def end_poll
if @poll_running
@poll_running = false @poll_running = false
@poll_channel.send Control::Done
@poll_end_hooks.each &.call
end
end end
end end
end end