async_block 0.2.1 → 0.2.2

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: edf6ec5cd97396b384affe1e3fe9b20e54e63bc8f25aeb364f0bfe8053e3cd85
4
+ data.tar.gz: 333fe7fca486e6ce5bd333009c7c1f963b15712785db67a48000554c36b78577
5
5
  SHA512:
6
- metadata.gz: 9c0eb1637ea4dcd557a281b534df1419bbabaeae9606bfccf52539b332f7df91775b29fa5e70812f7251be315b01737f436073cdef4ab521bb118974c9801c88
7
- data.tar.gz: cdd31ac2bf9a444fe0481884c3f10a5d755a997dd554f3c16243e2991e484f353f1ce8b1c96ff78d635aba2a9ad637aa2a5784d00533417374cf8d8a5971415c
6
+ metadata.gz: ecacd457ba31dc5fdc76e04526f25087a2b637b36172411fb335a07cacad8b30c6df22ea43dbc505cd612ad0a412f56c7aab78aab40654558dd7010bdae0aca0
7
+ data.tar.gz: c057869a25f1725b9046452dcb11ac5906b57eeb2b754753b2d7476acac939168d54559bad2b7f9d340035124d656cec97ca15dc9f1bfb5271086f4b2fff730c
@@ -5,11 +5,17 @@ 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)
12
- execute_block!(block)
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
+ @block = str_to_block(source)
17
+ @on_error = str_to_block(on_error_source)
18
+ execute_block!
13
19
  end
14
20
 
15
21
  private
@@ -18,8 +24,16 @@ module AsyncBlock
18
24
  SourceToBlock.new(source).convert
19
25
  end
20
26
 
21
- def execute_block!(block)
22
- block&.call
27
+ def execute_block!(_block)
28
+ @block.call
29
+ end
30
+
31
+ def send_error_message(job, error)
32
+ error_message = "Stopped retrying #{job.class} (JID #{job.job_id})
33
+ with #{job.arguments.join(', ')} due to
34
+ '#{error.class} - #{error.message}'.
35
+ This job was retried for #{job.executions} times.".squish
36
+ @on_error.call(error_message)
23
37
  end
24
38
  end
25
39
  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.2'
5
5
  end
data/lib/async_block.rb CHANGED
@@ -10,10 +10,16 @@ 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: default_on_error, &block)
14
14
  source = BlockToSource.new(block).convert
15
+ on_error_source = BlockToSource.new(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.nil?
18
+ AsyncWrapperJob.set(queue: queue.to_sym).perform_later(source, on_error_source)
19
+ else
20
+ AsyncWrapperJob.set(wait: delay, queue: queue.to_sym).perform_later(source, on_error_source)
21
+ end
18
22
  end
23
+
24
+ def self.default_on_error; end
19
25
  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.2
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-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport