async_magic 0.1.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 +7 -0
- data/.rspec +1 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +179 -0
- data/Rakefile +6 -0
- data/docs/async_magic.png +0 -0
- data/lib/async_magic/async.rb +79 -0
- data/lib/async_magic/railtie.rb +23 -0
- data/lib/async_magic/version.rb +5 -0
- data/lib/async_magic.rb +23 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c78bb8f9e0a8a26288148d46b325f23e6dfe1e5cdcff4a050efab84bba83eeb6
|
4
|
+
data.tar.gz: 8e76dde7e7c7ededa633c78980ced678e97b5fb81d84b1bff5585a53c786ff41
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 99d6c619806f99675fe75020d74e6076bf95953ef134576ff7334aadeab4b1efaa08945ce063fca12fb55ab8a5de02f13fe0aa59b929d3a83ab9299640884c1e
|
7
|
+
data.tar.gz: da8498a477bc14c89ffa967b41cc3152bec26f53720df9aab5153c6e1a2f575d0751eeb2d87a2c0e6dd5478fc13137105a4846b7a55e1c1be100f46cd6340fad
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Igor Kasyanchuk
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
# AsyncMagic
|
2
|
+
|
3
|
+

|
4
|
+
|
5
|
+
AsyncMagic is a lightweight Ruby gem that makes it effortless to add asynchronous method execution to your Ruby classes. With a simple `async` declaration, your methods will execute in a managed thread pool, providing non-blocking operations while maintaining clean, readable code.
|
6
|
+
|
7
|
+
Inspired by Rails [`async`](https://github.com/rails/rails/blob/9ffd264e016ee5501b19286c2f48d952e910e96c/activejob/lib/active_job/queue_adapters/async_adapter.rb#L89) methods.
|
8
|
+
|
9
|
+
## Features
|
10
|
+
|
11
|
+
- โจ Simple `async` declaration for asynchronous methods
|
12
|
+
- ๐งต Managed thread pool with configurable options
|
13
|
+
- ๐ Rails integration with ActiveRecord support
|
14
|
+
- ๐ฏ Support for both instance and class methods
|
15
|
+
- ๐ Built-in error logging
|
16
|
+
- โก Non-blocking execution with Future objects
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'async_magic'
|
24
|
+
```
|
25
|
+
|
26
|
+
```bash
|
27
|
+
$ bundle install
|
28
|
+
```
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
By default, AsyncMagic will include the methods in all Objects. But you can configure it to include in specific objects (ActiveRecord models, etc.) or manually include it in your classes.
|
33
|
+
|
34
|
+
See the [Configuration](./lib/async_magic.rb) for more details.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class UserHeavyLogic
|
38
|
+
async
|
39
|
+
def send_welcome_email(user)
|
40
|
+
# ... your code
|
41
|
+
end
|
42
|
+
|
43
|
+
async
|
44
|
+
def self.batch_process(users)
|
45
|
+
# ... your code
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# or
|
50
|
+
|
51
|
+
class UsersController < ApplicationController
|
52
|
+
def index
|
53
|
+
# ... your code
|
54
|
+
log_user_activity
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
async
|
60
|
+
def log_user_activity
|
61
|
+
# ... your code
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
## More examples
|
67
|
+
|
68
|
+
- check if completed
|
69
|
+
- get the result
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
mailer = UserMailer.new
|
73
|
+
future = mailer.send_welcome_email(user)
|
74
|
+
# Check if completed
|
75
|
+
future.completed?
|
76
|
+
# Get the result (blocks until complete)
|
77
|
+
result = future.value
|
78
|
+
```
|
79
|
+
|
80
|
+
- initializer to configure the thread pool
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
# config/initializers/async_magic.rb
|
84
|
+
|
85
|
+
AsyncMagic.configure do |config|
|
86
|
+
# Include in all ActiveRecord models
|
87
|
+
config.include_in = :active_record
|
88
|
+
# Or include globally (available everywhere)
|
89
|
+
# config.include_in = :global
|
90
|
+
# Or disable auto-inclusion
|
91
|
+
# config.include_in = nil
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
- configure the thread pool
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
AsyncMagic.configure do |config|
|
99
|
+
config.executor_options = {
|
100
|
+
min_threads: 10, # Minimum number of threads
|
101
|
+
max_threads: 50, # Maximum number of threads
|
102
|
+
auto_terminate: true, # Automatically terminate threads
|
103
|
+
idletime: 60, # Thread idle time (seconds)
|
104
|
+
max_queue: 0, # Unlimited queue size
|
105
|
+
fallback_policy: :caller_runs # What to do when queue is full
|
106
|
+
}
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
- error handling
|
111
|
+
```ruby
|
112
|
+
class MyService
|
113
|
+
include AsyncMagic
|
114
|
+
|
115
|
+
async
|
116
|
+
def risky_operation
|
117
|
+
# If this raises an error, it will be logged
|
118
|
+
raise "Something went wrong!"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
The error will be logged and the future will contain the error
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
future = MyService.new.risky_operation
|
127
|
+
future.rejected? # => true
|
128
|
+
future.reason # => #<RuntimeError: Something went wrong!>
|
129
|
+
```
|
130
|
+
|
131
|
+
- manually include AsyncMagic
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
class DataProcessor
|
135
|
+
include AsyncMagic
|
136
|
+
|
137
|
+
async
|
138
|
+
def process_data(data)
|
139
|
+
# Heavy processing here
|
140
|
+
end
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
processor = DataProcessor.new
|
146
|
+
future = processor.process_data(large_dataset)
|
147
|
+
# Wait for completion with timeout
|
148
|
+
if future.wait(5) # waits up to 5 seconds
|
149
|
+
result = future.value
|
150
|
+
else
|
151
|
+
puts "Processing is taking too long!"
|
152
|
+
end
|
153
|
+
```
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
AsyncMagic.shutdown # Waits up to 5 seconds for completion
|
157
|
+
```
|
158
|
+
|
159
|
+
## Code style
|
160
|
+
|
161
|
+
Just use:
|
162
|
+
|
163
|
+
```bash
|
164
|
+
$ bundle exec standardrb
|
165
|
+
# or
|
166
|
+
$ bundle exec standardrb --fix
|
167
|
+
```
|
168
|
+
|
169
|
+
## Contributing
|
170
|
+
|
171
|
+
You are welcome to contribute to this gem.
|
172
|
+
|
173
|
+
## License
|
174
|
+
|
175
|
+
This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
176
|
+
|
177
|
+
## Credits
|
178
|
+
|
179
|
+
Thanks AI to help me write this gem faster :)
|
data/Rakefile
ADDED
Binary file
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require "concurrent"
|
2
|
+
require "logger"
|
3
|
+
require "active_support/concern"
|
4
|
+
|
5
|
+
module AsyncMagic
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
DEFAULT_EXECUTOR_OPTIONS = {
|
9
|
+
min_threads: 10,
|
10
|
+
max_threads: 50,
|
11
|
+
auto_terminate: true,
|
12
|
+
idletime: 60, # 1 minute
|
13
|
+
max_queue: 0, # unlimited
|
14
|
+
fallback_policy: :caller_runs
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
THREAD_POOL = Concurrent::ThreadPoolExecutor.new(DEFAULT_EXECUTOR_OPTIONS)
|
18
|
+
|
19
|
+
ASYNC_LOGGER = Logger.new($stdout)
|
20
|
+
ASYNC_LOGGER.level = Logger::ERROR
|
21
|
+
|
22
|
+
included do
|
23
|
+
class_attribute :async_methods_list, instance_accessor: false, default: Set.new
|
24
|
+
class_attribute :async_class_methods_list, instance_accessor: false, default: Set.new
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
def async
|
29
|
+
@async_next_method = true
|
30
|
+
end
|
31
|
+
|
32
|
+
def singleton_method_added(method_name)
|
33
|
+
super
|
34
|
+
if defined?(@async_next_method) && @async_next_method
|
35
|
+
@async_next_method = false
|
36
|
+
|
37
|
+
original_method = method(method_name)
|
38
|
+
define_singleton_method(method_name) do |*args, &block|
|
39
|
+
future = Concurrent::Future.execute(executor: THREAD_POOL) do
|
40
|
+
original_method.call(*args, &block)
|
41
|
+
rescue => e
|
42
|
+
ASYNC_LOGGER.error("Async class method #{method_name} failed: #{e.message}")
|
43
|
+
ASYNC_LOGGER.error(e.backtrace.join("\n"))
|
44
|
+
raise e
|
45
|
+
end
|
46
|
+
future
|
47
|
+
end
|
48
|
+
|
49
|
+
async_class_methods_list << method_name
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def method_added(method_name)
|
54
|
+
super
|
55
|
+
if defined?(@async_next_method) && @async_next_method
|
56
|
+
@async_next_method = false
|
57
|
+
|
58
|
+
original_method = instance_method(method_name)
|
59
|
+
define_method(method_name) do |*args, &block|
|
60
|
+
future = Concurrent::Future.execute(executor: THREAD_POOL) do
|
61
|
+
original_method.bind_call(self, *args, &block)
|
62
|
+
rescue => e
|
63
|
+
ASYNC_LOGGER.error("Async method #{method_name} failed: #{e.message}")
|
64
|
+
ASYNC_LOGGER.error(e.backtrace.join("\n"))
|
65
|
+
raise e
|
66
|
+
end
|
67
|
+
future
|
68
|
+
end
|
69
|
+
|
70
|
+
async_methods_list << method_name
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.shutdown
|
76
|
+
THREAD_POOL.shutdown
|
77
|
+
THREAD_POOL.wait_for_termination(5) # Wait up to 5s
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "rails/railtie"
|
2
|
+
|
3
|
+
module AsyncMagic
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
initializer "async_magic.configure_inclusion" do
|
6
|
+
case AsyncMagic.include_in
|
7
|
+
when :active_record
|
8
|
+
ActiveSupport.on_load(:active_record) do
|
9
|
+
ActiveRecord::Base.include AsyncMagic
|
10
|
+
end
|
11
|
+
when :global
|
12
|
+
Object.include AsyncMagic
|
13
|
+
else
|
14
|
+
# If :include_in is anything else (including nil), do not auto-include.
|
15
|
+
# The user must manually include AsyncMagic in their classes.
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
at_exit do
|
20
|
+
AsyncMagic.shutdown
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/async_magic.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "async_magic/version"
|
2
|
+
require "async_magic/async"
|
3
|
+
require "async_magic/railtie" if defined?(Rails)
|
4
|
+
|
5
|
+
module AsyncMagic
|
6
|
+
mattr_accessor :executor_options, default: DEFAULT_EXECUTOR_OPTIONS
|
7
|
+
mattr_accessor :include_in, default: :global
|
8
|
+
|
9
|
+
def self.configure
|
10
|
+
yield self
|
11
|
+
rebuild_executor
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.rebuild_executor
|
15
|
+
THREAD_POOL.shutdown
|
16
|
+
THREAD_POOL.wait_for_termination(5)
|
17
|
+
AsyncMagic.send(:remove_const, :THREAD_POOL) if AsyncMagic.const_defined?(:THREAD_POOL)
|
18
|
+
AsyncMagic.const_set(
|
19
|
+
:THREAD_POOL,
|
20
|
+
Concurrent::ThreadPoolExecutor.new(executor_options)
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: async_magic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Kasyanchuk
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-12-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: concurrent-ruby
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: diff-lcs
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: debug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Simple async methods for Ruby, Ruby on Rails applications
|
98
|
+
email:
|
99
|
+
- igorkasyanchuk@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".rspec"
|
105
|
+
- ".standard.yml"
|
106
|
+
- CHANGELOG.md
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- docs/async_magic.png
|
111
|
+
- lib/async_magic.rb
|
112
|
+
- lib/async_magic/async.rb
|
113
|
+
- lib/async_magic/railtie.rb
|
114
|
+
- lib/async_magic/version.rb
|
115
|
+
homepage: https://github.com/igorkasyanchuk/async_magic
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata:
|
119
|
+
homepage_uri: https://github.com/igorkasyanchuk/async_magic
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: 3.0.0
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubygems_version: 3.5.22
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Simple async methods for Ruby, Ruby on Rails applications
|
139
|
+
test_files: []
|