sidekiqable 0.0.1
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/.claude/settings.local.json +10 -0
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +5 -0
- data/CLAUDE.md +178 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +175 -0
- data/Rakefile +12 -0
- data/lib/sidekiqable/async_proxy.rb +50 -0
- data/lib/sidekiqable/asyncable_methods.rb +25 -0
- data/lib/sidekiqable/configuration.rb +28 -0
- data/lib/sidekiqable/railtie.rb +19 -0
- data/lib/sidekiqable/version.rb +3 -0
- data/lib/sidekiqable/worker.rb +21 -0
- data/lib/sidekiqable.rb +24 -0
- data/sig/sidekiqable.rbs +4 -0
- metadata +74 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f538c5c15a22aa8109453302ddad6bebcd1bc0cde33de3824409f663c80a77b7
|
|
4
|
+
data.tar.gz: e5f64ef77a91e6328e83bfc4058f9cbbe433c354d880364b70e393fa6fd72ea5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6269de6edc907a4fc4797467ae1956734dab6f9a4addb3fd36e41e92c2b974ebb6e46611d2fe610a649157ebaaa4f2351b44e65eec4603be2c0a24244a2e1335
|
|
7
|
+
data.tar.gz: 60ad9d4cf92e95a72ff5429c555085932b3609994581951740deb7c1d1feccc1191f4e4daf6d4ac54c8a428b2913920b1262e7c12d07a237d07e1f5d5492d129
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CLAUDE.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
+
|
|
5
|
+
## Project Overview
|
|
6
|
+
|
|
7
|
+
Sidekiqable is a Ruby gem that allows scheduling class methods asynchronously via Sidekiq without writing dedicated workers. It uses a simple proxy pattern where you prefix method calls with `perform_async`, `perform_in`, or `perform_at` (e.g., `Foo.perform_async.bar(1, 2)`).
|
|
8
|
+
|
|
9
|
+
## Development Commands
|
|
10
|
+
|
|
11
|
+
### Testing
|
|
12
|
+
```bash
|
|
13
|
+
rake test # Run all tests
|
|
14
|
+
bundle exec minitest test/test_sidekiqable.rb # Run specific test file
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Linting
|
|
18
|
+
```bash
|
|
19
|
+
rake rubocop # Run RuboCop linter
|
|
20
|
+
rubocop -A # Auto-correct offenses
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Build & Install
|
|
24
|
+
```bash
|
|
25
|
+
bundle exec rake install # Install gem locally
|
|
26
|
+
bundle exec rake release # Tag and release to RubyGems
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Interactive Console
|
|
30
|
+
```bash
|
|
31
|
+
bin/console # Launch IRB with gem loaded
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Setup
|
|
35
|
+
```bash
|
|
36
|
+
bin/setup # Install dependencies
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Architecture
|
|
40
|
+
|
|
41
|
+
### Core Components
|
|
42
|
+
|
|
43
|
+
**`Sidekiqable::AsyncableMethods`** (lib/sidekiqable/asyncable_methods.rb)
|
|
44
|
+
- Entry point module that classes extend to gain async capabilities
|
|
45
|
+
- Defines three simple methods: `perform_async`, `perform_in(interval)`, and `perform_at(timestamp)`
|
|
46
|
+
- Each method returns an `AsyncProxy` instance configured for the appropriate scheduling mode
|
|
47
|
+
- Provides `sidekiqable_options(options)` method for per-class configuration
|
|
48
|
+
- Options set per-class take precedence over global configuration
|
|
49
|
+
- No method wrapping or interception - just straightforward class methods
|
|
50
|
+
|
|
51
|
+
**`Sidekiqable::AsyncProxy`** (lib/sidekiqable/async_proxy.rb)
|
|
52
|
+
- Simple proxy returned by `perform_async`, `perform_in`, and `perform_at`
|
|
53
|
+
- Uses `method_missing` to catch the actual method call (e.g., `.boo(1, 2)`)
|
|
54
|
+
- Validates arguments are Sidekiq-serializable via `Sidekiq.dump_json` (can be disabled)
|
|
55
|
+
- Raises error if blocks are passed (cannot be serialized)
|
|
56
|
+
- Enqueues job to `Worker` with compact payload format: `["ClassName.method_name", *args]`
|
|
57
|
+
- Applies configuration options before enqueuing using Sidekiq's `.set()` API
|
|
58
|
+
- Configuration priority: per-class options > global options > Sidekiq defaults
|
|
59
|
+
|
|
60
|
+
**`Sidekiqable::Worker`** (lib/sidekiqable/worker.rb)
|
|
61
|
+
- Standard Sidekiq worker that executes scheduled method calls
|
|
62
|
+
- Receives compact payload: `["ClassName.method_name", *args]`
|
|
63
|
+
- Splits the callable string on first dot to extract class name and method name
|
|
64
|
+
- Constantizes class name and invokes method with args via `public_send`
|
|
65
|
+
|
|
66
|
+
**`Sidekiqable::Configuration`** (lib/sidekiqable/configuration.rb)
|
|
67
|
+
- Global configuration object accessed via `Sidekiqable.configuration`
|
|
68
|
+
- Supports all standard Sidekiq worker options: `queue`, `retry`, `dead`, `backtrace`, `pool`, `tags`
|
|
69
|
+
- Sidekiqable-specific option: `validate_arguments` (default: true)
|
|
70
|
+
- Returns hash of Sidekiq options for worker configuration via `sidekiq_options`
|
|
71
|
+
- Can be configured via `Sidekiqable.configure` or Rails' `config.sidekiqable`
|
|
72
|
+
|
|
73
|
+
**`Sidekiqable::Railtie`** (lib/sidekiqable/railtie.rb)
|
|
74
|
+
- Rails integration that exposes `config.sidekiqable` for environment-specific configuration
|
|
75
|
+
- Automatically loaded when Rails is detected
|
|
76
|
+
|
|
77
|
+
### Execution Flow
|
|
78
|
+
|
|
79
|
+
**Async Execution:**
|
|
80
|
+
1. User extends class with `Sidekiqable::AsyncableMethods`
|
|
81
|
+
2. User calls `Foo.perform_async` which returns an `AsyncProxy` instance
|
|
82
|
+
3. User chains method call like `.boo(1, 2)`
|
|
83
|
+
4. Proxy's `method_missing` catches the call
|
|
84
|
+
5. Proxy validates arguments and enqueues job to `Worker` with compact payload `["Foo.boo", 1, 2]`
|
|
85
|
+
6. Worker splits "Foo.boo", constantizes `Foo`, and invokes `boo(1, 2)`
|
|
86
|
+
|
|
87
|
+
**Sync Execution:**
|
|
88
|
+
1. User calls method normally: `Foo.boo(1, 2)`
|
|
89
|
+
2. Method executes immediately (no proxy involved)
|
|
90
|
+
3. Returns result directly
|
|
91
|
+
|
|
92
|
+
### Configuration System
|
|
93
|
+
|
|
94
|
+
**Three levels of configuration (in order of precedence):**
|
|
95
|
+
|
|
96
|
+
1. **Per-class options** (highest priority) - Set via `sidekiqable_options` in the class
|
|
97
|
+
```ruby
|
|
98
|
+
class Foo
|
|
99
|
+
extend Sidekiqable::AsyncableMethods
|
|
100
|
+
sidekiqable_options queue: 'high', retry: 3
|
|
101
|
+
end
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
2. **Global configuration** - Set via `Sidekiqable.configure` or Rails `config.sidekiqable`
|
|
105
|
+
```ruby
|
|
106
|
+
Sidekiqable.configure do |config|
|
|
107
|
+
config.queue = 'default'
|
|
108
|
+
config.retry = 5
|
|
109
|
+
config.validate_arguments = true
|
|
110
|
+
end
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
3. **Sidekiq defaults** (lowest priority) - Used when not configured
|
|
114
|
+
|
|
115
|
+
**Available options:**
|
|
116
|
+
- Standard Sidekiq options: `queue`, `retry`, `dead`, `backtrace`, `pool`, `tags`
|
|
117
|
+
- Sidekiqable-specific: `validate_arguments` (enables/disables argument serialization validation)
|
|
118
|
+
|
|
119
|
+
**How options are applied:**
|
|
120
|
+
- `AsyncProxy` merges per-class and global options
|
|
121
|
+
- Uses Sidekiq's `.set()` API to apply options: `Worker.set(options).perform_async(...)`
|
|
122
|
+
- This happens at enqueue time, not worker definition time
|
|
123
|
+
|
|
124
|
+
### Implementation Notes
|
|
125
|
+
|
|
126
|
+
- No method wrapping or hooks - just simple `method_missing` on proxy objects
|
|
127
|
+
- Compact payload format reduces serialization overhead
|
|
128
|
+
- Clear separation between sync and async code paths
|
|
129
|
+
- All complexity is isolated to the `AsyncProxy` class (~50 lines)
|
|
130
|
+
- Configuration is applied dynamically at enqueue time, not on the Worker class itself
|
|
131
|
+
|
|
132
|
+
## Testing Notes
|
|
133
|
+
|
|
134
|
+
- Uses Minitest for testing with Sidekiq test mode
|
|
135
|
+
- Test helper location: test/test_helper.rb
|
|
136
|
+
- Tests cover: version check, method presence, async enqueuing, delayed jobs, worker execution, block rejection, and sync calls
|
|
137
|
+
- Run with `rake test` or `bundle exec rake test`
|
|
138
|
+
|
|
139
|
+
## Key Design Decisions
|
|
140
|
+
|
|
141
|
+
### Why the current API: `Foo.perform_async.bar(1, 2)`?
|
|
142
|
+
|
|
143
|
+
This syntax was chosen for clarity and simplicity:
|
|
144
|
+
- **Clear intent**: Starting with `perform_async` makes async behavior explicit
|
|
145
|
+
- **No method wrapping**: Avoids complex metaprogramming with `singleton_method_added` hooks
|
|
146
|
+
- **Familiar**: Similar to Sidekiq's standard `Worker.perform_async` pattern
|
|
147
|
+
- **Simple implementation**: Just 3 methods + proxy with `method_missing` (~60 total lines)
|
|
148
|
+
|
|
149
|
+
Alternative syntaxes considered:
|
|
150
|
+
- `Foo.bar(1, 2).perform_async` - Requires wrapping ALL methods, adds overhead
|
|
151
|
+
- `Foo.async.bar(1, 2)` - Similar to current, but less Sidekiq-like
|
|
152
|
+
- `Foo.bar_async(1, 2)` - Requires suffix convention, less flexible
|
|
153
|
+
|
|
154
|
+
### Why compact payload format: `["Foo.bar", 1, 2]`?
|
|
155
|
+
|
|
156
|
+
- **Reduces serialization size**: One less array element per job
|
|
157
|
+
- **Cleaner Sidekiq UI**: Job args look more natural
|
|
158
|
+
- **No edge cases**: Method names can't contain dots in Ruby, so parsing is safe
|
|
159
|
+
|
|
160
|
+
Alternative considered:
|
|
161
|
+
- `["Foo", "bar", 1, 2]` - More structured but slightly larger payload
|
|
162
|
+
|
|
163
|
+
### Why dynamic configuration via `.set()`?
|
|
164
|
+
|
|
165
|
+
Configuration is applied at enqueue time using `Worker.set(options)` rather than defining `sidekiq_options` on the Worker class:
|
|
166
|
+
- **Flexibility**: Per-class options can override global config
|
|
167
|
+
- **Single worker**: One `Worker` class handles all jobs, options vary per caller
|
|
168
|
+
- **Standard Sidekiq pattern**: Uses `.set()` API that all Sidekiq users know
|
|
169
|
+
|
|
170
|
+
## Ruby Version
|
|
171
|
+
|
|
172
|
+
Requires Ruby >= 3.1.0 (specified in sidekiqable.gemspec)
|
|
173
|
+
|
|
174
|
+
## Dependencies
|
|
175
|
+
|
|
176
|
+
- sidekiq >= 6.0 (runtime dependency)
|
|
177
|
+
- minitest ~> 5.16 (development)
|
|
178
|
+
- rubocop ~> 1.21 (development)
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 TODO: Write your name
|
|
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,175 @@
|
|
|
1
|
+
# Sidekiqable
|
|
2
|
+
|
|
3
|
+
Sidekiqable lets you enqueue any class method or modules asynchronously without writing dedicated Sidekiq workers or ActiveJob wrappers. Prefix method calls with `perform_async`, `perform_in`, or `perform_at` and Sidekiqable will serialize the call into a generic worker.
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
class ReportMailer
|
|
7
|
+
extend Sidekiqable::AsyncableMethods
|
|
8
|
+
|
|
9
|
+
def self.deliver_daily(user_id)
|
|
10
|
+
UserMailer.daily_report(user_id).deliver_now
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
ReportMailer.perform_async.deliver_daily(42)
|
|
15
|
+
ReportMailer.perform_in(5.minutes).deliver_daily(42)
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
The scheduled job will execute `ReportMailer.deliver_daily(42)` later via `Sidekiqable::Worker`.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Add the gem to your application:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
bundle add sidekiqable
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Or install it manually:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
gem install sidekiqable
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Rails
|
|
35
|
+
|
|
36
|
+
When used in a Rails application the included Railtie automatically loads Sidekiqable and exposes `config.sidekiqable` for environment-specific defaults.
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
1. Add `Sidekiqable::AsyncableMethods` to any class whose class methods you want to schedule.
|
|
41
|
+
2. Prefix method calls with the desired Sidekiq scheduling helper (`perform_async`, `perform_in`, or `perform_at`).
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
class Foo
|
|
45
|
+
extend Sidekiqable::AsyncableMethods
|
|
46
|
+
|
|
47
|
+
def self.boo(a, b)
|
|
48
|
+
Rails.logger.info("boo(#{a}, #{b})")
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Immediately enqueue
|
|
53
|
+
Foo.perform_async.boo(1, 2)
|
|
54
|
+
|
|
55
|
+
# Schedule relative to now
|
|
56
|
+
Foo.perform_in(5.minutes).boo(1, 2)
|
|
57
|
+
|
|
58
|
+
# Schedule at a specific time
|
|
59
|
+
Foo.perform_at(1.day.from_now).boo(1, 2)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Jobs are enqueued with the payload `["Foo.boo", 1, 2]` and executed by `Sidekiqable::Worker`, which constantizes the class and invokes the method.
|
|
63
|
+
|
|
64
|
+
### Synchronous execution
|
|
65
|
+
|
|
66
|
+
Normal method calls execute immediately without any async behavior.
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
Foo.boo(1, 2) # => executes synchronously and returns the original value
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Configuration
|
|
73
|
+
|
|
74
|
+
#### Global Configuration
|
|
75
|
+
|
|
76
|
+
Use the global configuration to set default Sidekiq options for all classes. If not configured, Sidekiqable uses Sidekiq's default values:
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
Sidekiqable.configure do |config|
|
|
80
|
+
config.queue = "mailers" # default: "default"
|
|
81
|
+
config.retry = 5 # default: true
|
|
82
|
+
config.dead = true # default: true
|
|
83
|
+
config.backtrace = 10 # default: false (or integer for lines)
|
|
84
|
+
config.tags = ["reporting"] # default: nil
|
|
85
|
+
config.validate_arguments = true # default: true (Sidekiqable-specific)
|
|
86
|
+
end
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Inside Rails you can configure via `config.sidekiqable`:
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
# config/application.rb or an environment file
|
|
93
|
+
config.sidekiqable.queue = "low"
|
|
94
|
+
config.sidekiqable.retry = false
|
|
95
|
+
config.sidekiqable.dead = false
|
|
96
|
+
config.sidekiqable.backtrace = false
|
|
97
|
+
config.sidekiqable.tags = ["background"]
|
|
98
|
+
config.sidekiqable.validate_arguments = false # disable validation for performance
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Per-Class Configuration
|
|
102
|
+
|
|
103
|
+
You can override global options for specific classes using `sidekiqable_options`:
|
|
104
|
+
|
|
105
|
+
```ruby
|
|
106
|
+
class ReportMailer
|
|
107
|
+
extend Sidekiqable::AsyncableMethods
|
|
108
|
+
|
|
109
|
+
sidekiqable_options queue: 'high_priority',
|
|
110
|
+
retry: 3,
|
|
111
|
+
backtrace: 20,
|
|
112
|
+
tags: ['mailer', 'critical']
|
|
113
|
+
|
|
114
|
+
def self.deliver_daily(user_id)
|
|
115
|
+
# ...
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
class BackgroundTask
|
|
120
|
+
extend Sidekiqable::AsyncableMethods
|
|
121
|
+
|
|
122
|
+
sidekiqable_options queue: 'low_priority',
|
|
123
|
+
retry: false,
|
|
124
|
+
dead: false
|
|
125
|
+
|
|
126
|
+
def self.cleanup
|
|
127
|
+
# ...
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Per-class options take precedence over global configuration. Any Sidekiq option supported by `worker.set()` can be used.
|
|
133
|
+
|
|
134
|
+
#### Available options
|
|
135
|
+
|
|
136
|
+
**Sidekiq job options** (uses Sidekiq defaults when not configured):
|
|
137
|
+
|
|
138
|
+
- `queue`: Use a named queue for jobs
|
|
139
|
+
- **Default:** `"default"`
|
|
140
|
+
- `retry`: Enable retries for jobs. Can be `true`, `false`, or an integer for max retry attempts
|
|
141
|
+
- **Default:** `true`
|
|
142
|
+
- **Example:** `retry: 3` (retry up to 3 times)
|
|
143
|
+
- `dead`: Whether a failing job should go to the Dead queue after exhausting retries
|
|
144
|
+
- **Default:** `true`
|
|
145
|
+
- `backtrace`: Whether to save error backtrace in the retry payload for the Web UI. Can be `true`, `false`, or an integer for number of lines to save
|
|
146
|
+
- **Default:** `false`
|
|
147
|
+
- **Warning:** Backtraces are large and can consume significant Redis space with many retries. Consider using an error service like Honeybadger instead.
|
|
148
|
+
- `pool`: Use the given Redis connection pool to push this job type to a specific shard
|
|
149
|
+
- **Default:** `nil` (uses default Sidekiq connection)
|
|
150
|
+
- `tags`: Add an Array of tags to each job for filtering in the Web UI
|
|
151
|
+
- **Default:** `nil`
|
|
152
|
+
- **Example:** `tags: ["reporting", "daily"]`
|
|
153
|
+
|
|
154
|
+
**Sidekiqable-specific options:**
|
|
155
|
+
|
|
156
|
+
- `validate_arguments`: Enable/disable argument serialization validation before enqueuing. When enabled, provides clear error messages for non-serializable arguments. Disable for better performance if you're confident your arguments are always serializable.
|
|
157
|
+
- **Default:** `true`
|
|
158
|
+
|
|
159
|
+
### Argument safety
|
|
160
|
+
|
|
161
|
+
By default, arguments are validated with `Sidekiq.dump_json` before enqueuing to ensure they can be serialized by Sidekiq. This validation can be disabled via `config.validate_arguments = false` if needed. Passing a block while enqueuing will always raise an error because Sidekiq cannot persist blocks.
|
|
162
|
+
|
|
163
|
+
## Development
|
|
164
|
+
|
|
165
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to execute the test suite. You can also run `bin/console` to experiment with the gem.
|
|
166
|
+
|
|
167
|
+
To install the gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `lib/sidekiqable/version.rb` and then run `bundle exec rake release`. This tags the release and pushes the `.gem` to RubyGems (once your credentials are configured).
|
|
168
|
+
|
|
169
|
+
## Contributing
|
|
170
|
+
|
|
171
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/r3cha/sidekiqable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/r3cha/sidekiqable/blob/main/CODE_OF_CONDUCT.md).
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sidekiqable
|
|
4
|
+
class AsyncProxy
|
|
5
|
+
def initialize(target_class, mode, schedule_arg = nil)
|
|
6
|
+
@target_class = target_class
|
|
7
|
+
@mode = mode
|
|
8
|
+
@schedule_arg = schedule_arg
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def method_missing(method_name, *args, &block)
|
|
12
|
+
raise Sidekiqable::Error, "Cannot enqueue blocks to Sidekiq" if block
|
|
13
|
+
|
|
14
|
+
super unless %i[perform_async perform_in perform_at].include?(@mode)
|
|
15
|
+
|
|
16
|
+
validate_serializable!(method_name, args) if Sidekiqable.configuration.validate_arguments
|
|
17
|
+
|
|
18
|
+
worker_class = Sidekiqable::Worker
|
|
19
|
+
worker = apply_config(worker_class)
|
|
20
|
+
|
|
21
|
+
callable = "#{@target_class.name}.#{method_name}"
|
|
22
|
+
payload = [callable, *args]
|
|
23
|
+
|
|
24
|
+
worker.send(@mode, *payload)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def respond_to_missing?(method_name, include_private = false)
|
|
28
|
+
@target_class.respond_to?(method_name, include_private)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def validate_serializable!(method_name, args)
|
|
34
|
+
callable = "#{@target_class.name}.#{method_name}"
|
|
35
|
+
Sidekiq.dump_json([callable, *args])
|
|
36
|
+
rescue Sidekiq::Error, JSON::GeneratorError => e
|
|
37
|
+
raise Sidekiqable::Error,
|
|
38
|
+
"Arguments for #{@target_class}##{method_name} are not Sidekiq-serializable: #{e.message}"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def apply_config(worker_class)
|
|
42
|
+
global_options = Sidekiqable.configuration.sidekiq_options
|
|
43
|
+
class_options = @target_class.respond_to?(:sidekiqable_options) ? @target_class.sidekiqable_options : {}
|
|
44
|
+
|
|
45
|
+
options = global_options.merge(class_options)
|
|
46
|
+
|
|
47
|
+
options.empty? ? worker_class : worker_class.set(options)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sidekiqable
|
|
4
|
+
module AsyncableMethods
|
|
5
|
+
def sidekiqable_options(options = nil)
|
|
6
|
+
if options
|
|
7
|
+
@sidekiqable_options = options
|
|
8
|
+
else
|
|
9
|
+
@sidekiqable_options ||= {}
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def perform_async
|
|
14
|
+
Sidekiqable::AsyncProxy.new(self, :perform_async)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def perform_in(interval)
|
|
18
|
+
Sidekiqable::AsyncProxy.new(self, :perform_in, interval)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def perform_at(timestamp)
|
|
22
|
+
Sidekiqable::AsyncProxy.new(self, :perform_at, timestamp)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sidekiqable
|
|
4
|
+
class Configuration
|
|
5
|
+
attr_accessor :queue, :retry, :dead, :backtrace, :pool, :tags, :validate_arguments
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@queue = "default"
|
|
9
|
+
@retry = true
|
|
10
|
+
@dead = true
|
|
11
|
+
@backtrace = false
|
|
12
|
+
@pool = nil
|
|
13
|
+
@tags = nil
|
|
14
|
+
@validate_arguments = true
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def sidekiq_options
|
|
18
|
+
{}.tap do |opts|
|
|
19
|
+
opts[:queue] = queue if queue
|
|
20
|
+
opts[:retry] = @retry unless @retry.nil?
|
|
21
|
+
opts[:dead] = dead unless dead.nil?
|
|
22
|
+
opts[:backtrace] = backtrace unless backtrace.nil?
|
|
23
|
+
opts[:pool] = pool if pool
|
|
24
|
+
opts[:tags] = tags if tags
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/ordered_options"
|
|
4
|
+
require "rails/railtie"
|
|
5
|
+
|
|
6
|
+
module Sidekiqable
|
|
7
|
+
class Railtie < ::Rails::Railtie
|
|
8
|
+
config.sidekiqable = ActiveSupport::OrderedOptions.new
|
|
9
|
+
|
|
10
|
+
initializer "sidekiqable.configure" do |app|
|
|
11
|
+
options = app.config.sidekiqable
|
|
12
|
+
|
|
13
|
+
Sidekiqable.configure do |config|
|
|
14
|
+
config.queue = options.queue if options.key?(:queue)
|
|
15
|
+
config.retry = options.retry if options.key?(:retry)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sidekiqable
|
|
4
|
+
class Worker
|
|
5
|
+
include Sidekiq::Job
|
|
6
|
+
|
|
7
|
+
def perform(callable, *args)
|
|
8
|
+
class_name, method_name = callable.split(".", 2)
|
|
9
|
+
klass = constantize!(class_name)
|
|
10
|
+
klass.public_send(method_name, *args)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def constantize!(name)
|
|
16
|
+
Object.const_get(name)
|
|
17
|
+
rescue NameError => e
|
|
18
|
+
raise Sidekiqable::Error, "Failed to constantize #{name}: #{e.message}"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
data/lib/sidekiqable.rb
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "sidekiq"
|
|
4
|
+
|
|
5
|
+
require_relative "sidekiqable/version"
|
|
6
|
+
require_relative "sidekiqable/configuration"
|
|
7
|
+
require_relative "sidekiqable/worker"
|
|
8
|
+
require_relative "sidekiqable/async_proxy"
|
|
9
|
+
require_relative "sidekiqable/asyncable_methods"
|
|
10
|
+
require_relative "sidekiqable/railtie" if defined?(Rails::Railtie)
|
|
11
|
+
|
|
12
|
+
module Sidekiqable
|
|
13
|
+
class Error < StandardError; end
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
def configuration
|
|
17
|
+
@configuration ||= Configuration.new
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def configure
|
|
21
|
+
yield(configuration)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
data/sig/sidekiqable.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sidekiqable
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Sidekiqable Maintainers
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: sidekiq
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '6.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '6.0'
|
|
26
|
+
description: Sidekiqable lets you enqueue class method invocations directly by chaining
|
|
27
|
+
perform_async, perform_in, or perform_at without writing boilerplate workers.
|
|
28
|
+
email:
|
|
29
|
+
- opensource@sidekiqable.dev
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- ".claude/settings.local.json"
|
|
35
|
+
- ".rubocop.yml"
|
|
36
|
+
- CHANGELOG.md
|
|
37
|
+
- CLAUDE.md
|
|
38
|
+
- CODE_OF_CONDUCT.md
|
|
39
|
+
- LICENSE.txt
|
|
40
|
+
- README.md
|
|
41
|
+
- Rakefile
|
|
42
|
+
- lib/sidekiqable.rb
|
|
43
|
+
- lib/sidekiqable/async_proxy.rb
|
|
44
|
+
- lib/sidekiqable/asyncable_methods.rb
|
|
45
|
+
- lib/sidekiqable/configuration.rb
|
|
46
|
+
- lib/sidekiqable/railtie.rb
|
|
47
|
+
- lib/sidekiqable/version.rb
|
|
48
|
+
- lib/sidekiqable/worker.rb
|
|
49
|
+
- sig/sidekiqable.rbs
|
|
50
|
+
homepage: https://github.com/r3cha/sidekiqable
|
|
51
|
+
licenses:
|
|
52
|
+
- MIT
|
|
53
|
+
metadata:
|
|
54
|
+
homepage_uri: https://github.com/r3cha/sidekiqable
|
|
55
|
+
source_code_uri: https://github.com/r3cha/sidekiqable
|
|
56
|
+
changelog_uri: https://github.com/r3cha/sidekiqable/blob/main/CHANGELOG.md
|
|
57
|
+
rdoc_options: []
|
|
58
|
+
require_paths:
|
|
59
|
+
- lib
|
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: 3.1.0
|
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '0'
|
|
70
|
+
requirements: []
|
|
71
|
+
rubygems_version: 3.6.9
|
|
72
|
+
specification_version: 4
|
|
73
|
+
summary: Schedule class methods asynchronously with Sidekiq via a proxy pattern.
|
|
74
|
+
test_files: []
|