facio 0.1.15 → 0.1.16
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 +17 -2
- data/lib/facio/concerns/callbacks.rb +3 -2
- data/lib/facio/concerns/execution.rb +4 -23
- data/lib/facio/service.rb +1 -14
- data/lib/facio/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d340a59ed3ac5e635d5eb4f0ffd36dba7c9f48ac6e46eb2ef9f3b9d1b83c01b2
|
|
4
|
+
data.tar.gz: 056cdd8cbc0b68be37615c9077510ec506a7ac89fb18f27072ff9f8a63ad61e9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 10e473dbee725d09d3ae2a661aaaaf865941452a49131206b5828401ab0a3d62f45a2d378b7b56a796d981d4d4803c4848399b90f75136b2446beb0fa3cd6584
|
|
7
|
+
data.tar.gz: 7178d9571ad1ef46ef5ecc0bdef1574871b872476ff2392cc54da2ddc77abb92b7f7bb76770ff6eb1b6ac9dc164a91204c4f84e0a599ff8b77de34537d01925f
|
data/README.md
CHANGED
|
@@ -76,14 +76,22 @@ In this case, once the service is done, you should see the Message's text being
|
|
|
76
76
|
|
|
77
77
|
Services are subclasses of ActiveJob::Base, which basically gives you things like concurrency control and retry logic.
|
|
78
78
|
|
|
79
|
+
As a Facio::Service is just an ActiveJob, perform_later will return the service if enqueued successfully, or false if the context is invalid. If you want to inspect the context you can do so by passing a block to perform_later:
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
SimpleService.perform_later(value: "no lemon, no melon") do |job|
|
|
83
|
+
puts job.context.errors.full_messages
|
|
84
|
+
end
|
|
85
|
+
|
|
79
86
|
### Context
|
|
80
87
|
|
|
81
88
|
A context is just an PORO, which includes the ActiveModel::API, which includes validation. You can add validation to the context,
|
|
82
89
|
Facio will check the validity of the context before performing the service. Context can be created in separate files, but also inline in the service.
|
|
83
90
|
|
|
84
|
-
It has some useful
|
|
91
|
+
It has some useful extras allowing for associations (has_one/has_many), it also has a type caster for models, so that you can pass the id or the model itself into an attribute.
|
|
85
92
|
|
|
86
93
|
Using context like below (inline) will then construct a Class, with Facio::Context as a base_class. You can also use external context classes and refer to the in the inline class using the base_class.
|
|
94
|
+
|
|
87
95
|
```ruby
|
|
88
96
|
# some_context.rb
|
|
89
97
|
class SomeContext < Facio::Context
|
|
@@ -135,9 +143,16 @@ class InlineResultService < Facio::Service
|
|
|
135
143
|
result.text = context.value.to_s.reverse
|
|
136
144
|
end
|
|
137
145
|
end
|
|
138
|
-
|
|
139
146
|
```
|
|
140
147
|
|
|
148
|
+
## Notes
|
|
149
|
+
|
|
150
|
+
Facio uses two around callbacks, one for the enqueue and one for the perform.
|
|
151
|
+
The `around_enqueue` callback will check the validity of the context, and if it's invalid, it will not enqueue the job.
|
|
152
|
+
The `around_perform` callback will again check the validity of the context, and if it's invalid, it will not perform the job.
|
|
153
|
+
|
|
154
|
+
If you use callbacks yourself in your service, in some cases you might want to inspect the callback ordering.
|
|
155
|
+
|
|
141
156
|
## License
|
|
142
157
|
|
|
143
158
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -16,7 +16,6 @@ module Callbacks
|
|
|
16
16
|
# This is the most close to expected behaviour this can get.
|
|
17
17
|
raise ActiveRecord::Rollback if context.failed?
|
|
18
18
|
end
|
|
19
|
-
|
|
20
19
|
else
|
|
21
20
|
result = block.call
|
|
22
21
|
end
|
|
@@ -27,10 +26,12 @@ module Callbacks
|
|
|
27
26
|
end
|
|
28
27
|
end
|
|
29
28
|
|
|
30
|
-
|
|
29
|
+
around_enqueue do |job, block|
|
|
31
30
|
@performed = false
|
|
32
31
|
@context = self.class.context_class.new(arguments.first)
|
|
33
32
|
@result = self.class.result_class&.new
|
|
33
|
+
|
|
34
|
+
block.call if @context.valid?
|
|
34
35
|
end
|
|
35
36
|
end
|
|
36
37
|
end
|
|
@@ -4,29 +4,7 @@ module Execution
|
|
|
4
4
|
# Facio doesn't support arguments in the perform method, that's done with the context object.
|
|
5
5
|
# This is why we override perform here.
|
|
6
6
|
def facio_perform_with_arguments(*args)
|
|
7
|
-
|
|
8
|
-
@result = self.class.result_class&.new
|
|
9
|
-
@performed = false
|
|
10
|
-
|
|
11
|
-
if @context.valid?
|
|
12
|
-
result = nil
|
|
13
|
-
if transactional && defined?(ActiveRecord::Base)
|
|
14
|
-
ActiveRecord::Base.transaction(requires_new: true) do
|
|
15
|
-
# result = block.call
|
|
16
|
-
result = facio_perform_without_arguments
|
|
17
|
-
# This will only rollback the changes of the service, SILENTLY, however the context will be failed? already.
|
|
18
|
-
# This is the most close to expected behaviour this can get.
|
|
19
|
-
raise ActiveRecord::Rollback if context.failed?
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
else
|
|
23
|
-
result = facio_perform_without_arguments
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
@performed = true
|
|
27
|
-
# Purely as a convenience, but also to enforce a standard
|
|
28
|
-
context.result ||= result if @result.nil?
|
|
29
|
-
end
|
|
7
|
+
facio_perform_without_arguments
|
|
30
8
|
end
|
|
31
9
|
|
|
32
10
|
included do
|
|
@@ -35,7 +13,10 @@ module Execution
|
|
|
35
13
|
if method_name == :perform
|
|
36
14
|
# Avoid reentrance
|
|
37
15
|
unless method_defined?(:facio_perform_without_arguments)
|
|
16
|
+
# We copy the user defined perform method on the job and set it aside
|
|
38
17
|
alias_method :facio_perform_without_arguments, :perform
|
|
18
|
+
# We take or method above and set that as the new perform method
|
|
19
|
+
# This way we can pass the arguments to the context object
|
|
39
20
|
alias_method :perform, :facio_perform_with_arguments # This invokes method_added(:perform)
|
|
40
21
|
end
|
|
41
22
|
end
|
data/lib/facio/service.rb
CHANGED
|
@@ -11,6 +11,7 @@ module Facio
|
|
|
11
11
|
include ServiceResult
|
|
12
12
|
include Transactional
|
|
13
13
|
include Translations
|
|
14
|
+
include Callbacks
|
|
14
15
|
include Execution
|
|
15
16
|
|
|
16
17
|
class << self
|
|
@@ -20,20 +21,6 @@ module Facio
|
|
|
20
21
|
job
|
|
21
22
|
end
|
|
22
23
|
|
|
23
|
-
def perform_later(...)
|
|
24
|
-
job = job_or_instantiate(...)
|
|
25
|
-
|
|
26
|
-
enqueue_result = job.enqueue
|
|
27
|
-
|
|
28
|
-
job.instance_variable_set(:@performed, false)
|
|
29
|
-
job.instance_variable_set(:@context, context_class.new(job.arguments.first))
|
|
30
|
-
job.instance_variable_set(:@result, result_class&.new)
|
|
31
|
-
|
|
32
|
-
yield job if block_given?
|
|
33
|
-
|
|
34
|
-
enqueue_result
|
|
35
|
-
end
|
|
36
|
-
|
|
37
24
|
alias_method :perform_now, :perform
|
|
38
25
|
end
|
|
39
26
|
|
data/lib/facio/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: facio
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.16
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tom de Grunt
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-01-
|
|
11
|
+
date: 2026-01-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: actionpack
|