Class: Cinchize::Cinchize

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, network, plugins, plugin_options) ⇒ Cinchize

Returns a new instance of Cinchize.



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

def initialize options, network, plugins, plugin_options
  @network = network
  @plugins = plugins      
  @plugin_options = plugin_options
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



67
68
69
# File 'lib/cinchize.rb', line 67

def options
  @options
end

Instance Method Details

#_sym_hash(hsh) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/cinchize.rb', line 155

def _sym_hash hsh
  hsh.keys.inject({}) do |memo, key| 
    if hsh[key].is_a? Hash
      memo[key.to_sym] = _sym_hash(hsh[key])
    else 
      memo[key.to_sym] = hsh[key]
    end
    memo 
  end
end

#app_nameObject



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

def app_name
  options[:app_name]
end

#clean_app_nameObject



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

def clean_app_name
  app_name.split('_', 2).last
end

#dirObject



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

def dir
  options[:dir]
end

#restartObject



88
89
90
91
# File 'lib/cinchize.rb', line 88

def restart
  stop
  start
end

#running?Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
153
# File 'lib/cinchize.rb', line 147

def running? 
  pidfile = Daemons::PidFile.new dir, app_name
  return false if pidfile.pid.nil?
  return Process.kill(0, pidfile.pid) != 0
rescue Errno::ESRCH => e
  return false
end

#startObject



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
121
122
123
124
# File 'lib/cinchize.rb', line 93

def start 
  if running?
    raise ArgumentError.new "#{clean_app_name} is already running"      
  end

  puts "* starting #{clean_app_name}"

  daemon = Daemons::ApplicationGroup.new(app_name, {
    :ontop => options[:ontop],
    :dir => dir,
    :dir_mode => options[:dir_mode]
  })
  app = daemon.new_application :mode => :none, :log_output => options[:log_output]
  app.start

  network = _sym_hash(@network)
  plugins = @plugins
  plugin_options = @plugin_options

  loop do
    bot = Cinch::Bot.new do  
      configure do |c|
        c.load network

        c.plugins.plugins = plugins
        c.plugins.options = plugin_options
      end
    end

    bot.start
  end
end

#statusObject



139
140
141
142
143
144
145
# File 'lib/cinchize.rb', line 139

def status 
  if running?
    puts "* #{clean_app_name} is running"
  else
    puts "* #{clean_app_name} is not running"
  end
end

#stopObject



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cinchize.rb', line 126

def stop 
  unless running?
    puts "* #{clean_app_name} is not running"
    return
  end

  pidfile = Daemons::PidFile.new dir, app_name
  puts "* stopping #{clean_app_name}"

  Process.kill("QUIT", pidfile.pid)
  File.delete(pidfile.filename)
end