Module: Opto::Task::Runner

Defined in:
lib/opto/task/runner.rb

Instance Method Summary collapse

Instance Method Details

#add_error(field, classification, message) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/opto/task/runner.rb', line 32

def add_error(field, classification, message)
  runner_errors[field] ||= {}
  runner_errors[field][classification] =
    case runner_errors[field][classification]
    when String
      runner_errors[field][classification] == message ? runner_errors[field][classification] : [runner_errors[field][classification], message]
    when Array
      runner_errors[field][classification].include?(message) ? runner_errors[field][classification] : (runner_errors[field][classification] + [message])
    else
      message
    end
end

#errorsObject



7
8
9
10
# File 'lib/opto/task/runner.rb', line 7

def errors
  wait_for_lock
  super.merge(runner_errors)
end

#mutexObject



23
24
25
# File 'lib/opto/task/runner.rb', line 23

def mutex
  @mutex ||= Mutex.new
end

#runObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/opto/task/runner.rb', line 45

def run
  mutex.synchronize do
    current_run = Opto::Task::Result.new(self)

    if valid?

      runner_errors.clear
      outcome = perform
      current_run.errors.merge!(runner_errors)
      if current_run.success?
        current_run.outcome = outcome
        after if self.respond_to?(:after)
      end
      runner_errors.clear
    else
      current_run.errors.merge!(errors)
    end
    return current_run
  end
end

#runner_errorsObject



27
28
29
30
# File 'lib/opto/task/runner.rb', line 27

def runner_errors
  wait_for_lock
  @runner_errors ||= {}
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
# File 'lib/opto/task/runner.rb', line 12

def valid?
  wait_for_lock
  runner_errors.clear
  validate if self.respond_to?(:validate)
  errors.empty? && super
end

#wait_for_lockObject



19
20
21
# File 'lib/opto/task/runner.rb', line 19

def wait_for_lock
  sleep 0.001 until !mutex.locked? || mutex.owned?
end