async_block 0.1.9 → 0.2.0

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: 980162aac88aba7f5243484afe563360d0b0ca7f36c4791e67b53b74bfaa9eed
4
- data.tar.gz: d1b41a0d31b594b53ebf804851baf79a17396650e4c9c85393d2345c3dfb29c3
3
+ metadata.gz: f4bffa0f5cc7922087739ef5df009ad311aaa33fb259bd277f62b5cf8ed6666b
4
+ data.tar.gz: 1e1f2af060baf563bd6402d5b3b8f49b9b3791156fc3549f2471ec8ba5c1e397
5
5
  SHA512:
6
- metadata.gz: 1b740ff8cd0f0a97114d6cb7e2bd1a64614aebf484a46edf08cd329d2f5162efe77c874642314823e93268002825902c435c6eb7c50a34e6ab4b107280a99c10
7
- data.tar.gz: 77a1af646a875c7913913f31e8d2c98301405327b0135de34a7edd4170ab6dbba32e03cf3a7f7fe91b07f19c1d55826db35e38197586e5392a0df9432d2284c5
6
+ metadata.gz: 7beb4c8fdea5aaeba84aea747932707beba14c7a7e6f610d2feddacb14feb18612c5d782802e109226abd745a72b6fa30ef32333de51c40db745148d0b3d1004
7
+ data.tar.gz: 22f23aeb80243b8c3f378cc551eb133f4e2a2a6c74b624f42589b3182c2ad70404bd8ce7baf42995ec289ba3b6c5823b94834487b398fea22de9f16b0ef5aa7d
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # AsyncBlock
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/async_block`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ AsyncBlock - A usable code block for rails to run your code asynchronously, this will help in preventing writing of new jobs to run smaller pieces of code
6
4
 
7
5
  ## Installation
8
6
 
@@ -16,6 +14,9 @@ If bundler is not being used to manage dependencies, install the gem by executin
16
14
 
17
15
  ## Usage
18
16
 
17
+ Add your code in wrapped inside AsyncBlock as described below.
18
+ \*\* NOTE - AsyncBlock cannot be executed from irb/console
19
+
19
20
  ```
20
21
  AsyncBlock.perform_async do
21
22
  puts('This is running in a temporary job')
@@ -24,8 +25,18 @@ end
24
25
  ```
25
26
 
26
27
  To put some delay on the async execution (say 5 minutes):
28
+
29
+ ```
30
+ AsyncBlock.perform(delay: 5.minutes) do
31
+ puts('This is running in a temporary job')
32
+ puts('Put your code below')
33
+ end
34
+ ```
35
+
36
+ To explicitly mention a queue for the async execution:
37
+
27
38
  ```
28
- AsyncBlock.perform_with_delay(5.minutes) do
39
+ AsyncBlock.perform(delay: 1.minute, queue: 'p3') do
29
40
  puts('This is running in a temporary job')
30
41
  puts('Put your code below')
31
42
  end
data/async_block.gemspec CHANGED
@@ -36,8 +36,6 @@ Gem::Specification.new do |spec|
36
36
 
37
37
  # Uncomment to register a new dependency of your gem
38
38
  spec.add_dependency 'activesupport', ['>= 3.0', '< 8.0']
39
- # spec.add_dependency 'ParseTree', '~> 3.0'
40
- # spec.add_dependency 'ruby2ruby', '~> 2.5.0'
41
39
 
42
40
  # For more information and examples about making a new gem, check out our
43
41
  # guide at: https://bundler.io/guides/creating_gem.html
@@ -7,15 +7,15 @@ module AsyncBlock
7
7
  class AsyncWrapperJob < ActiveJob::Base
8
8
  queue_as :async_wrapper_job
9
9
 
10
- def perform(block_string)
11
- block = str_to_block(block_string)
10
+ def perform(source)
11
+ block = str_to_block(source)
12
12
  execute_block!(block)
13
13
  end
14
14
 
15
15
  private
16
16
 
17
- def str_to_block(block_string)
18
- SourceToBlock.new(block_string).convert
17
+ def str_to_block(source)
18
+ SourceToBlock.new(source).convert
19
19
  end
20
20
 
21
21
  def execute_block!(block)
@@ -13,12 +13,12 @@ module AsyncBlock
13
13
  end
14
14
 
15
15
  def convert
16
- block_to_str
16
+ block_to_source
17
17
  end
18
18
 
19
19
  private
20
20
 
21
- def block_to_str
21
+ def block_to_source
22
22
  @block&.source
23
23
  end
24
24
  end
@@ -31,18 +31,18 @@ module AsyncBlock
31
31
  # block = BlockToSource.new(block_string).convert
32
32
  # block&.call
33
33
  class SourceToBlock
34
- def initialize(block_string)
35
- @block_string = block_string
34
+ def initialize(source)
35
+ @source = source
36
36
  end
37
37
 
38
38
  def convert
39
- str_to_block
39
+ source_to_block
40
40
  end
41
41
 
42
42
  private
43
43
 
44
- def str_to_block
45
- instance_eval("lambda { #{@block_string} }", __FILE__, __LINE__)
44
+ def source_to_block
45
+ instance_eval("lambda { #{@source} }", __FILE__, __LINE__)
46
46
  end
47
47
  end
48
48
  end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AsyncBlock
4
+ DEFAULT_QUEUE_NAME = 'async_wrapper_job'
5
+ DEFAULT_DELAY = 1.second
6
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AsyncBlock
4
- VERSION = '0.1.9'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/async_block.rb CHANGED
@@ -1,34 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'async_block/version'
4
+ require_relative 'async_block/constants'
4
5
  require_relative 'async_block/block_source_service'
5
6
  require_relative 'async_block/async_wrapper_job'
6
7
 
7
8
  # AsyncBlock - A usable code block for rails to run your code asynchronusly,
8
9
  # this will help in preventing writing of new jobs to run smaller pieces of code
9
- #
10
- # The following full app-like example demonstrates how to use the AsyncBlock
11
- #
12
- # AsyncBlock.perform_async do
13
- # puts('This is running in a temporary job')
14
- # puts('Put your code below')
15
- # end
16
- #
17
- # To put some delay on the async execution (say 5 minutes):
18
- # AsyncBlock.perform_with_delay(5.minutes) do
19
- # puts('This is running in a temporary job')
20
- # puts('Put your code below')
21
- # end
22
10
  module AsyncBlock
23
11
  class Error < StandardError; end
24
12
 
25
- def self.perform_async(&block)
26
- block_string = BlockToSource.new(block).convert
27
- AsyncWrapperJob.perform_later(block_string)
28
- end
29
-
30
- def self.perform_with_delay(delay, &block)
31
- block_string = BlockToSource.new(block).convert
32
- AsyncWrapperJob.set(wait: delay).perform_later(block_string)
13
+ def self.perform_async(queue: nil, delay: nil, &block)
14
+ source = BlockToSource.new(block).convert
15
+ queue ||= DEFAULT_QUEUE_NAME
16
+ delay ||= DEFAULT_DELAY
17
+ AsyncWrapperJob.set(wait: delay, queue: queue.to_sym).perform_later(source)
33
18
  end
34
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async_block
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adittya Dey
@@ -50,6 +50,7 @@ files:
50
50
  - lib/async_block.rb
51
51
  - lib/async_block/async_wrapper_job.rb
52
52
  - lib/async_block/block_source_service.rb
53
+ - lib/async_block/constants.rb
53
54
  - lib/async_block/version.rb
54
55
  - sig/async_block.rbs
55
56
  homepage: https://github.com/Velocity-Engineering/async-block