Module: SkyRunner
- Defined in:
- lib/skyrunner.rb,
lib/skyrunner/engine.rb,
lib/generators/sky_runner/install/install_generator.rb
Defined Under Namespace
Modules: Generators, Job Classes: Engine
Constant Summary collapse
- SQS_MAX_BATCH_SIZE =
Constant defined by AWS
10
- @@dynamo_db_table_name =
"skyrunner_jobs"
- @@dynamo_db_read_capacity =
10
- @@dynamo_db_write_capacity =
10
- @@sqs_queue_name =
"skyrunner_tasks"
- @@sqs_visibility_timeout =
90
- @@sqs_message_retention_period =
345600
- @@logger =
Log4r::Logger.new("skyrunner")
- @@consumer_threads =
10
- @@run_locally =
false
- @@stop_consuming_mutex =
Mutex.new
Class Method Summary collapse
- .consume!(&block) ⇒ Object
- .dynamo_db_table ⇒ Object
- .init!(params = {}) ⇒ Object
- .log(type, message) ⇒ Object
- .retry_dynamo_db(&block) ⇒ Object
- .setup {|_self| ... } ⇒ Object
- .sqs_queue ⇒ Object
- .stop_consuming!(its_a_trap = false) ⇒ Object
- .stop_consuming? ⇒ Boolean
Class Method Details
.consume!(&block) ⇒ Object
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/skyrunner.rb', line 65 def self.consume!(&block) raise "Queue #{SkyRunner::sqs_queue_name} not found. Try running 'skyrunner init'" unless sqs_queue raise "DynamoDB table #{SkyRunner::dynamo_db_table_name} not found. Try running 'skyrunner init'" unless dynamo_db_table && dynamo_db_table.exists? local_queue = Queue.new threads = [] 1.upto(SkyRunner::consumer_threads) do threads << Thread.new do table = SkyRunner::dynamo_db_table loop do begin if local_queue.empty? break if SkyRunner::stop_consuming? sleep 1 next end klass, job_id, task_id, job_args, task_args, is_solo, = local_queue.pop if klass begin unless is_solo # Avoid running the same task twice, enter record and raise error if exists already. SkyRunner::retry_dynamo_db do table.items.put({ id: "#{job_id}-tasks", task_id: task_id }, unless_exists: ["id", "task_id"]) end end SkyRunner::log :info, "Run Task: #{task_args} Job: #{job_id} Message: #{.id}" job = klass.new job.skyrunner_job_id = job_id job.skyrunner_job_is_solo = is_solo begin job.consume!(job_args, task_args) .delete rescue Exception => e .delete rescue nil block.call(e) if block_given? SkyRunner::log :error, "Task Failed: #{task_args} Job: #{job_id} #{e.} #{e.backtrace.join("\n")}" end rescue AWS::DynamoDB::Errors::ConditionalCheckFailedException => e .delete rescue nil end end rescue Exception => e puts e. puts e.backtrace.join("\n") raise e end end end end 1.upto((SkyRunner::consumer_threads.to_f / SQS_MAX_BATCH_SIZE).ceil + 1) do threads << Thread.new do begin loop do table = SkyRunner::dynamo_db_table queue = sqs_queue break if SkyRunner::stop_consuming? sleep 1 while local_queue.size >= SkyRunner::consumer_threads = [] queue.(limit: SQS_MAX_BATCH_SIZE, wait_time_seconds: 5) do || << [, JSON.parse(.body)] end next unless .size > 0 job_ids = .select { |m| !m[1]["is_solo"] }.map { |m| [m[1]["job_id"], m[1]["job_id"]] }.uniq job_records = {} if job_ids.uniq.size > 0 SkyRunner::retry_dynamo_db do # Read DynamoDB records into job and task lookup tables. table.batch_get(["id", "task_id", "failed"], job_ids.uniq, consistent_read: true) do |record| job_records[record["id"]] = record end end end .each do || , = job_id = ["job_id"] task_id = ["task_id"] is_solo = ["is_solo"] job_args = ["job_args"] task_args = ["task_args"] job_record = job_records[job_id] if is_solo || (job_record && job_record["failed"] == 0) begin klass = Kernel.const_get(["job_class"]) local_queue.push([klass, job_id, task_id, job_args, task_args, is_solo, ]) rescue NameError => e block.call(e) if block_given? .delete rescue nil log :error, "Task Failed: No such class #{["job_class"]} #{e.}" end else .delete rescue nil end end end rescue Exception => e puts e. puts e.backtrace.join("\n") raise e end end end log :info, "Consumer started." threads.each(&:join) true end |
.dynamo_db_table ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/skyrunner.rb', line 196 def self.dynamo_db_table table = Thread.current.thread_variable_get(:skyrunner_dyn_table) return table if table dynamo_db.tables[SkyRunner::dynamo_db_table_name].tap do |table| table.load_schema if table && table.exists? Thread.current.thread_variable_set(:skyrunner_dyn_table, table) end end |
.init!(params = {}) ⇒ Object
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/skyrunner.rb', line 19 def self.init!(params = {}) table = self.dynamo_db_table if !table.exists? || params[:purge] table_name = SkyRunner::dynamo_db_table_name if table.exists? && params[:purge] SkyRunner.log :warn, "Purging DynamoDB table #{table_name}." table.delete sleep 1 while table.exists? end SkyRunner.log :info, "Creating DynamoDB table #{table_name}." table = dynamo_db.tables.create(table_name, SkyRunner::dynamo_db_read_capacity, SkyRunner::dynamo_db_write_capacity, hash_key: { id: :string }, range_key: { task_id: :string }) sleep 1 while table.status == :creating end queue = self.sqs_queue if !queue || params[:purge] queue_name = SkyRunner::sqs_queue_name if queue && params[:purge] SkyRunner.log :warn, "Purging SQS queue #{queue_name}. Waiting 65 seconds to re-create." queue.delete sleep 65 end SkyRunner.log :info, "Creating SQS queue #{queue_name}." queue = sqs.queues.create(queue_name, visibility_timeout: SkyRunner::sqs_visibility_timeout, message_retention_period: SkyRunner::) end true end |
.log(type, message) ⇒ Object
214 215 216 |
# File 'lib/skyrunner.rb', line 214 def self.log(type, ) SkyRunner::logger.send(type, "[SkyRunner] #{}") end |
.retry_dynamo_db(&block) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/skyrunner.rb', line 265 def self.retry_dynamo_db(&block) handler = Proc.new do |exception, num, delay| if exception SkyRunner.log :warn, "Having to retry DynamoDB requests. #{exception.}" end end with_retries(handler: handler, max_tries: 100, rescue: AWS::DynamoDB::Errors::ProvisionedThroughputExceededException, base_sleep_seconds: 2, max_sleep_seconds: 60) do block.call end end |
.setup {|_self| ... } ⇒ Object
15 16 17 |
# File 'lib/skyrunner.rb', line 15 def self.setup yield self end |
.sqs_queue ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/skyrunner.rb', line 206 def self.sqs_queue begin sqs.queues.named(SkyRunner::sqs_queue_name) rescue AWS::SQS::Errors::NonExistentQueue => e return nil end end |
.stop_consuming!(its_a_trap = false) ⇒ Object
255 256 257 258 259 260 261 262 263 |
# File 'lib/skyrunner.rb', line 255 def self.stop_consuming!(its_a_trap=false) if its_a_trap SkyRunner::stop_consuming_flag = true else @@stop_consuming_mutex.synchronize do SkyRunner::stop_consuming_flag = true end end end |
.stop_consuming? ⇒ Boolean
249 250 251 252 253 |
# File 'lib/skyrunner.rb', line 249 def self.stop_consuming? @@stop_consuming_mutex.synchronize do SkyRunner::stop_consuming_flag end end |