Class: Timberline

Inherits:
Object
  • Object
show all
Defined in:
lib/timberline.rb,
lib/timberline/queue.rb,
lib/timberline/config.rb,
lib/timberline/worker.rb,
lib/timberline/version.rb,
lib/timberline/envelope.rb,
lib/timberline/exceptions.rb,
lib/timberline/anonymous_worker.rb

Overview

The Timberline class serves as a base namespace for Timberline libraries, but also provides some convenience methods for accessing queues and quickly and easily processing items.

Defined Under Namespace

Classes: AnonymousWorker, Config, Envelope, ItemErrored, ItemRetried, MissingContentException, Queue, Worker

Constant Summary collapse

VERSION =

The current canonical version for Timberline.

"0.8.6"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



24
25
26
# File 'lib/timberline.rb', line 24

def config
  @config
end

.watch_procObject

Returns the value of attribute watch_proc.



25
26
27
# File 'lib/timberline.rb', line 25

def watch_proc
  @watch_proc
end

Class Method Details

.all_queuesArray<Timberline::Queue>

Returns a list of all non-hidden queues for this instance of Timberline.

Returns:

  • (Array<Timberline::Queue>)

    a list of all non-hidden queues for this instance of Timberline



72
73
74
# File 'lib/timberline.rb', line 72

def self.all_queues
  Timberline.redis.smembers("timberline_queue_names").map { |name| queue(name) }
end

.configure(&block) {|@config| ... } ⇒ Object

Method for providing custom configuration by yielding the config object. Lazy-loads the Timberline configuration.

Parameters:

  • block (Block)

    a block that accepts and manipulates a Timberline::Config

Yields:



118
119
120
121
# File 'lib/timberline.rb', line 118

def self.configure(&block)
  initialize_if_necessary
  yield @config
end

.error_item(item) ⇒ Object

Convenience method to error out a queue item



97
98
99
100
# File 'lib/timberline.rb', line 97

def self.error_item(item)
  origin_queue = queue(item.origin_queue)
  origin_queue.error_item(item)
end

.log_job_results?Boolean

Lazy-loads the Timberline configuration.

Returns:

  • (Boolean)

    whether we want to record result stats for each job in a redis queue



146
147
148
149
# File 'lib/timberline.rb', line 146

def self.log_job_results?
  initialize_if_necessary
  @config.log_job_result_stats
end

.max_retriesInteger

Lazy-loads the Timberline configuration.

Returns:

  • (Integer)

    the maximum number of retries



125
126
127
128
# File 'lib/timberline.rb', line 125

def self.max_retries
  initialize_if_necessary
  @config.max_retries
end

.pause(queue_name) ⇒ Object

Convenience method to pause a Queue by name.



104
105
106
# File 'lib/timberline.rb', line 104

def self.pause(queue_name)
  queue(queue_name).pause
end

.push(queue_name, data, metadata = {}) ⇒ Object

Convenience method to push an item onto a queue



84
85
86
# File 'lib/timberline.rb', line 84

def self.push(queue_name, data, ={})
  queue(queue_name).push(data, )
end

.queue(queue_name, opts = {}) ⇒ Object

Convenience method to create a new Queue object



78
79
80
# File 'lib/timberline.rb', line 78

def self.queue(queue_name, opts = {})
  Queue.new(queue_name, opts)
end

.redisRedis::Namespace

Obtain a reference to the redis connection that Timberline is using.

If Timberline has not already been configured, this method will initialize a new Timberline::Config first.

If a Redis connection has not yet been established, this method will establish one.

Returns:

  • (Redis::Namespace)


61
62
63
64
65
66
67
68
# File 'lib/timberline.rb', line 61

def self.redis
  initialize_if_necessary
  if @redis.nil?
    self.redis = Redis.new(@config.redis_config)
  end

  @redis
end

.redis=(server) ⇒ Object

Update the redis server that Timberline uses for its connections.

If Timberline has not already been configured, this method will initialize a new Timberline::Config first.

Parameters:

  • server (Redis, Redis::Namespace, nil)

    if Redis, wraps it in a namespace. if Redis::Namespace, uses that namespace directly. If nil, clears out any reference to the existing redis server.

Raises:

  • (StandardError)

    if server is not an instance of Redis, Redis::Namespace, or nil.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/timberline.rb', line 39

def self.redis=(server)
  initialize_if_necessary
  if server.is_a? Redis
    @redis = Redis::Namespace.new(@config.namespace, redis: server)
  elsif server.is_a? Redis::Namespace
    @redis = server
  elsif server.nil?
    @redis = nil
  else
    raise "Not a valid Redis connection."
  end
end

.retry_item(item) ⇒ Object

Convenience method to retry a queue item



90
91
92
93
# File 'lib/timberline.rb', line 90

def self.retry_item(item)
  origin_queue = queue(item.origin_queue)
  origin_queue.retry_item(item)
end

.stat_timeoutInteger

Lazy-loads the Timberline configuration.

Returns:

  • (Integer)

    the stat_timeout expressed in minutes



132
133
134
135
# File 'lib/timberline.rb', line 132

def self.stat_timeout
  initialize_if_necessary
  @config.stat_timeout
end

.stat_timeout_secondsInteger

Lazy-loads the Timberline configuration.

Returns:

  • (Integer)

    the stat_timeout expressed in seconds



139
140
141
142
# File 'lib/timberline.rb', line 139

def self.stat_timeout_seconds
  initialize_if_necessary
  @config.stat_timeout * 60
end

.unpause(queue_name) ⇒ Object

Convenience method to unpause a Queue by name.



110
111
112
# File 'lib/timberline.rb', line 110

def self.unpause(queue_name)
  queue(queue_name).unpause
end

.watch(queue_name, &block) ⇒ Object

Create and start a new AnonymousWorker with the given queue_name and block. Convenience method.

Parameters:

  • queue_name (String)

    the name of the queue to watch.

  • block (Block)

    the block to execute for each queue item

See Also:



158
159
160
# File 'lib/timberline.rb', line 158

def self.watch(queue_name, &block)
  Timberline::AnonymousWorker.new(&block).watch(queue_name)
end