Class: Workaholic

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proccess, limit) ⇒ Workaholic

Returns a new instance of Workaholic.



16
17
18
19
# File 'lib/workaholic.rb', line 16

def initialize proccess, limit
  @proccess = proccess
  @limit = limit.to_i
end

Instance Attribute Details

#limitObject

Find long running cron jobs.

Example:

>> require 'workaholic'
>> workers = Workaholic.new 'cron', 300
>> workers.scan
=> {:"4389"=>71983, :"4335"=>71994}

Arguments:

proccess: (String)
limit: (Int)


14
15
16
# File 'lib/workaholic.rb', line 14

def limit
  @limit
end

#proccessObject

Find long running cron jobs.

Example:

>> require 'workaholic'
>> workers = Workaholic.new 'cron', 300
>> workers.scan
=> {:"4389"=>71983, :"4335"=>71994}

Arguments:

proccess: (String)
limit: (Int)


14
15
16
# File 'lib/workaholic.rb', line 14

def proccess
  @proccess
end

Instance Method Details

#etime_to_i(etime) ⇒ Object



32
33
34
35
# File 'lib/workaholic.rb', line 32

def etime_to_i etime
  h,m,s = etime.split(':')
  (h.to_i * 60 * 60) + (m.to_i * 60) + (s.to_i) 
end

#scanObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/workaholic.rb', line 21

def scan
  # Get all child processes of the desired parent process.
  pids = %x(ps -o pid,etime --ppid=`pidof -s #{@proccess}` --sort=etime | awk '{print $1,$2}' | tr '-' ' ' | awk '{print $1,$3}' | egrep '^[0-9]' | grep ':')
  workaholics = {}
  pids.each_line do |proccess|
    pid, etime = proccess.split
    workaholics[pid.to_sym] = etime_to_i etime if (etime_to_i etime) > @limit
  end
  workaholics
end