Class: Kogno::SequencesCtl

Inherits:
Object
  • Object
show all
Defined in:
lib/core/bin_helpers/sequences_ctl.rb

Class Method Summary collapse

Class Method Details

.alive?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 30

def alive?
  pid = get_pid
  return false if pid == 0
  Process.getpgid(pid) rescue false
end

.delete_pidObject



17
18
19
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 17

def delete_pid
  save_pid("")
end

.fgObject



51
52
53
54
55
56
57
58
59
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 51

def fg
  log_environment_info()
  if get_pid > 0 && alive?
    logger.write "Error: It has already started..", :red
  else        
    require File.join(Kogno::Application.project_path,'application.rb')
    Sequence.process_all
  end
end

.get_pidObject



21
22
23
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 21

def get_pid
  File.read(File.join(pid_file)).to_i
end

.kill_process(pid) ⇒ Object



25
26
27
28
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 25

def kill_process(pid)
  return false if pid.to_i == 0
  Process.kill("KILL",pid) rescue nil
end

.log_environment_infoObject



9
10
11
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 9

def log_environment_info
  logger.write "Kogno #{Gem.loaded_specs["kogno"].version.version} sequences server starting in #{Kogno::Application.config.environment}", :bright
end

.options(option) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 76

def options(option)
  case option.to_s.to_sym
    when :start
      start
    when :stop
      stop
    when :fg
      fg
    when :status
      status
    when :restart
      stop
      start
    else
      puts "usage: sequences stop|start|restart|status|fg"
  end
end

.pid_fileObject



5
6
7
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 5

def pid_file
  Application.file_path(File.join(Application.project_path,"tmp","sequence.pid"))
end

.save_pid(pid) ⇒ Object



13
14
15
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 13

def save_pid(pid)
  File.write(pid_file, pid)
end

.startObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 36

def start
  if get_pid > 0 && alive?
    logger.write "Error: It has already started..", :red
  else 
    log_environment_info()     
    logger.write "START", :green
    pid = fork do
      Logger.set(:sequences)
      require File.join(Kogno::Application.project_path,'application.rb')
      Sequence.process_all
    end
    save_pid(pid)
  end
end

.statusObject



68
69
70
71
72
73
74
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 68

def status
  if alive?
    logger.write "Running | Pid: #{get_pid}..", :green
  else
    logger.write "Stopped", :red
  end
end

.stopObject



61
62
63
64
65
66
# File 'lib/core/bin_helpers/sequences_ctl.rb', line 61

def stop
  pid = get_pid
  kill_process(pid)
  delete_pid
  logger.write "STOP", :red
end