Class: Postburner::Connection
- Inherits:
-
Object
- Object
- Postburner::Connection
- Defined in:
- lib/postburner/connection.rb
Overview
Wrapper around Beaneater connection with automatic reconnection.
Provides a simplified interface to Beanstalkd via Beaneater, with automatic connection management and reconnection on failures. Each worker thread creates its own connection instance for thread safety.
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#beanstalk ⇒ Beaneater::Pool
Returns the underlying Beaneater pool.
-
#clear_tubes!(tube_names = nil) ⇒ Hash
Clears jobs from specified tubes or collects stats for all tubes.
-
#close ⇒ void
Closes the Beanstalkd connection.
-
#connected? ⇒ Boolean
Checks if currently connected to Beanstalkd.
-
#initialize(url = nil) ⇒ Connection
constructor
A new instance of Connection.
-
#reconnect! ⇒ void
Forces reconnection to Beanstalkd.
-
#tubes ⇒ Beaneater::Tubes
Returns the tubes interface for Beanstalkd operations.
Constructor Details
#initialize(url = nil) ⇒ Connection
Returns a new instance of Connection.
23 24 25 26 27 |
# File 'lib/postburner/connection.rb', line 23 def initialize(url = nil) @url = url || Postburner.configuration.beanstalk_url @pool = nil connect! end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
19 20 21 |
# File 'lib/postburner/connection.rb', line 19 def url @url end |
Instance Method Details
#beanstalk ⇒ Beaneater::Pool
Returns the underlying Beaneater pool.
53 54 55 56 |
# File 'lib/postburner/connection.rb', line 53 def beanstalk ensure_connected! @pool end |
#clear_tubes!(tube_names = nil) ⇒ Hash
Clears jobs from specified tubes or collects stats for all tubes.
Low-level method that returns data only (no output to stdout). Delegates to Postburner.stats for collecting statistics. For user-facing output, use Postburner.clear_jobs! instead.
SAFETY: Only allows clearing tubes that are defined in the loaded configuration (watched_tube_names) or the scheduler tube. This prevents accidentally clearing tubes from other applications or environments.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/postburner/connection.rb', line 123 def clear_tubes!(tube_names = nil) ensure_connected! # Validate that tubes to clear are in the loaded configuration (or scheduler tube) if tube_names&.any? allowed = Postburner.watched_tube_names + [Postburner.scheduler_tube_name] invalid_tubes = tube_names - allowed if invalid_tubes.any? raise ArgumentError, <<~ERROR Cannot clear tubes not in configuration. Invalid tubes: #{invalid_tubes.join(', ')} Configured tubes: #{allowed.join(', ')} ERROR end end # Get stats using Postburner.stats result = Postburner.stats(tube_names) result[:cleared] = tube_names&.any? ? true : false # Actually clear if tube names were provided and validated if tube_names&.any? tube_names.each do |tube_name| tubes[tube_name].clear end end result end |
#close ⇒ void
This method returns an undefined value.
Closes the Beanstalkd connection.
87 88 89 90 |
# File 'lib/postburner/connection.rb', line 87 def close @pool&.close rescue nil @pool = nil end |
#connected? ⇒ Boolean
Checks if currently connected to Beanstalkd.
62 63 64 65 66 |
# File 'lib/postburner/connection.rb', line 62 def connected? @pool && @pool.respond_to?(:connection) && @pool.connection rescue false end |
#reconnect! ⇒ void
This method returns an undefined value.
Forces reconnection to Beanstalkd.
Closes existing connection (if any) and establishes a fresh one. Use this when connection has gone stale or after network issues.
78 79 80 81 |
# File 'lib/postburner/connection.rb', line 78 def reconnect! close connect! end |
#tubes ⇒ Beaneater::Tubes
Returns the tubes interface for Beanstalkd operations.
Automatically ensures connection is active before returning.
41 42 43 44 |
# File 'lib/postburner/connection.rb', line 41 def tubes ensure_connected! @pool.tubes end |