Module: Yup

Defined in:
lib/yup.rb,
lib/yup/state.rb,
lib/yup/version.rb,
lib/yup/state/bdb.rb,
lib/yup/state/redis.rb,
lib/yup/request_handler.rb,
lib/yup/request_forwarder.rb

Defined Under Namespace

Modules: State Classes: RequestForwarder, RequestHandler

Constant Summary collapse

VERSION =
File.read("#{File.dirname(__FILE__)}/../../VERSION")
@@resend_delay =
60.0
@@watermark =
100
@@logger =
Logger.new(STDOUT)
@@retry_unless_2xx =
false

Class Method Summary collapse

Class Method Details

.loggerObject



23
# File 'lib/yup.rb', line 23

def self.logger; @@logger end

.logger=(logger) ⇒ Object



24
# File 'lib/yup.rb', line 24

def self.logger=(logger); @@logger = logger end

.resend_delayObject



15
# File 'lib/yup.rb', line 15

def self.resend_delay; @@resend_delay end

.resend_delay=(seconds) ⇒ Object



16
# File 'lib/yup.rb', line 16

def self.resend_delay=(seconds); @@resend_delay = seconds end

.retry_unless_2xxObject



27
# File 'lib/yup.rb', line 27

def self.retry_unless_2xx; @@retry_unless_2xx end

.retry_unless_2xx=(bool) ⇒ Object



28
# File 'lib/yup.rb', line 28

def self.retry_unless_2xx=(bool); @@retry_unless_2xx = bool end

.run(config) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yup.rb', line 30

def self.run(config)
  host        = config[:listen_host] || 'localhost'
  port        = config[:listen_port] || 8080
  status_code = config[:status_code] || 200
  forward_to  = config[:forward_to]
  timeout     = config[:timeout] || 60

  EM.run do
    EM.start_server(host, port, RequestHandler, forward_to, status_code, nil, timeout)
    logger.info { "listening on #{host}:#{port}" }
  end
end

.run_with_bdb(config) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/yup.rb', line 54

def self.run_with_bdb(config)
  require 'yup/state/bdb'

  host        = config[:listen_host] || 'localhost'
  port        = config[:listen_port] || 8080
  status_code = config[:status_code] || 200
  forward_to  = config[:forward_to]
  dbpath      = config[:persistent]
  timeout     = config[:timeout] || 60
  feedback_channel = File.join(Dir.tmpdir, "yupd-#{$$}-feedback")
  state            = State::BDB.new(dbpath, forward_to, feedback_channel)

  pid = Process.fork do
    State::BDB::RequestForwarder.new(state, forward_to, timeout).run_loop
  end

  if pid
    db_closer = proc do
      Yup.logger.info { "Terminating consumer #{$$}" }
      Process.kill("KILL", pid)
      state.dispose()
      exit 0
    end
    Signal.trap("TERM", &db_closer)
    Signal.trap("INT", &db_closer)
  end

  EM.run do
    EM.start_unix_domain_server(feedback_channel, State::BDB::FeedbackHandler, state)
    logger.info { "Feedback through #{feedback_channel}" }

    EM.start_server(host, port, RequestHandler, forward_to, status_code, state, timeout)
    logger.info { "Listening on #{host}:#{port}" }
  end
end

.run_with_redis(config) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/yup.rb', line 90

def self.run_with_redis(config)
  require 'yup/state/redis'

  host        = config[:listen_host] || 'localhost'
  port        = config[:listen_port] || 8080
  status_code = config[:status_code] || 200
  forward_to  = config[:forward_to]
  dbpath      = config[:persistent]
  timeout     = config[:timeout] || 60
  state       = State::Redis.new(dbpath, forward_to)

  pid = Process.fork do
    State::Redis::RequestForwarder.new(state, forward_to, timeout).run_loop
  end

  if pid
    db_closer = proc do
      Yup.logger.info { "Terminating consumer #{$$}" }
      Process.kill("KILL", pid)
      state.dispose()
      exit 0
    end
    Signal.trap("TERM", &db_closer)
    Signal.trap("INT", &db_closer)
  end

  EM.run do
    EM.start_server(host, port, RequestHandler, forward_to, status_code, state, timeout)
    logger.info { "Listening on #{host}:#{port}" }
  end
end

.run_with_state(config) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/yup.rb', line 43

def self.run_with_state(config)
  case State.queue_type(config[:persistent])
  when :bdb
    self.run_with_bdb(config)
  when :redis
    self.run_with_redis(config)
  else
    abort "Unknown scheme of persistent queue."
  end
end

.watermarkObject



19
# File 'lib/yup.rb', line 19

def self.watermark; @@watermark end

.watermark=(seconds) ⇒ Object



20
# File 'lib/yup.rb', line 20

def self.watermark=(seconds); @@watermark = seconds end