job_contracts 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8ccfdc1005a713277b03897aaf58f87db30e04d9697e92f462c8712eeb4faf8d
4
+ data.tar.gz: 3f3a4d679a61701c986cd858ca259b1208a2f3d82bd94681b96e884252fb4403
5
+ SHA512:
6
+ metadata.gz: 41d343fa07382f35997fe9a6a75410fcfaf8f599b42cf9421349ae4084ed49c5b5467579b83fd326992c27bf4d3ec64bbb38882ee370e297d8688dcd571131c4
7
+ data.tar.gz: 95336d3dd5464cbb7731ce25c946f5502a7cbc2158a7ea73ef727726b9226e7d660a735351bc5260f783cf6e3ab1a6bc93adeac7c15464a81288863cd70896f9
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Hopsoft
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # Job Contracts
2
+
3
+ ## Enforceable contracts for background jobs
4
+
5
+ ## Todos
6
+
7
+ - [ ] Add documentation
8
+ - [ ] Add automated tests
9
+
10
+ ## License
11
+
12
+ 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,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,65 @@
1
+ module JobContracts
2
+ # Universal mixin for jobs/workers
3
+ module Contractable
4
+ extend ActiveSupport::Concern
5
+
6
+ module Prepends
7
+ extend ActiveSupport::Concern
8
+
9
+ def perform(*args)
10
+ should_perform = true
11
+ self.class.contracts_to_enforce_before_perform.each do |contract|
12
+ contract.enforce! self
13
+ should_perform = false if contract.breached? && contract.halt?
14
+ end
15
+ super if should_perform
16
+ self.class.contracts_to_enforce_after_perform.each do |contract|
17
+ contract.enforce! self
18
+ end
19
+ end
20
+ end
21
+
22
+ module ClassMethods
23
+ attr_reader :after_contract_breach_callback
24
+
25
+ def contracts_to_enforce_before_perform
26
+ @contracts_to_enforce_before_perform ||= Set.new
27
+ end
28
+
29
+ def contracts_to_enforce_after_perform
30
+ @contracts_to_enforce_after_perform ||= Set.new
31
+ end
32
+
33
+ def after_contract_breach(value = nil, &block)
34
+ @after_contract_breach_callback = value || block
35
+ end
36
+
37
+ def add_contract(contract)
38
+ if contract.class.const_defined?(:ContractableIncludes)
39
+ include contract.class.const_get(:ContractableIncludes)
40
+ end
41
+
42
+ if contract.class.const_defined?(:ContractablePrepends)
43
+ prepend contract.class.const_get(:ContractablePrepends)
44
+ end
45
+
46
+ prepend JobContracts::Contractable::Prepends
47
+
48
+ if contract.trigger == :before
49
+ contracts_to_enforce_before_perform << contract
50
+ else
51
+ contracts_to_enforce_after_perform << contract
52
+ end
53
+ end
54
+ end
55
+
56
+ def after_contract_breach(contract)
57
+ method = self.class.after_contract_breach_callback
58
+ case method
59
+ when Proc then method.call(contract)
60
+ when String, Symbol then send(method, contract)
61
+ else raise NotImplementedError
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,46 @@
1
+ require_relative "contractable"
2
+
3
+ module JobContracts
4
+ # Sidekiq mixin for jobs/workers
5
+ module SidekiqContractable
6
+ extend ActiveSupport::Concern
7
+ include Contractable
8
+
9
+ class MetadataNotFoundError < StandardError; end
10
+
11
+ def metadata
12
+ hit = nil
13
+ begin
14
+ attempts ||= 1
15
+ hit = Sidekiq::WorkSet.new.find do |_pid, _tid, work|
16
+ work.dig("payload", "jid") == jid
17
+ end
18
+ raise MetadataNotFoundError if hit.blank?
19
+ rescue MetadataNotFoundError
20
+ # The WorkSet only updates every 5 seconds
21
+ # SEE: https://github.com/mperham/sidekiq/wiki/API#workers
22
+ # Re-attempt up to 10 times with a simple backoff strategy (up to 5.5 seconds)
23
+ # TODO: Is there a faster and more reliable way to fetch the job's metadata after perform has begun?
24
+ # May need to query Redis directly if the data is still in there at this point
25
+ attempts += 1
26
+ if attempts <= 10
27
+ sleep 0.1 * attempts
28
+ retry
29
+ end
30
+ end
31
+
32
+ hit&.last || {}
33
+ end
34
+
35
+ # Matches the ActiveJob API
36
+ def queue_name
37
+ metadata["queue"]
38
+ end
39
+
40
+ # Matches the ActiveJob API
41
+ def enqueued_at
42
+ seconds = metadata.dig("payload", "enqueued_at")
43
+ (seconds ? Time.at(seconds) : nil)&.iso8601.to_s
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,49 @@
1
+ require "observer"
2
+
3
+ module JobContracts
4
+ class Contract
5
+ include Observable
6
+
7
+ attr_reader :trigger
8
+
9
+ def initialize(trigger: :after, halt: false, **kwargs)
10
+ @trigger = trigger.to_sym
11
+ @halt = halt
12
+ expect.merge! kwargs
13
+ end
14
+
15
+ def expect
16
+ @expect ||= HashWithIndifferentAccess.new
17
+ end
18
+
19
+ def actual
20
+ @actual ||= HashWithIndifferentAccess.new
21
+ end
22
+
23
+ # Method to be implemented by subclasses
24
+ # NOTE: subclasses should update `actual`, set `satisfied`, and call `super`
25
+ def enforce!(contractable)
26
+ add_observer contractable, :after_contract_breach
27
+ changed if breached?
28
+ notify_observers self
29
+ ensure
30
+ delete_observer contractable
31
+ end
32
+
33
+ def satisfied?
34
+ !!satisfied
35
+ end
36
+
37
+ def breached?
38
+ !satisfied?
39
+ end
40
+
41
+ def halt?
42
+ !!@halt
43
+ end
44
+
45
+ protected
46
+
47
+ attr_accessor :satisfied
48
+ end
49
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "contract"
2
+
3
+ module JobContracts
4
+ class DurationContract < Contract
5
+ def initialize(duration:)
6
+ super
7
+ end
8
+
9
+ def enforce!(contractable)
10
+ actual[:duration] = (Time.current - Time.parse(contractable.enqueued_at)).seconds
11
+ self.satisfied = actual[:duration] < expect[:duration].seconds
12
+ super
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ require_relative "contract"
2
+
3
+ module JobContracts
4
+ class QueueNameContract < Contract
5
+ def initialize(queue_name:)
6
+ super trigger: :before, halt: true, queue_name: queue_name
7
+ end
8
+
9
+ def enforce!(contractable)
10
+ queue_name = contractable.queue_name
11
+ actual[:queue_name] = queue_name
12
+ self.satisfied = queue_name.to_s == expect[:queue_name].to_s
13
+ super
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "contract"
2
+
3
+ module JobContracts
4
+ class ReadOnlyContract < Contract
5
+ module ContractablePrepends
6
+ extend ActiveSupport::Concern
7
+
8
+ def perform(*args)
9
+ ActiveRecord::Base.while_preventing_writes do
10
+ super
11
+ end
12
+ rescue ActiveRecord::ReadOnlyError => error
13
+ @read_only_error = error
14
+ end
15
+ end
16
+
17
+ module ContractableIncludes
18
+ extend ActiveSupport::Concern
19
+ included do
20
+ attr_reader :read_only_error
21
+ end
22
+ end
23
+
24
+ # def initialize
25
+ # super trigger: :before
26
+ # end
27
+
28
+ def enforce!(contractable)
29
+ if contractable.read_only_error.present?
30
+ actual[:error] = contractable.read_only_error.message
31
+ self.satisfied = false
32
+ end
33
+ super
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,4 @@
1
+ module JobContracts
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module JobContracts
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ require "job_contracts/version"
2
+ require "job_contracts/railtie"
3
+
4
+ root = Pathname.new(File.dirname(File.absolute_path(__FILE__)))
5
+ root.glob("**/*.rb").each { |file| require file }
6
+
7
+ module JobContracts
8
+ # Your code goes here...
9
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: job_contracts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Hopkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: sidekiq
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 6.4.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 6.4.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: standard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.10.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.10.0
55
+ description: Enforceable contracts for background jobs
56
+ email:
57
+ - natehop@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/job_contracts.rb
66
+ - lib/job_contracts/concerns/contractable.rb
67
+ - lib/job_contracts/concerns/sidekiq_contractable.rb
68
+ - lib/job_contracts/contracts/contract.rb
69
+ - lib/job_contracts/contracts/duration_contract.rb
70
+ - lib/job_contracts/contracts/queue_name_contract.rb
71
+ - lib/job_contracts/contracts/read_only_contract.rb
72
+ - lib/job_contracts/railtie.rb
73
+ - lib/job_contracts/version.rb
74
+ homepage: https://github.com/hopsoft/job_contracts
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://github.com/hopsoft/job_contracts
79
+ source_code_uri: https://github.com/hopsoft/job_contracts
80
+ changelog_uri: https://github.com/hopsoft/job_contracts/releases
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.3.7
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Enforceable contracts for background jobs
100
+ test_files: []