Module: Daemonizer

Includes:
Errors
Defined in:
lib/daemonizer.rb,
lib/daemonizer/cli.rb,
lib/daemonizer/config.rb,
lib/daemonizer/engine.rb,
lib/daemonizer/option.rb,
lib/daemonizer/worker.rb,
lib/daemonizer/handler.rb,
lib/daemonizer/autoload.rb,
lib/daemonizer/daemonize.rb,
lib/daemonizer/worker_pool.rb,
lib/daemonizer/process_manager.rb

Defined Under Namespace

Modules: Daemonize, Errors, Stats Classes: CLI, Config, Dsl, Engine, FakeHandler, Handler, Option, ProcessManager, Worker, WorkerPool

Constant Summary

Constants included from Errors

Errors::Error, Errors::InvalidCommandError, Errors::InvalidFrameworkError

Class Method Summary collapse

Class Method Details

.[](pool) ⇒ Object



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

def self.[](pool)
  find_pools(pool).first or nil
end

.__p(*path) ⇒ Object



3
# File 'lib/daemonizer/autoload.rb', line 3

def self.__p(*path) File.join(File.dirname(File.expand_path(__FILE__)), *path) end

.daemonfileObject



38
39
40
41
42
43
44
# File 'lib/daemonizer.rb', line 38

def self.daemonfile
  if defined?(@@daemonfile)
    @@daemonfile
  else
    "Demfile"
  end
end

.daemonfile=(daemonfile_name) ⇒ Object



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

def self.daemonfile=(daemonfile_name)
  @@daemonfile = find_daemonfile(daemonfile_name)
end

.find_daemonfile(daemonfile_name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/daemonizer.rb', line 23

def self.find_daemonfile(daemonfile_name)
  previous = nil
  current  = File.expand_path(Dir.pwd)

  until !File.directory?(current) || current == previous
    filename = File.join(current, daemonfile_name)
    return filename if File.file?(filename)
    current, previous = File.expand_path("..", current), current
  end
end

.find_pools(pool_name = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/daemonizer.rb', line 102

def self.find_pools(pool_name = nil)
  pools = Dsl.evaluate(daemonfile)

  if pool_name
    if pool = pools[pool_name.to_sym]
      [pool]
    else
      puts "#{pool_name} is not configured"
      []
    end
  else
    pools.values
  end
end

.flush_loggerObject



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

def self.flush_logger
  @@logger_file.flush
end

.init_console_logger(name) ⇒ Object



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

def self.init_console_logger(name)
  @@logger_file = STDOUT
  @@logger = Logger.new(@@logger_file)
  set_logger_common_options
end

.init_logger(name, log_file) ⇒ Object



62
63
64
65
66
67
# File 'lib/daemonizer.rb', line 62

def self.init_logger(name, log_file)
  @@logger_file = File.open(log_file, File::WRONLY | File::APPEND)
  @@logger_file.sync = true
  @@logger = Logger.new(@@logger_file)
  set_logger_common_options
end

.log_levelObject



58
59
60
# File 'lib/daemonizer.rb', line 58

def self.log_level
  @@log_level ||= :info
end

.log_level=(level) ⇒ Object



54
55
56
# File 'lib/daemonizer.rb', line 54

def self.log_level=(level)
  @@log_level = level
end

.loggerObject



90
91
92
93
94
95
96
# File 'lib/daemonizer.rb', line 90

def self.logger
  if defined?(@@logger)
    @@logger
  else
    nil
  end
end

.logger_contextObject



50
51
52
# File 'lib/daemonizer.rb', line 50

def self.logger_context
  @@logger_context
end

.logger_context=(str) ⇒ Object



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

def self.logger_context=(str)
  @@logger_context = str
end

.reopen_log_fileObject



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

def self.reopen_log_file
  true #do not need it in append-only mode
end

.rootObject



15
16
17
18
19
20
21
# File 'lib/daemonizer.rb', line 15

def self.root
  if defined?(@@root)
    @@root
  else
    File.dirname(daemonfile)
  end
end

.root=(value) ⇒ Object



11
12
13
# File 'lib/daemonizer.rb', line 11

def self.root=(value)
  @@root = value
end

.set_logger_common_optionsObject



69
70
71
72
73
74
# File 'lib/daemonizer.rb', line 69

def self.set_logger_common_options
  @@logger.sev_threshold = Logger::const_get(Daemonizer.log_level.to_s.upcase) || Logger::INFO
  @@logger.formatter = Proc.new do |severity, datetime, progname, msg|
    "%s %s -- %s -- %s\n" % [ datetime.strftime("%Y-%m-%d %H:%M:%S"), severity, Daemonizer.logger_context, msg ]
  end
end