Skip to content

Commit 2d7b850

Browse files
committed
feat(protocol): add a redis health check
1 parent 71325c7 commit 2d7b850

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: placeos-driver
2-
version: 7.13.3
2+
version: 7.14.0
33

44
dependencies:
55
action-controller:

spec/helper.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class PlaceOS::Driver::ProcessManagerMock
1515

1616
class_getter callbacks : Hash(String, Proc(Request, Request?)) = {} of String => Request -> Request?
1717

18+
getter terminated : Channel(Nil) { Channel(Nil).new }
19+
1820
def self.register(name : String, &callback : Request -> Request?)
1921
ProcessManagerMock.callbacks[name] = callback
2022
end

src/placeos-driver/protocol.cr

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class PlaceOS::Driver::Protocol
195195

196196
private def produce_io(timeout_period)
197197
spawn(same_thread: true) { self.process! }
198+
spawn { self.redis_health_check }
198199

199200
# Ensures all outgoing event processing is done on the same thread
200201
@timeouts = Tasker.every(timeout_period) do
@@ -276,4 +277,30 @@ class PlaceOS::Driver::Protocol
276277
@processor.close
277278
@timeouts.try &.cancel
278279
end
280+
281+
private def redis_health_check
282+
failures = 0
283+
loop do
284+
return if @process_manager.terminated.closed?
285+
286+
time = (50 + rand(10)).seconds
287+
select
288+
when @process_manager.terminated.receive?
289+
return
290+
when timeout(time)
291+
# perform health check
292+
begin
293+
::PlaceOS::Driver::RedisStorage.with_redis(&.ping)
294+
failures = 0
295+
rescue error
296+
failures += 1
297+
raise error if failures >= 2
298+
Log.warn(exception: error) { "redis healthcheck failed - retrying" }
299+
end
300+
end
301+
end
302+
rescue error
303+
Log.fatal(exception: error) { "redis healthcheck failed - terminating process" }
304+
@process_manager.terminate
305+
end
279306
end

0 commit comments

Comments
 (0)