Skip to content

Commit 86aa54d

Browse files
committed
feat: add ping to subscribe loop
1 parent 42d66c1 commit 86aa54d

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

src/placeos-driver/subscriptions.cr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,16 @@ class PlaceOS::Driver
119119
end
120120

121121
private def monitor_changes
122+
monitor_count = 0
123+
122124
SimpleRetry.try_to(
123125
base_interval: 1.second,
124126
max_interval: 5.seconds,
125127
randomise: 500.milliseconds
126128
) do
127129
puts "\n\nSTART SUBSCRIPTION MONITORING\n\n"
128130
return if terminated?
131+
monitor_count += 1
129132
wait = Channel(Nil).new
130133
begin
131134
# This will run on redis reconnect
@@ -153,6 +156,11 @@ class PlaceOS::Driver
153156
while details = subscription_channel.receive?
154157
sub, chan = details
155158

159+
if chan == "ping"
160+
redis.ping
161+
next
162+
end
163+
156164
begin
157165
SimpleRetry.try_to(
158166
max_attempts: 4,
@@ -177,7 +185,15 @@ class PlaceOS::Driver
177185
# NOTE:: this version of subscribe only supports splat arguments
178186
redis.subscribe(SYSTEM_ORDER_UPDATE) do |on|
179187
on.message { |c, m| on_message(c, m) }
180-
spawn(same_thread: true) { wait.close }
188+
spawn(same_thread: true) do
189+
instance = monitor_count
190+
wait.close
191+
loop do
192+
sleep 1.second
193+
break if instance != monitor_count
194+
subscription_channel.send({true, "ping"}) rescue nil
195+
end
196+
end
181197
end
182198

183199
raise "no subscriptions, restarting loop" unless terminated?

0 commit comments

Comments
 (0)