async_block 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f4eda1460ecfb9b05ded3656325ee3f8d081376b63f71588d06b06cde53212c
4
- data.tar.gz: 015b40f6cc3551c05183d190a2e95530b1a26fd7d6a331bd62b7eab5db7c9000
3
+ metadata.gz: ea3a6e56db84717647f95b697dd3df1d7b70a586a0b91b25012e3a59eaa0a03c
4
+ data.tar.gz: 4e3f8d7d5008271e2a2e5a945d032b5267190dccaff29f53b08b7b7213f6042d
5
5
  SHA512:
6
- metadata.gz: 9c0eb1637ea4dcd557a281b534df1419bbabaeae9606bfccf52539b332f7df91775b29fa5e70812f7251be315b01737f436073cdef4ab521bb118974c9801c88
7
- data.tar.gz: cdd31ac2bf9a444fe0481884c3f10a5d755a997dd554f3c16243e2991e484f353f1ce8b1c96ff78d635aba2a9ad637aa2a5784d00533417374cf8d8a5971415c
6
+ metadata.gz: 53625548312d7a9d4254bcccd77b0839471d705129ce0ddcfbba848aa794a4137d35d5751256d6494a716fbf1885fe391bbe57dcf4bbe88c2e60f76dc58917b2
7
+ data.tar.gz: d19e25f430c30a68b9db9a30c0f41c626d5df1a221dbc51b3fb2754a8098ecdeb3358fa711583c27810ab30d1c530a0174cc692357e3f9e1a204c1943d41f12f
@@ -5,21 +5,37 @@ require_relative 'block_source_service'
5
5
  module AsyncBlock
6
6
  # Job to dynamically run a block of code
7
7
  class AsyncWrapperJob < ActiveJob::Base
8
+ class CodeBlockError < StandardError; end
8
9
  queue_as :async_wrapper_job
9
10
 
10
- def perform(source)
11
- block = str_to_block(source)
11
+ retry_on StandardError, wait: :exponentially_longer, attempts: 3 do |job, error|
12
+ send_error_message(job, error)
13
+ end
14
+
15
+ def perform(source, on_error_source)
16
+ # Converts source code to callable block
17
+ block = source_to_block(source)
18
+ # Puts :on_error method in instance
19
+ source_to_block(on_error_source).call
12
20
  execute_block!(block)
13
21
  end
14
22
 
15
23
  private
16
24
 
17
- def str_to_block(source)
25
+ def source_to_block(source)
18
26
  SourceToBlock.new(source).convert
19
27
  end
20
28
 
21
29
  def execute_block!(block)
22
- block&.call
30
+ block.call
31
+ end
32
+
33
+ def send_error_message(job, error)
34
+ error_message = "Stopped retrying #{job.class} (JID #{job.job_id})
35
+ with #{job.arguments.join(', ')} due to
36
+ '#{error.class} - #{error.message}'.
37
+ This job was retried for #{job.executions} times.".squish
38
+ on_error(error_message)
23
39
  end
24
40
  end
25
41
  end
@@ -2,5 +2,4 @@
2
2
 
3
3
  module AsyncBlock
4
4
  DEFAULT_QUEUE_NAME = 'async_wrapper_job'
5
- DEFAULT_DELAY = 1.second
6
5
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AsyncBlock
4
- VERSION = '0.2.1'
4
+ VERSION = '0.2.3'
5
5
  end
data/lib/async_block.rb CHANGED
@@ -10,10 +10,18 @@ require_relative 'async_block/async_wrapper_job'
10
10
  module AsyncBlock
11
11
  class Error < StandardError; end
12
12
 
13
- def self.perform(queue: nil, delay: nil, &block)
13
+ def self.perform(queue: nil, delay: nil, on_error: nil, &block)
14
14
  source = BlockToSource.new(block).convert
15
+ on_error_source = BlockToSource.new(on_error || default_on_error).convert
15
16
  queue ||= DEFAULT_QUEUE_NAME
16
- delay ||= DEFAULT_DELAY
17
- AsyncWrapperJob.set(wait: delay, queue: queue.to_sym).perform_later(source)
17
+ if delay.instance_of?(ActiveSupport::Duration)
18
+ AsyncWrapperJob.set(wait: delay, queue: queue.to_sym).perform_later(source, on_error_source)
19
+ else
20
+ AsyncWrapperJob.set(queue: queue.to_sym).perform_later(source, on_error_source)
21
+ end
22
+ end
23
+
24
+ def self.default_on_error(error_message)
25
+ puts('AsyncBlockError', error_message)
18
26
  end
19
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async_block
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adittya Dey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-12 00:00:00.000000000 Z
11
+ date: 2023-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport