Module: Creeper

Extended by:
Legacy::ClassMethods
Defined in:
lib/creeper/cli.rb,
lib/creeper.rb,
lib/creeper/web.rb,
lib/creeper/util.rb,
lib/creeper/fetch.rb,
lib/creeper/rails.rb,
lib/creeper/client.rb,
lib/creeper/legacy.rb,
lib/creeper/worker.rb,
lib/creeper/logging.rb,
lib/creeper/manager.rb,
lib/creeper/testing.rb,
lib/creeper/version.rb,
lib/creeper/paginator.rb,
lib/creeper/processor.rb,
lib/creeper/middleware/chain.rb,
lib/creeper/redis_connection.rb,
lib/creeper/exception_handler.rb,
lib/creeper/beanstalk_connection.rb,
lib/creeper/extensions/action_mailer.rb,
lib/creeper/extensions/active_record.rb,
lib/creeper/extensions/generic_proxy.rb,
lib/creeper/middleware/server/logging.rb,
lib/creeper/middleware/server/timeout.rb,
lib/creeper/middleware/server/retry_jobs.rb,
lib/creeper/middleware/server/active_record.rb

Overview

require ‘creeper/scheduled’

Defined Under Namespace

Modules: ExceptionHandler, Extensions, Legacy, Logging, Middleware, Paginator, Util, Worker Classes: BeanstalkConnection, CLI, Client, Fetcher, Manager, Processor, Rails, RedisConnection, SprocketsMiddleware, Web

Constant Summary collapse

DEFAULTS =
{
  :queues => [],
  :concurrency => 25,
  :require => '.',
  :environment => nil,
  :timeout => 8,
  :enable_rails_extensions => true,
}
VERSION =
"2.0.2"

Class Method Summary collapse

Methods included from Legacy::ClassMethods

enqueue, enqueue!, job_descriptions

Class Method Details

.beanstalk(&block) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/creeper.rb', line 65

def self.beanstalk(&block)
  if block_given?
    yield beanstalk
  else
    @beanstalk ||= Creeper::BeanstalkConnection.create
  end
end

.beanstalk=(hash) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/creeper.rb', line 73

def self.beanstalk=(hash)
  if @beanstalk
    @beanstalk.close rescue nil
  end
  if hash.is_a?(Hash)
    @beanstalk = BeanstalkConnection.create(hash)
  elsif hash.is_a?(Beanstalk::Pool)
    @beanstalk = hash
  else
    raise ArgumentError, "beanstalk= requires a Hash or Beanstalk::Pool"
  end
end

.client_middleware {|@client_chain| ... } ⇒ Object

Yields:

  • (@client_chain)


113
114
115
116
117
# File 'lib/creeper.rb', line 113

def self.client_middleware
  @client_chain ||= Client.default_middleware
  yield @client_chain if block_given?
  @client_chain
end

.configure_client {|_self| ... } ⇒ Object

Configuration for Creeper client, use like:

Creeper.configure_client do |config|
  config.redis = { :namespace => 'myapp', :size => 1, :url => 'redis://myhost:8877/mydb' }
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Creeper)

    the object that the method was called on



57
58
59
# File 'lib/creeper.rb', line 57

def self.configure_client
  yield self unless server?
end

.configure_server {|_self| ... } ⇒ Object

Configuration for Creeper server, use like:

Creeper.configure_server do |config|
  config.redis = { :namespace => 'myapp', :size => 25, :url => 'redis://myhost:8877/mydb' }
  config.server_middleware do |chain|
    chain.add MyServerHook
  end
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Creeper)

    the object that the method was called on



47
48
49
# File 'lib/creeper.rb', line 47

def self.configure_server
  yield self if server?
end

.dump_json(object) ⇒ Object



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

def self.dump_json(object)
  MultiJson.encode(object)
end

.hook_rails!Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/creeper/rails.rb', line 2

def self.hook_rails!
  return unless Creeper.options[:enable_rails_extensions]
  if defined?(ActiveRecord)
    ActiveRecord::Base.extend(Creeper::Extensions::ActiveRecord)
    ActiveRecord::Base.send(:include, Creeper::Extensions::ActiveRecord)
  end

  if defined?(ActionMailer)
    ActionMailer::Base.extend(Creeper::Extensions::ActionMailer)
  end
end

.load_json(string) ⇒ Object



129
130
131
# File 'lib/creeper.rb', line 129

def self.load_json(string)
  MultiJson.decode(string)
end

.loggerObject



137
138
139
# File 'lib/creeper.rb', line 137

def self.logger
  Creeper::Logging.logger
end

.logger=(log) ⇒ Object



141
142
143
# File 'lib/creeper.rb', line 141

def self.logger=(log)
  Creeper::Logging.logger = log
end

.optionsObject



30
31
32
# File 'lib/creeper.rb', line 30

def self.options
  @options ||= DEFAULTS.dup
end

.options=(opts) ⇒ Object



34
35
36
# File 'lib/creeper.rb', line 34

def self.options=(opts)
  @options = opts
end

.poll_interval=(interval) ⇒ Object



145
146
147
# File 'lib/creeper.rb', line 145

def self.poll_interval=(interval)
  self.options[:poll_interval] = interval
end

.redis(&block) ⇒ Object

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/creeper.rb', line 86

def self.redis(&block)
  @redis ||= Creeper::RedisConnection.create
  raise ArgumentError, "requires a block" if !block
  tries = 0
  begin
    @redis.with(&block)
  rescue Redis::TimeoutError, Redis::ConnectionError
    tries += 1
    if tries < 30
      sleep tries
      retry
    else
      raise
    end
  end
end

.redis=(hash) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/creeper.rb', line 103

def self.redis=(hash)
  if hash.is_a?(Hash)
    @redis = RedisConnection.create(hash)
  elsif hash.is_a?(ConnectionPool)
    @redis = hash
  else
    raise ArgumentError, "redis= requires a Hash or ConnectionPool"
  end
end

.server?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/creeper.rb', line 61

def self.server?
  defined?(Creeper::CLI)
end

.server_middleware {|@server_chain| ... } ⇒ Object

Yields:

  • (@server_chain)


119
120
121
122
123
# File 'lib/creeper.rb', line 119

def self.server_middleware
  @server_chain ||= Processor.default_middleware
  yield @server_chain if block_given?
  @server_chain
end