async_block 0.2.0 → 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: f4bffa0f5cc7922087739ef5df009ad311aaa33fb259bd277f62b5cf8ed6666b
4
- data.tar.gz: 1e1f2af060baf563bd6402d5b3b8f49b9b3791156fc3549f2471ec8ba5c1e397
3
+ metadata.gz: edf6ec5cd97396b384affe1e3fe9b20e54e63bc8f25aeb364f0bfe8053e3cd85
4
+ data.tar.gz: 333fe7fca486e6ce5bd333009c7c1f963b15712785db67a48000554c36b78577
5
5
  SHA512:
6
- metadata.gz: 7beb4c8fdea5aaeba84aea747932707beba14c7a7e6f610d2feddacb14feb18612c5d782802e109226abd745a72b6fa30ef32333de51c40db745148d0b3d1004
7
- data.tar.gz: 22f23aeb80243b8c3f378cc551eb133f4e2a2a6c74b624f42589b3182c2ad70404bd8ce7baf42995ec289ba3b6c5823b94834487b398fea22de9f16b0ef5aa7d
6
+ metadata.gz: ecacd457ba31dc5fdc76e04526f25087a2b637b36172411fb335a07cacad8b30c6df22ea43dbc505cd612ad0a412f56c7aab78aab40654558dd7010bdae0aca0
7
+ data.tar.gz: c057869a25f1725b9046452dcb11ac5906b57eeb2b754753b2d7476acac939168d54559bad2b7f9d340035124d656cec97ca15dc9f1bfb5271086f4b2fff730c
data/README.md CHANGED
@@ -18,7 +18,7 @@ Add your code in wrapped inside AsyncBlock as described below.
18
18
  \*\* NOTE - AsyncBlock cannot be executed from irb/console
19
19
 
20
20
  ```
21
- AsyncBlock.perform_async do
21
+ AsyncBlock.perform do
22
22
  puts('This is running in a temporary job')
23
23
  puts('Put your code below')
24
24
  end
@@ -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.0'
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_async(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.0
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