From b1e13893ff6e94722aee9077941a79857a41523f Mon Sep 17 00:00:00 2001 From: Patrick Barnum Date: Fri, 18 Oct 2024 23:44:06 -0700 Subject: [PATCH] [RFC] Adds Ping() to client/scheduler/server (#585) * [RFC] Adds Ping() to client/scheduler/server * Checks for scheduler state closed --- client.go | 5 +++++ scheduler.go | 11 +++++++++++ server.go | 15 ++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index e440d097..aec50b13 100644 --- a/client.go +++ b/client.go @@ -419,6 +419,11 @@ func (c *Client) EnqueueContext(ctx context.Context, task *Task, opts ...Option) return newTaskInfo(msg, state, opt.processAt, nil), nil } +// Ping performs a ping against the redis connection. +func (c *Client) Ping() error { + return c.broker.Ping() +} + func (c *Client) enqueue(ctx context.Context, msg *base.TaskMessage, uniqueTTL time.Duration) error { if uniqueTTL > 0 { return c.broker.EnqueueUnique(ctx, msg, uniqueTTL) diff --git a/scheduler.go b/scheduler.go index 89ec2efc..5f0b05da 100644 --- a/scheduler.go +++ b/scheduler.go @@ -336,3 +336,14 @@ func (s *Scheduler) clearHistory() { } } } + +// Ping performs a ping against the redis connection. +func (s *Scheduler) Ping() error { + s.state.mu.Lock() + defer s.state.mu.Unlock() + if s.state.value == srvStateClosed { + return nil + } + + return s.rdb.Ping() +} diff --git a/server.go b/server.go index 41b4eaba..e4053991 100644 --- a/server.go +++ b/server.go @@ -759,7 +759,7 @@ func (srv *Server) Shutdown() { func (srv *Server) Stop() { srv.state.mu.Lock() if srv.state.value != srvStateActive { - // Invalid calll to Stop, server can only go from Active state to Stopped state. + // Invalid call to Stop, server can only go from Active state to Stopped state. srv.state.mu.Unlock() return } @@ -770,3 +770,16 @@ func (srv *Server) Stop() { srv.processor.stop() srv.logger.Info("Processor stopped") } + +// Ping performs a ping against the redis connection. +// +// This is an alternative to the HealthCheckFunc available in the Config object. +func (srv *Server) Ping() error { + srv.state.mu.Lock() + defer srv.state.mu.Unlock() + if srv.state.value == srvStateClosed { + return nil + } + + return srv.broker.Ping() +}