Class: RoDaemon::Daemon

Inherits:
RoThor
  • Object
show all
Includes:
Helper
Defined in:
lib/ro_daemon/daemon.rb

Instance Method Summary collapse

Methods included from Helper

#logs_dir

Instance Method Details

#daemonize(cmd, *args, &blk) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ro_daemon/daemon.rb', line 14

def daemonize(cmd, *args, &blk)
  logs = Dir["#{logs_dir}/*.log"]
  ms_cnn_db

  d = nil

  begin
    d = Data::Daemon.new(cmd: cmd)
  rescue ActiveRecord::StatementInvalid => e
    Data::Mg.st
    d = Data::Daemon.new(cmd: cmd)
  end

  stdout, stderr = d.stdout, d.stderr
  pid = spawn_daemon(cmd, stdout, stderr)

  fix_db_err do
    d.save
  end

  pid
end

#db(*args, &blk) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ro_daemon/daemon.rb', line 89

def db(*args, &blk)
  _db = logs_dir('manager.sqlite')

  if File.exist?(_db)
    return _db
  end

  begin
    new_db
  rescue Errno::ENOENT => e
    if e.to_s.match(%r{No such file or directory - sqlite3})
      install_sqlite
      new_db
    else
      raise e
    end
  rescue ShErr => e
    msg = e.to_s
    if msg.match(%r{command not found})
      install_sqlite
      new_db
    else
      raise e
    end
  end
  _db
end

#fix_db_err(*args, &blk) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ro_daemon/daemon.rb', line 52

def fix_db_err(*args, &blk)
  begin
    yield
  rescue ActiveRecord::StatementInvalid => e
    if e.to_s.match(%r{Could not find table})
      Data::Mg.st
      yield
    else
      raise e
    end
  end
end

#get_stdout_stderr(logs) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ro_daemon/daemon.rb', line 65

def get_stdout_stderr(logs)
  logs.select do |log|
    Time.now - File.mtime(log) > 60.days and Data::Daemon.first(stdout: log).closed?
  end.each do |f|
    FileUtils.rm_f(f)
  end

  if Data::Daemon.count > 0
    (1.. Data::Daemon.all.last.id)
  end

  unless test(?d, logs_dir)
    FileUtils.mkdir(logs_dir)
  end

  stdout = File.open logs_dir("#{stdout_fn}.log"), 'w+'
  stderr = File.open logs_dir("#{stdout_fn}_err.log"), "w+"
  return stderr, stdout
end

#install_sqliteObject



117
118
119
# File 'lib/ro_daemon/daemon.rb', line 117

def install_sqlite
  sh "sudo apt-get install sqlite3 -y"
end

#ls(*args, &blk) ⇒ Object



39
40
41
42
# File 'lib/ro_daemon/daemon.rb', line 39

def ls(*args, &blk)
  ms_cnn_db
  puts Data::Daemon.all.map(&:inspect)
end

#ms_cnn_db(*args, &blk) ⇒ Object



85
86
87
# File 'lib/ro_daemon/daemon.rb', line 85

def ms_cnn_db(*args, &blk)
  @cnn ||= ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: db)
end

#new_dbObject



121
122
123
# File 'lib/ro_daemon/daemon.rb', line 121

def new_db
  sh "sqlite3 dbname.sqlite .dump"
end

#spawn_daemon(cmd, stdout, stderr) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/ro_daemon/daemon.rb', line 44

def spawn_daemon(cmd, stdout, stderr)
  pid = spawn(cmd, out: stdout, err: stderr)
  puts
  puts "spawn pid:#{pid} cmd:#{cmd}\nstdout: #{stdout}\nstderr: #{stderr}"
  puts
  pid
end