Class: Traker::Instrumenter

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

Overview

Wraps array of rake tasks, and auguments each one of them with Traker features

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tasks) ⇒ Instrumenter

Returns a new instance of Instrumenter.



9
10
11
12
# File 'lib/traker/instrumenter.rb', line 9

def initialize(tasks)
  @tasks = tasks
  @config = Traker::Config.load
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/traker/instrumenter.rb', line 7

def config
  @config
end

#tasksObject

Returns the value of attribute tasks.



7
8
9
# File 'lib/traker/instrumenter.rb', line 7

def tasks
  @tasks
end

Instance Method Details

#instrumentObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/traker/instrumenter.rb', line 14

def instrument
  tasks.each do |t|
    task_name = t.name

    next unless tasks_to_be_run.map { |task| task['name'] }.include?(task_name)

    handler = proc do |&block|
      record = Traker::Task.find_or_initialize_by(name: task_name, environment: config.env)

      record.started_at = DateTime.now
      record.is_success = true

      begin
        block.call
        record.run_count += 1
      rescue StandardError => e
        record.is_success = false
        record.error = e.backtrace.first
        raise e
      ensure
        record.finished_at = DateTime.now
        record.save!
      end
    end

    yield task_name, handler
  end
end