Class: Daemonizer::Dsl

Inherits:
Object
  • Object
show all
Defined in:
lib/daemonizer/dsl.rb

Defined Under Namespace

Classes: DslError

Constant Summary collapse

CALLBACKS =
[:before_prepare, :after_prepare, :before_start]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDsl

Returns a new instance of Dsl.



10
11
12
13
14
15
# File 'lib/daemonizer/dsl.rb', line 10

def initialize
  @source   = nil
  @options  = {}
  @pool     = :default
  @configs  = {}
end

Class Method Details

.evaluate(gemfile) ⇒ Object



4
5
6
7
8
# File 'lib/daemonizer/dsl.rb', line 4

def self.evaluate(gemfile)
  builder = new
  builder.instance_eval(File.read(gemfile.to_s), gemfile.to_s, 1)
  builder.instance_variable_get("@configs")
end

Instance Method Details

#config_copyObject



103
104
105
106
107
108
109
# File 'lib/daemonizer/dsl.rb', line 103

def config_copy
  options = @options.dup
  options[:handler_options] = @options[:handler_options].dup if @options[:handler_options]
  options[:callbacks] = @options[:callbacks].dup if @options[:callbacks]
  options[:on_poll] = @options[:on_poll].dup if @options[:on_poll]
  options
end

#handler(handler) ⇒ Object



56
57
58
# File 'lib/daemonizer/dsl.rb', line 56

def handler(handler)
  @options[:handler] = handler
end

#log_file(log) ⇒ Object



64
65
66
# File 'lib/daemonizer/dsl.rb', line 64

def log_file(log)
  @options[:log_file] = log
end

#log_level(level) ⇒ Object



42
43
44
# File 'lib/daemonizer/dsl.rb', line 42

def log_level(level)
  @options[:log_level] = level.to_sym
end

#not_cow_friendlyObject



52
53
54
# File 'lib/daemonizer/dsl.rb', line 52

def not_cow_friendly
  @options[:cow_friendly] = false
end

#on_poll(&block) ⇒ Object



46
47
48
49
# File 'lib/daemonizer/dsl.rb', line 46

def on_poll(&block)
  @options[:on_poll] ||= []
  @options[:on_poll] << block
end

#pid_file(pid) ⇒ Object



80
81
82
# File 'lib/daemonizer/dsl.rb', line 80

def pid_file(pid)
  @options[:pid_file] = pid
end

#poll_period(seconds) ⇒ Object



60
61
62
# File 'lib/daemonizer/dsl.rb', line 60

def poll_period(seconds)
  @options[:poll_period] = seconds.to_i
end

#pool(name, &blk) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/daemonizer/dsl.rb', line 91

def pool(name, &blk)
  @pool = name.to_sym
  options = config_copy
  yield
  @configs[@pool] = Daemonizer::Config.new(@pool, @options)
rescue Daemonizer::Config::ConfigError => e
  puts "* Error in pool \"#{@pool}\": #{e.to_s}. Skipping..."
ensure
  @options = options
  @pool = nil
end

#prepare(&blk) ⇒ Object



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

def prepare(&blk)
  @options[:prepare] = blk
end

#set_callback(callback, &block) ⇒ Object



29
30
31
32
33
34
# File 'lib/daemonizer/dsl.rb', line 29

def set_callback(callback, &block)
  return unless CALLBACKS.include?(callback.to_sym)
  @options[:callbacks] ||= {}
  @options[:callbacks][callback.to_sym] ||= []
  @options[:callbacks][callback.to_sym] << block
end

#set_option(option, value = nil, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/daemonizer/dsl.rb', line 17

def set_option(option, value = nil, &block)
  @options[:handler_options] ||= {}
  if not value.nil?
    @options[:handler_options][option.to_sym] = Daemonizer::Option.new(option, value)
  elsif block_given?
    @options[:handler_options][option.to_sym] = Daemonizer::Option.new(option, block, true)
  else
    raise DslError, "you should supply block or value to :set_option"
  end
end

#settings_group(&blk) ⇒ Object



84
85
86
87
88
89
# File 'lib/daemonizer/dsl.rb', line 84

def settings_group(&blk)
  options = config_copy
  yield
ensure
  @options = options
end

#start(&blk) ⇒ Object



76
77
78
# File 'lib/daemonizer/dsl.rb', line 76

def start(&blk)
  @options[:start] = blk
end

#workers(num) ⇒ Object



68
69
70
# File 'lib/daemonizer/dsl.rb', line 68

def workers(num)
  @options[:workers] = num.to_i
end