Module: SkyRunner::Job

Defined in:
lib/skyrunner/job.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#skyrunner_job_idObject

Returns the value of attribute skyrunner_job_id.



2
3
4
# File 'lib/skyrunner/job.rb', line 2

def skyrunner_job_id
  @skyrunner_job_id
end

#skyrunner_job_is_soloObject

Returns the value of attribute skyrunner_job_is_solo.



3
4
5
# File 'lib/skyrunner/job.rb', line 3

def skyrunner_job_is_solo
  @skyrunner_job_is_solo
end

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/skyrunner/job.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#consume!(job_args, task_args) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/skyrunner/job.rb', line 115

def consume!(job_args, task_args)
  begin
    self.send(task_args[0].to_sym, task_args[1].symbolize_keys)
    handle_task_completed!(job_args)
  rescue Exception => e
    handle_task_failed!(job_args) rescue nil
    raise
  end
end

#execute!(job_args = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/skyrunner/job.rb', line 35

def execute!(job_args = {})
  return execute_local!(job_args) if SkyRunner::run_locally

  job_id = SecureRandom.hex
  self.skyrunner_job_id = job_id

  table = nil
  record = nil

  queue = SkyRunner.sqs_queue
  pending_args = []
  fired_solo = false

  flush = lambda do
    messages = pending_args.map do |task_args|
      { job_id: job_id, task_id: SecureRandom.hex, job_args: job_args, task_args: task_args, job_class: self.class.name }
    end

    if record.nil?
      # Run an un-coordinated solo job if only one message
      if messages.size > 1
        SkyRunner::retry_dynamo_db do
          table = SkyRunner.dynamo_db_table
          record = table.items.put(id: job_id, created_at: Time.now.to_s, task_id: job_id, class: self.class.name, args: job_args.to_json, total_tasks: 1, completed_tasks: 0, done: 0, failed: 0)
        end
      else
        fired_solo = true

        messages.each do |m|
          m[:is_solo] = true
        end
      end
    end

    messages = messages.map(&:to_json)

    dropped_message_count = 0
    pending_args.clear

    begin
      queue.batch_send(messages)
    rescue AWS::SQS::Errors::BatchSendError => e
      dropped_message_count = e.errors.size

      # Re-add dropped args
      e.errors.each do |error|
        pending_args << JSON.parse(error[:message_body])["task_args"]
      end
    end

    if record
      SkyRunner::retry_dynamo_db do
        record.attributes.add({ total_tasks: messages.size - dropped_message_count })
      end
    end
  end

  self.run(job_args) do |*task_args|
    pending_args << task_args

    if pending_args.size >= SkyRunner::SQS_MAX_BATCH_SIZE
      1.upto(5) do
        flush.()
        sleep 5 if pending_args.size > 0
        break if pending_args.size == 0
      end
    end
  end

  1.upto(5) do
    flush.() if pending_args.size > 0
    sleep 5 if pending_args.size > 0
    break if pending_args.size == 0
  end

  unless fired_solo
    handle_task_completed!(job_args)
  end
end

#fire_post_event_method(event_type, job_args) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/skyrunner/job.rb', line 125

def fire_post_event_method(event_type, job_args)
  (self.class.job_event_methods[event_type] || []).each do |method|
    if self.method(method).arity == 0 && self.method(method).parameters.size == 0
      self.send(method)
    else
      self.send(method, job_args.symbolize_keys)
    end
  end
end

#is_solo?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/skyrunner/job.rb', line 31

def is_solo?
  self.skyrunner_job_is_solo
end