Class: CavalerieWeb::SequenceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/cavalerie_web/managers/sequence_manager.rb

Class Method Summary collapse

Class Method Details

.set_threads_timeout(threads_array, timeout = 15) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cavalerie_web/managers/sequence_manager.rb', line 50

def self.set_threads_timeout threads_array, timeout = 15
  time = 0

  while threads_array.count > 0

    if time < timeout
      sleep 1
      time += 1
      time_left = timeout - time

      if time === 3
        puts Message.warning "It's seems to take longer than usual..."
      end

      if time > 4 && time_left > 0
        puts Message.warning "#{timeout - time} second(s) left before timeout"
      end

      threads_array.delete_if do |thread|
        thread.status == false
      end

    else
      puts Message.error "Timout. #{threads_array.count} thread(s) not finished in time.\n\nThreads: #{threads_array}"

      threads_array.each do |thread|
        thread.kill
      end
      threads_array.clear

      abort
    end
  end
end

.start_sequence(sequence_name, max_retries = 1) ⇒ Object



5
6
7
8
9
10
11
12
13
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
42
43
44
45
46
47
48
# File 'lib/cavalerie_web/managers/sequence_manager.rb', line 5

def self.start_sequence sequence_name, max_retries=1
  Dir.chdir $gem_path

  klass = Object.const_get(format_to_sequence_class(sequence_name))
  human_sequence_name = format_for_human sequence_name
  retry_counter = 0
  sequence_ended = false

  while !sequence_ended
    Dir.chdir $gem_path

    begin
      if retry_counter == 0
        puts Message.notice "Starting sequence \"#{human_sequence_name}\""
      else
        puts Message.notice "Restarting sequence \"#{human_sequence_name}\" (try: #{retry_counter + 1})"
      end

      klass.start

      sequence_ended = true

    rescue StandardError => initial_error
      begin

        puts Message.warning "Warning: #{initial_error.class}: #{initial_error.message}\n\nCurrent path: #{Dir.pwd}\n\nBacktrace:\n\n" + initial_error.backtrace.join("\n") + "\n\n"

        retry_counter += 1
        raise MaxRetriesReachError, "Sequence \"#{human_sequence_name}\" restarted #{max_retries} time(s) but never completed successfully. Initial error was:\n\n\t#{initial_error.class}: #{initial_error.message}" unless retry_counter < max_retries

        puts Message.notice "Rescuing #{initial_error.class} from sequence #{klass.to_s}"

        klass.rescue initial_error
        next

      rescue StandardError => error
        SetupManager.reset_setup_status
        sequence_ended = true
        abort Message.error "Failed to rescue error. #{error.message}\n\nBacktrace:\n\n" + error.backtrace.join("\n")
      end

    end
  end
end