async_block 0.1.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +15 -4
- data/async_block.gemspec +0 -2
- data/lib/async_block/async_wrapper_job.rb +4 -4
- data/lib/async_block/block_source_service.rb +7 -7
- data/lib/async_block/constants.rb +6 -0
- data/lib/async_block/version.rb +1 -1
- data/lib/async_block.rb +6 -21
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4bffa0f5cc7922087739ef5df009ad311aaa33fb259bd277f62b5cf8ed6666b
|
4
|
+
data.tar.gz: 1e1f2af060baf563bd6402d5b3b8f49b9b3791156fc3549f2471ec8ba5c1e397
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7beb4c8fdea5aaeba84aea747932707beba14c7a7e6f610d2feddacb14feb18612c5d782802e109226abd745a72b6fa30ef32333de51c40db745148d0b3d1004
|
7
|
+
data.tar.gz: 22f23aeb80243b8c3f378cc551eb133f4e2a2a6c74b624f42589b3182c2ad70404bd8ce7baf42995ec289ba3b6c5823b94834487b398fea22de9f16b0ef5aa7d
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# AsyncBlock
|
2
2
|
|
3
|
-
|
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.
|
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(
|
11
|
-
block = str_to_block(
|
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(
|
18
|
-
SourceToBlock.new(
|
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
|
-
|
16
|
+
block_to_source
|
17
17
|
end
|
18
18
|
|
19
19
|
private
|
20
20
|
|
21
|
-
def
|
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(
|
35
|
-
@
|
34
|
+
def initialize(source)
|
35
|
+
@source = source
|
36
36
|
end
|
37
37
|
|
38
38
|
def convert
|
39
|
-
|
39
|
+
source_to_block
|
40
40
|
end
|
41
41
|
|
42
42
|
private
|
43
43
|
|
44
|
-
def
|
45
|
-
instance_eval("lambda { #{@
|
44
|
+
def source_to_block
|
45
|
+
instance_eval("lambda { #{@source} }", __FILE__, __LINE__)
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
data/lib/async_block/version.rb
CHANGED
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
|
-
|
27
|
-
|
28
|
-
|
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.
|
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
|