Class: Cronbox

Inherits:
Object
  • Object
show all
Defined in:
lib/cronbox.rb,
lib/cronbox/version.rb,
lib/cronbox/data_file.rb,
lib/cronbox/cli_wrapper.rb

Defined Under Namespace

Classes: CliWrapper, DataFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Cronbox

Returns a new instance of Cronbox.



8
9
10
11
# File 'lib/cronbox.rb', line 8

def initialize(file=nil)
  file ||= File.join(Dir.home, '.cronbox')
  @data = Cronbox::DataFile.new(file)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/cronbox.rb', line 13

def data
  @data
end

Class Method Details

.time_delta_string(time) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cronbox.rb', line 80

def self.time_delta_string(time)
  d = Time.now.to_i - time.to_i
  case d
    when 0 then 'just now'
    when 1 then 'a second ago'
    when 2..59 then d.to_s+' seconds ago'
    when 60..119 then 'a minute ago' #120 = 2 minutes
    when 120..3540 then (d/60).to_i.to_s+' minutes ago'
    when 3541..7100 then 'an hour ago' # 3600 = 1 hour
    when 7101..82800 then ((d+99)/3600).to_i.to_s+' hours ago'
    when 82801..172000 then 'a day ago' # 86400 = 1 day
    when 172001..518400 then ((d+800)/(60*60*24)).to_i.to_s+' days ago'
    when 518400..1036800 then 'a week ago'
    else ((d+180000)/(60*60*24*7)).to_i.to_s+' weeks ago'
  end
end

Instance Method Details

#delete(id_or_label) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cronbox.rb', line 65

def delete(id_or_label)
  if id_or_label.to_i > 0
    e = @data.find_entry(id: id_or_label.to_i)
  else
    e = @data.find_entry(label: id_or_label)
  end
  if e
    @data.del_entry(e)
    @data.save!
    e
  else
    nil
  end
end

#execute(label, cmd, *args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cronbox.rb', line 35

def execute(label, cmd, *args)
  e = Hash.new
  e['command'] = ([cmd] + args).join(' ')
  e['status'] = nil
  e['output'] = []
  e['label'] = label.to_s unless label.to_s.empty?
  begin
    e['ts'] = Time.now.to_i
    Open3.popen3(cmd, *args) do |stdin, stdout, stderr, thread|
      e['status'] = thread.value.to_i
      e['output'] = stderr.readlines + stdout.readlines
    end
  rescue => ex
    e['status'] = 1
    e['output'] = [ex.message]
  end

  existing = @data.find_entry(label: e['label']) if e['label']
  existing ||= @data.find_entry(command: e['command'])

  if existing
    e['label'] ||= existing['label']
    existing.merge!(e)
  else
    @data.add_entry(e)
  end

  @data.save!
end

#report(errors_only = false) ⇒ Object



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

def report(errors_only=false)
  entries = @data.entries.sort_by { |e| e['ts'] }
  report = {
      count: 0,
      errors: 0,
      entries: [],
  }
  entries.reverse.each do |e|
    e = e.clone
    e['is_error'] = e['status'].to_i > 0
    e['when'] = self.class.time_delta_string(e['ts'])
    e['has_output'] = (e['output'].respond_to?(:size) && e['output'].size > 0)
    next if errors_only and not e['is_error']
    report[:count] += 1
    report[:errors] += 1 if e['is_error']
    report[:entries].push e
  end
  report
end