Module: Magent::Stats

Included in:
GenericChannel
Defined in:
lib/magent/stats.rb

Instance Method Summary collapse

Instance Method Details

#on_job_failed(updated_by) ⇒ Object



38
39
40
# File 'lib/magent/stats.rb', line 38

def on_job_failed(updated_by)
  stats_collection.update({:_id => @name}, {:$inc => {:total_errors => 1, :"workers.#{updated_by}.total_errors" => 1} })
end

#on_job_processed(last_job, duration, updated_by) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/magent/stats.rb', line 25

def on_job_processed(last_job, duration, updated_by)
  last_job["duration"] = ("%f" % duration)
  updated_at = Time.now

  updates = {}
  updates[:$inc] = {:total_jobs => 1, :"workers.#{updated_by}.total_jobs" => 1}
  updates[:$set] = {:updated_at => updated_at, :updated_by => updated_by,
                    :last_processed_job => last_job,
                    :"workers.#{updated_by}.last_update_at" => updated_at }

  stats_collection.update({:_id => @name}, updates, {:multi => true})
end

#on_quit(updated_by) ⇒ Object



48
49
50
# File 'lib/magent/stats.rb', line 48

def on_quit(updated_by)
  stats_collection.update({:_id => @name}, {:set => {:"workers.#{updated_by}.quitted_at" => Time.now.utc} })
end

#on_start(updated_by) ⇒ Object



42
43
44
45
46
# File 'lib/magent/stats.rb', line 42

def on_start(updated_by)
  puts ">>> Current Stats: #{stats.inspect}"
  stats_collection.update({:_id => @name}, {:$set => {:"workers.#{updated_by}.last_update_at" => Time.now.utc},
                                            :$inc => {:"workers.#{updated_by}.total_restarts" => 1} }, {:safe => true, :multi => true})
end

#statsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/magent/stats.rb', line 7

def stats
  @stats ||= if doc = stats_collection.find({:_id => @name}, {}).next_document
    doc
  else
    stats_collection.save({:_id => @name,
                           :created_at => Time.now.utc,
                           :total_errors => 0,
                           :total_jobs => 0,
                           :last_processed_job => {"duration" => 0, "method" => "", "priority" => 0},
                           :updated_at => Time.now.utc,
                           :updated_by => "",
                           :workers => {}
                          }, {:safe => true})

    stats_collection.find({:_id => @name}, {}).next_document
  end
end

#stats_collectionObject



3
4
5
# File 'lib/magent/stats.rb', line 3

def stats_collection
  @stats_collection ||= Magent.database.collection("magent.stats")
end