Module: CPEE::Instantiation

Defined in:
lib/cpee-instantiation/utils.rb,
lib/cpee-instantiation/instantiation.rb

Defined Under Namespace

Modules: Helpers Classes: ContinueTask, InstantiateGit, InstantiateUrl, InstantiateXML

Constant Summary collapse

SERVER =
File.expand_path(File.join(__dir__,'instantiation.xml'))

Class Method Summary collapse

Class Method Details

.cleanup_services(watchdog_start_off) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cpee-instantiation/utils.rb', line 39

def self::cleanup_services(watchdog_start_off)
  return if watchdog_start_off
  Dir[File.join(__dir__,'routing','*.rb')].each do |s|
    s = s.sub(/\.rb$/,'')
    pid = (File.read(s + '.pid').to_i rescue nil)
    if !pid.nil? || (Process.kill(0, pid) rescue false)
      system "#{s}.rb stop 1>/dev/null 2>&1"
      puts "➡ Service #{File.basename(s,'.rb')} stopped ..."
    end
  end
end

.implementation(opts) ⇒ Object

}}}



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/cpee-instantiation/instantiation.rb', line 288

def self::implementation(opts)
  opts[:cpee]       ||= 'http://localhost:9298/'
  opts[:self]       ||= "http#{opts[:secure] ? 's' : ''}://#{opts[:host]}:#{opts[:port]}/"

  opts[:watchdog_frequency]         ||= 7
  opts[:watchdog_start_off]         ||= false

  ### set redis_cmd to nil if you want to do global
  ### at least redis_path or redis_url and redis_db have to be set if you do global
  opts[:redis_db]                   ||= 0
  opts[:redis_url]                  ||= 'unix://redis.sock' # sadly we have to do this for now
  opts[:redis_unixsocket]           ||= true
  opts[:redis_cmd]                  ||= 'redis-server --port #redis_port# --unixsocket #redis_path# --unixsocketperm 600 --pidfile #redis_pid# --dir #redis_db_dir# --dbfilename                  #redis_db_name# --databases 1 --save 900 1 --save 300 10 --save 60 10000 --rdbcompression yes --daemonize yes --protected-mode no'
  opts[:redis_pid]                  ||= 'redis.pid' # use e.g. /var/run/redis.pid if you do global. Look it up in your redis config
  opts[:redis_db_name]              ||= 'redis.rdb' # use e.g. /var/lib/redis.rdb for global stuff. Look it up in your redis config

  CPEE::redis_connect opts, 'Instantiation'

  Proc.new do
    parallel do
      CPEE::Instantiation::watch_services(opts[:watchdog_start_off],opts[:redis_url],File.join(opts[:basepath],opts[:redis_path]),opts[:redis_db])
      EM.add_periodic_timer(opts[:watchdog_frequency]) do ### start services
        CPEE::Instantiation::watch_services(opts[:watchdog_start_off],opts[:redis_url],File.join(opts[:basepath],opts[:redis_path]),opts[:redis_db])
      end
    end

    on resource do
      run InstantiateXML, opts[:cpee], true if post 'xmlsimple'
      on resource 'xml' do
        run InstantiateXML, opts[:cpee], false if post 'xml'
      end
      on resource 'url' do
        run InstantiateUrl, opts[:cpee], opts[:self], opts[:redis], false if post 'url'
        run InstantiateUrl, opts[:cpee], opts[:self], opts[:redis], true  if post 'url_info'
      end
      on resource 'git' do
        run InstantiateGit, opts[:cpee], opts[:self], opts[:redis] if post 'git'
      end
      on resource 'callback' do
        on resource do
          run ContinueTask, opts[:cpee], opts[:redis] if post
        end
      end
    end
  end
end

.watch_services(watchdog_start_off, url, path, db) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cpee-instantiation/utils.rb', line 20

def self::watch_services(watchdog_start_off,url,path,db)
  return if watchdog_start_off
  EM.defer do
    Dir[File.join(__dir__,'routing','*.rb')].each do |s|
      s = s.sub(/\.rb$/,'')
      pid = (File.read(s + '.pid').to_i rescue nil)
      cmd = if url.nil?
        "-p \"#{path}\" -d #{db} restart"
      else
        "-u \"#{url}\" -d #{db} restart"
      end
      if (pid.nil? || !(Process.kill(0, pid) rescue false)) && !File.exist?(s + '.lock')
        system "#{s}.rb " + cmd + " 1>/dev/null 2>&1"
        puts "➡ Service #{File.basename(s)} (-v #{cmd}) started ..."
      end
    end
  end
end