chaotic_job 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 970f6e59807e48fc6bc71581fe2a12cd0c890429879b079dc8205f73aa74f2d5
4
+ data.tar.gz: 3378067f6be89a0f2a715ea60a18ada92af2d430d555d96756c6d3024802b5be
5
+ SHA512:
6
+ metadata.gz: 37a614599cc3b971ec6fb93b19d145be3cce82198bd631c945d996c1f1b30f0d0ac0f0bf643649548d780ffeecbb1a237fb5a5051043c394e087c7cbd04f94f8
7
+ data.tar.gz: 042ae03b7c80a1fc4570fb0617645f1d3801e331fa73dbd80b8a6bc11529136db4f99d8313bc2f7efb7f5808c66c792a419d8197d81734622cc174bf83bfe76c
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-11-06
4
+
5
+ - Initial release
@@ -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) 2024 Stephen Margheim
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,46 @@
1
+ # 👾 Chaotic Job
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/chaotic_job.svg)](https://rubygems.org/gems/chaotic_job)
4
+ [![Gem Downloads](https://img.shields.io/gem/dt/chaotic_job)](https://rubygems.org/gems/chaotic_job)
5
+ ![Tests](https://github.com/fractaledmind/chaotic_job/actions/workflows/main.yml/badge.svg)
6
+ ![Coverage](https://img.shields.io/badge/code%20coverage-98%25-success)
7
+ [![Sponsors](https://img.shields.io/github/sponsors/fractaledmind?color=eb4aaa&logo=GitHub%20Sponsors)](https://github.com/sponsors/fractaledmind)
8
+ [![Twitter Follow](https://img.shields.io/twitter/url?label=%40fractaledmind&style=social&url=https%3A%2F%2Ftwitter.com%2Ffractaledmind)](https://twitter.com/fractaledmind)
9
+
10
+ ## Installation
11
+
12
+ TODO: Replace `chaotic_job` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
13
+
14
+ Install the gem and add to the application's Gemfile by executing:
15
+
16
+ ```bash
17
+ bundle add chaotic_job
18
+ ```
19
+
20
+ If bundler is not being used to manage dependencies, install the gem by executing:
21
+
22
+ ```bash
23
+ gem install chaotic_job
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ TODO: Write usage instructions here
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/chaotic_job. 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/[USERNAME]/chaotic_job/blob/main/CODE_OF_CONDUCT.md).
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
43
+
44
+ ## Code of Conduct
45
+
46
+ Everyone interacting in the ChaoticJob project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/chaotic_job/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Glitch.new.before("job_crucible.rb:10") { do_anything }
4
+ # Glitch.new.after("job_crucible.rb:11") { do_anything }
5
+ # Glitch.new.inject! { execute code to glitch }
6
+
7
+ module ChaoticJob
8
+ class Glitch
9
+ def initialize
10
+ @breakpoints = {}
11
+ @file_contents = {}
12
+ end
13
+
14
+ def before(path_with_line, &block)
15
+ set_breakpoint(path_with_line, :before, &block)
16
+ end
17
+
18
+ def after(path_with_line, &block)
19
+ set_breakpoint(path_with_line, :after, &block)
20
+ end
21
+
22
+ def inject!
23
+ prev_key = nil
24
+ trace = TracePoint.new(:line) do |tp|
25
+ key = "#{tp.path}:#{tp.lineno}"
26
+ # content = @file_contents[tp.path]
27
+ # line = content[tp.lineno - 1]
28
+ # next unless line.match? key
29
+
30
+ begin
31
+ execute_block(@breakpoints[prev_key][:after]) if prev_key && @breakpoints.key?(prev_key)
32
+
33
+ execute_block(@breakpoints[key][:before]) if @breakpoints.key?(key)
34
+ ensure
35
+ prev_key = key
36
+ end
37
+ end
38
+
39
+ trace.enable
40
+ yield if block_given?
41
+ ensure
42
+ trace.disable
43
+ execute_block(@breakpoints[prev_key][:after]) if prev_key && @breakpoints.key?(prev_key)
44
+ end
45
+
46
+ def all_executed?
47
+ @breakpoints.all? do |_location, handlers|
48
+ handlers.all? { |_position, handler| handler[:executed] }
49
+ end
50
+ end
51
+
52
+ # def inspect
53
+ # @breakpoints.flat_map do |location, configs|
54
+ # configs.keys.map { |position| "#{position}-#{location}" }
55
+ # end.join("|>")
56
+ # end
57
+
58
+ private
59
+
60
+ def set_breakpoint(path_with_line, position, &block)
61
+ @breakpoints[path_with_line] ||= {}
62
+ # contents = File.read(file_path).split("\n") unless @file_contents.key?(path_with_line)
63
+ # @file_contents << contents
64
+ @breakpoints[path_with_line][position] = { block: block, executed: false }
65
+ end
66
+
67
+ def execute_block(handler)
68
+ return unless handler
69
+ return if handler[:executed]
70
+
71
+ handler[:executed] = true
72
+ handler[:block].call
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Journal.log
4
+ # Journal.log(thing, scope: :special)
5
+ # Journal.total
6
+ # Journal.total(scope: :special)
7
+ # Journal.all
8
+
9
+ module ChaoticJob
10
+ module Journal
11
+ extend self
12
+
13
+ def reset!
14
+ @logs = {}
15
+ end
16
+
17
+ def log(item = 1, scope: :default)
18
+ @logs ||= {}
19
+ @logs[scope] ||= []
20
+ @logs[scope] << item
21
+ end
22
+
23
+ def total(scope: :default)
24
+ @logs[scope]&.size || 0
25
+ end
26
+
27
+ def all(scope: :default)
28
+ @logs[scope]
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Performer.new(Job1).perform_all
4
+ # Performer.new(Job1).perform_all_within(time)
5
+ # Performer.new(Job1).perform_all_after(time)
6
+
7
+ module ChaoticJob
8
+ class Performer
9
+ include ActiveJob::TestHelper
10
+
11
+ def initialize(job, retry_window: 4)
12
+ @job = job
13
+ @retry_window = retry_window
14
+ end
15
+
16
+ def perform_all
17
+ @job.enqueue
18
+ enqueued_jobs_with.sort_by(&:scheduled_at, nil: :first).each do |job|
19
+ perform_job(job)
20
+ end
21
+ end
22
+
23
+ def perform_all_after(2.seconds.from_now)
24
+ end
25
+
26
+ def perform_all_within(time)
27
+ end
28
+
29
+ private
30
+
31
+ def perform_enqueued_jobs_only_with_retries
32
+ retry_window = Time.now + @retry_window
33
+ flush_enqueued_jobs(at: retry_window) until enqueued_jobs_with(at: retry_window).empty?
34
+ end
35
+
36
+ def perform_any_enqueued_jobs_including_future_scheduled_ones
37
+ flush_enqueued_jobs until enqueued_jobs_with.empty?
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Scenario.new(job).run { |scenario| ... }
4
+ # Scenario.new(job).all_glitched?
5
+
6
+ module ChaoticJob
7
+ class Scenario
8
+ attr_reader :events
9
+
10
+ def initialize(job, glitches:, raise: RetryableError, capture: /active_job/)
11
+ @job = job
12
+ @glitches = glitches
13
+ @raise = raise
14
+ @capture = capture
15
+ @glitch = nil
16
+ @events = []
17
+ end
18
+
19
+ def run
20
+ @job.class.retry_on RetryableError, attempts: 10, wait: 1, jitter: 0
21
+
22
+ ActiveSupport::Notifications.subscribed(->(event) { @events << event.dup }, @capture) do
23
+ glitch.inject! do
24
+ block_given? ? yield : Performance.rehearse(@job)
25
+ end
26
+ end
27
+
28
+ # TODO: assert that all glitches ran
29
+ end
30
+
31
+ def to_s
32
+ @glitches.map { |position, location| "#{position}-#{location}" }.join("|>")
33
+ end
34
+
35
+ def all_glitched?
36
+ @glitch.all_executed?
37
+ end
38
+
39
+ private
40
+
41
+ def glitch
42
+ @glitch ||= Glitch.new.tap do |glitch|
43
+ @glitches.each do |position, location, _description|
44
+ glitch.public_send(position, location) { raise @raise }
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Simulation.new(job).run { |scenario| ... }
4
+ # Simulation.new(job).permutations
5
+ # Simulation.new(job).variants
6
+ # Simulation.new(job).scenarios
7
+ module ChaoticJob
8
+ class Simulation
9
+ def initialize(job, test: nil, variations: 100, seed: nil, depth: 3)
10
+ @template = job
11
+ @test = test
12
+ @variations = variations
13
+ @seed = seed || Random.new_seed
14
+ @random = Random.new(@seed)
15
+ @depth = depth
16
+ end
17
+
18
+ def run(&callback)
19
+ @template.class.retry_on RetryableError, attempts: @depth + 2, wait: 1, jitter: 0
20
+
21
+ debug "Running #{variants.size} simulations of the total #{permutations.size} possibilities..."
22
+
23
+ scenarios.map do |scenario|
24
+ run_scenario(scenario, &callback)
25
+ end
26
+ end
27
+
28
+ def permutations
29
+ callstack = capture_callstack.to_a
30
+ error_locations = callstack.each_cons(2).flat_map do |left, right|
31
+ lpath, lno = left
32
+ rpath, rno = right
33
+ key = "#{lpath}:#{lno}"
34
+ # inject an error before and after each non-adjacent line
35
+ if lpath == rpath && rno == lno + 1
36
+ [[:before, key]]
37
+ else
38
+ [[:before, key], [:after, key]]
39
+ end
40
+ end
41
+ final_key = callstack.last.join(":")
42
+ error_locations.push [:before, final_key], [:after, final_key]
43
+ error_locations.permutation(@depth)
44
+ end
45
+
46
+ def variants
47
+ return permutations if @variations.nil?
48
+
49
+ permutations.to_a.sample(@variations, random: @random)
50
+ end
51
+
52
+ def scenarios
53
+ variants.map do |glitches|
54
+ job = clone_job_template
55
+ scenario = Scenario.new(job, glitches: glitches)
56
+ job.job_id = scenario.to_s
57
+ scenario
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def capture_callstack
64
+ return @callstack if defined?(@callstack)
65
+
66
+ @callstack = Set.new
67
+ job_class = @template.class
68
+ job_file_path = job_class.instance_method(:perform).source_location&.first
69
+
70
+ trace = TracePoint.new(:line) do |tp|
71
+ next if tp.defined_class == self.class
72
+ next unless tp.path == job_file_path ||
73
+ tp.defined_class == job_class
74
+
75
+ @callstack << [tp.path, tp.lineno]
76
+ end
77
+
78
+ trace.enable { @template.dup.perform_now }
79
+ @template.class.queue_adapter.enqueued_jobs = []
80
+ @callstack
81
+ end
82
+
83
+ def run_scenario(scenario, &callback)
84
+ debug "Running simulation with scenario: #{scenario}"
85
+ @test.before_setup
86
+ scenario.enact!
87
+ @test.after_teardown
88
+ callback.call(scenario)
89
+ end
90
+
91
+ def clone_job_template
92
+ serialized_template = @template.serialize
93
+ job = ActiveJob::Base.deserialize(serialized_template)
94
+ job.exception_executions = {}
95
+ job
96
+ end
97
+
98
+ def debug(...)
99
+ @template.logger.debug(...)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChaoticJob
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "chaotic_job/version"
4
+
5
+ module ChaoticJob
6
+ class RetryableError < StandardError; end
7
+
8
+ module Helpers
9
+ def run_simulation(job, ..., &block)
10
+ Simulation.new(job, test: self, ...).run(&block)
11
+ end
12
+
13
+ def run_scenario(job)
14
+ Scenario.new(job).run
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ module ChaoticJob
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chaotic_job
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Margheim
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-11-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - stephen.margheim@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".standard.yml"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/chaotic_job.rb
27
+ - lib/chaotic_job/glitch.rb
28
+ - lib/chaotic_job/journal.rb
29
+ - lib/chaotic_job/performer.rb
30
+ - lib/chaotic_job/scenario.rb
31
+ - lib/chaotic_job/simulation.rb
32
+ - lib/chaotic_job/version.rb
33
+ - sig/chaotic_job.rbs
34
+ homepage: https://github.com/fractaledmind/chaotic_job
35
+ licenses:
36
+ - MIT
37
+ metadata:
38
+ homepage_uri: https://github.com/fractaledmind/chaotic_job
39
+ source_code_uri: https://github.com/fractaledmind/chaotic_job
40
+ changelog_uri: https://github.com/fractaledmind/chaotic_job/CHANGELOG.md
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 3.0.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.5.21
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Test ActiveJobs for reliability and resilience.
60
+ test_files: []