belated 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.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/main.yml +16 -0
  3. data/.gitignore +14 -0
  4. data/.rake_tasks~ +10 -0
  5. data/.rspec +4 -0
  6. data/.rubocop.yml +42 -0
  7. data/CHANGELOG.md +25 -0
  8. data/CODE_OF_CONDUCT.md +84 -0
  9. data/Gemfile +15 -0
  10. data/Gemfile.lock +191 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +135 -0
  13. data/Rakefile +12 -0
  14. data/belated.gemspec +40 -0
  15. data/bin/belated +38 -0
  16. data/bin/bundle +3 -0
  17. data/bin/console +15 -0
  18. data/bin/setup +8 -0
  19. data/dummy/Rakefile +8 -0
  20. data/dummy/app/assets/config/manifest.js +3 -0
  21. data/dummy/app/assets/images/.keep +0 -0
  22. data/dummy/app/assets/stylesheets/application.css +15 -0
  23. data/dummy/app/channels/application_cable/channel.rb +6 -0
  24. data/dummy/app/channels/application_cable/connection.rb +6 -0
  25. data/dummy/app/controllers/application_controller.rb +5 -0
  26. data/dummy/app/controllers/concerns/.keep +0 -0
  27. data/dummy/app/helpers/application_helper.rb +4 -0
  28. data/dummy/app/javascript/packs/application.js +15 -0
  29. data/dummy/app/jobs/application_job.rb +9 -0
  30. data/dummy/app/mailers/application_mailer.rb +6 -0
  31. data/dummy/app/models/application_record.rb +5 -0
  32. data/dummy/app/models/concerns/.keep +0 -0
  33. data/dummy/app/models/user.rb +2 -0
  34. data/dummy/app/views/application/index.html.erb +4 -0
  35. data/dummy/app/views/layouts/application.html.erb +15 -0
  36. data/dummy/app/views/layouts/mailer.html.erb +13 -0
  37. data/dummy/app/views/layouts/mailer.text.erb +1 -0
  38. data/dummy/bin/rails +6 -0
  39. data/dummy/bin/rake +6 -0
  40. data/dummy/bin/setup +35 -0
  41. data/dummy/config.ru +8 -0
  42. data/dummy/config/application.rb +23 -0
  43. data/dummy/config/boot.rb +7 -0
  44. data/dummy/config/cable.yml +10 -0
  45. data/dummy/config/credentials.yml.enc +1 -0
  46. data/dummy/config/database.yml +25 -0
  47. data/dummy/config/environment.rb +7 -0
  48. data/dummy/config/environments/development.rb +78 -0
  49. data/dummy/config/environments/production.rb +122 -0
  50. data/dummy/config/environments/test.rb +51 -0
  51. data/dummy/config/initializers/application_controller_renderer.rb +9 -0
  52. data/dummy/config/initializers/backtrace_silencers.rb +10 -0
  53. data/dummy/config/initializers/content_security_policy.rb +29 -0
  54. data/dummy/config/initializers/cookies_serializer.rb +7 -0
  55. data/dummy/config/initializers/filter_parameter_logging.rb +8 -0
  56. data/dummy/config/initializers/inflections.rb +17 -0
  57. data/dummy/config/initializers/mime_types.rb +5 -0
  58. data/dummy/config/initializers/permissions_policy.rb +12 -0
  59. data/dummy/config/initializers/wrap_parameters.rb +16 -0
  60. data/dummy/config/locales/en.yml +33 -0
  61. data/dummy/config/master.key +1 -0
  62. data/dummy/config/puma.rb +45 -0
  63. data/dummy/config/routes.rb +5 -0
  64. data/dummy/config/storage.yml +34 -0
  65. data/dummy/db/migrate/20210613015406_create_users.rb +10 -0
  66. data/dummy/db/production.sqlite3 +0 -0
  67. data/dummy/db/schema.rb +20 -0
  68. data/dummy/db/test.sqlite3 +0 -0
  69. data/dummy/lib/assets/.keep +0 -0
  70. data/dummy/log/.keep +0 -0
  71. data/dummy/log/development.log +0 -0
  72. data/dummy/log/production.log +3 -0
  73. data/dummy/log/test.log +595 -0
  74. data/dummy/public/404.html +67 -0
  75. data/dummy/public/422.html +67 -0
  76. data/dummy/public/500.html +66 -0
  77. data/dummy/public/apple-touch-icon-precomposed.png +0 -0
  78. data/dummy/public/apple-touch-icon.png +0 -0
  79. data/dummy/public/favicon.ico +0 -0
  80. data/dummy/storage/.keep +0 -0
  81. data/dummy/tmp/.keep +0 -0
  82. data/dummy/tmp/development_secret.txt +1 -0
  83. data/dummy/tmp/pids/.keep +0 -0
  84. data/dummy/tmp/storage/.keep +0 -0
  85. data/hard_worker_dump +1 -0
  86. data/lib/belated.rb +147 -0
  87. data/lib/belated/client.rb +16 -0
  88. data/lib/belated/rails.rb +5 -0
  89. data/lib/belated/version.rb +5 -0
  90. data/lib/belated/worker.rb +27 -0
  91. metadata +179 -0
data/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # Hardworker
2
+
3
+ [![CodeFactor](https://www.codefactor.io/repository/github/sampokuokkanen/belated/badge)](https://www.codefactor.io/repository/github/sampokuokkanen/belated) [![Gem Version](https://badge.fury.io/rb/belated.svg)](https://badge.fury.io/rb/belated)
4
+
5
+ This is Belated, a new Ruby backend job library! It supports running procs and classes in the background.
6
+ ~~Also, you lose all jobs if you restart the process.~~ It now uses YAML to load the queue into a file, which it then calls at startup to find the previous jobs.
7
+ Note that Belated used to be called HardWorker. That name was already in use in Sidekiq documentation and a bit too generic anyway.
8
+
9
+ It uses dRuby to do the communication! Which is absolute great. No need for Redis or PostgreSQL, just Ruby standard libraries.
10
+
11
+ TODO LIST:
12
+
13
+ - ~~Marshal the job queue into a file so you don't lose all progress~~
14
+ (Ended up using YAML)
15
+ - ~~Support Rails~~ (Supported!)
16
+ - ~~Parse options from command line, eg. `--workers 10`~~(Done!)
17
+ - Maybe support ActiveJob?
18
+ - Have a web UI
19
+ - Do some performance testing
20
+ - Add a section telling people to use Sidekiq if they can
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem 'belated'
28
+ ```
29
+
30
+ And then execute:
31
+
32
+ $ bundle install
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install belated
37
+
38
+ ## Usage
39
+
40
+ Start up Belated!
41
+
42
+ $ belated
43
+
44
+ Then, in another program, connect to Belated and give it a job to do.
45
+ Sample below:
46
+
47
+ ```ruby
48
+ class DummyWorker
49
+ attr_accessor :queue
50
+
51
+ def initialize
52
+ server_uri = Belated::URI
53
+ self.queue = DRbObject.new_with_uri(server_uri)
54
+ end
55
+ end
56
+
57
+ class DumDum
58
+ # classes need to have a perform method
59
+ def perform
60
+ 5 / 4
61
+ end
62
+ end
63
+
64
+ # Need to start dRuby on the client side
65
+ DRb.start_service
66
+ dummy = DummyWorker.new
67
+ dummy.queue.push(proc { 2 / 1 })
68
+ dummy.queue.push(DumDum.new)
69
+ ```
70
+
71
+ Hardworker runs on localhost, port 8788. Should probably make that a value you can change...
72
+
73
+ ## Rails
74
+
75
+ Usage with Rails:
76
+ First, start up Belated.
77
+ Then,
78
+
79
+ ```ruby
80
+ $client = Belated::Client.new
81
+ ```
82
+
83
+ and you can use the client!
84
+ Call
85
+
86
+ ```ruby
87
+ $client.perform_belated(job)
88
+ ```
89
+
90
+ If you want to pass a job to Belated.
91
+
92
+ # Settings
93
+
94
+ Configuring Belated:
95
+
96
+ ```ruby
97
+ Belated.configure do |config|
98
+ config.rails = false # default is true
99
+ config.rails_path = # './dummy' default is '.'
100
+ config.connect = false # Connect to dRuby, default is true, useful for testing only
101
+ config.workers = 2 # default is 1
102
+ end
103
+ ```
104
+
105
+ From command line:
106
+
107
+ $ bundle exec belated --rails=true
108
+
109
+ Use Rails or not.
110
+
111
+ $ bundle exec belated --rails_path=/my_rails_project
112
+
113
+ Path to Rails project.
114
+
115
+ $ bundle exec belated --workers=10
116
+
117
+ Number of workers.
118
+
119
+ ## Development
120
+
121
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
122
+
123
+ 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).
124
+
125
+ ## Contributing
126
+
127
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sampokuokkanen/belated. 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]/hardworker/blob/master/CODE_OF_CONDUCT.md).
128
+
129
+ ## License
130
+
131
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
132
+
133
+ ## Code of Conduct
134
+
135
+ Everyone interacting in the Belated project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/hardworker/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/belated.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/belated/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'belated'
7
+ spec.version = Belated::VERSION
8
+ spec.authors = ['Sampo Kuokkanen']
9
+ spec.email = ['sampo.kuokkanen@gmail.com']
10
+
11
+ spec.summary = 'Run background jobs with Belated and dRuby!'
12
+ spec.description = %(
13
+ A simple Ruby backend job framework without Redis or PostgreSQL dependency.
14
+ Used to be named HardWorker.
15
+ ).gsub(/\s+/, ' ').strip
16
+ spec.homepage = 'https://github.com/sampokuokkanen/belated'
17
+ spec.license = 'MIT'
18
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
19
+
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = spec.homepage
22
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
28
+ end
29
+ spec.bindir = 'bin'
30
+ spec.executables = ['belated']
31
+ spec.require_paths = ['lib']
32
+
33
+ # Uncomment to register a new dependency of your gem
34
+ spec.add_dependency 'drb'
35
+ spec.add_dependency 'dry-configurable'
36
+ spec.add_development_dependency 'byebug'
37
+
38
+ # For more information and examples about making a new gem, checkout our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ end
data/bin/belated ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $TESTING = false
5
+ $LOAD_PATH.unshift File.expand_path("#{File.dirname(__FILE__)}/../lib")
6
+ require 'belated'
7
+ require 'optparse'
8
+ require 'byebug'
9
+
10
+ OptionParser.new { |opts|
11
+ opts.banner = 'Usage: belated [options]'
12
+
13
+ opts.separator ''
14
+ opts.separator 'Options:'
15
+
16
+ opts.on('-r=RAILS', '--rails=RAILS', 'Load Rails or not, default true') do |rails|
17
+ Belated.config.rails = rails == 'true'
18
+ end
19
+
20
+ opts.on('-r_path=PATH', '--rails_path=PATH', 'Path to Rails') do |path|
21
+ Belated.config.rails_path = path
22
+ end
23
+
24
+ opts.on('-w=WORKERS', '--workers=WORKERS', 'Number of workers, default 1') do |workers|
25
+ Belated.config.workers = workers.to_i
26
+ end
27
+
28
+ opts.on('-e=ENV', '--env=ENV', 'Environment, default development') do |env|
29
+ Belated.config.env = env
30
+ end
31
+
32
+ opts.on('-c=CONNECT', '--connect=CONNECT', 'Start dRuby connection, default true') do |connect|
33
+ Belated.config.connect = connect == 'true'
34
+ end
35
+ }.parse!
36
+
37
+ instance = Belated.instance
38
+ instance.start
data/bin/bundle ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
3
+ load Gem.bin_path('bundler', 'bundle')
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'belated'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/dummy/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
4
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
5
+
6
+ require_relative 'config/application'
7
+
8
+ Rails.application.load_tasks
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../stylesheets .css
3
+ //= link active_translate_self_manifest.js
File without changes
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApplicationCable
4
+ class Channel < ActionCable::Channel::Base
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApplicationCable
4
+ class Connection < ActionCable::Connection::Base
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ApplicationController < ActionController::Base
4
+ def index; end
5
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApplicationHelper
4
+ end
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require rails-ujs
14
+ //= require activestorage
15
+ //= require_tree .
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ApplicationJob < ActiveJob::Base
4
+ # Automatically retry jobs that encountered a deadlock
5
+ # retry_on ActiveRecord::Deadlocked
6
+
7
+ # Most jobs are safe to ignore if the underlying records are no longer available
8
+ # discard_on ActiveJob::DeserializationError
9
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ApplicationMailer < ActionMailer::Base
4
+ default from: 'from@example.com'
5
+ layout 'mailer'
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ApplicationRecord < ActiveRecord::Base
4
+ self.abstract_class = true
5
+ end
File without changes
@@ -0,0 +1,2 @@
1
+ class User < ApplicationRecord
2
+ end
@@ -0,0 +1,4 @@
1
+ Hello!
2
+ <h1>
3
+ <%= 'Hello'.translate_to_fi %>
4
+ </h1>
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <%= csrf_meta_tags %>
7
+ <%= csp_meta_tag %>
8
+
9
+ <%= stylesheet_link_tag 'application', media: 'all' %>
10
+ </head>
11
+
12
+ <body>
13
+ <%= yield %>
14
+ </body>
15
+ </html>
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <style>
6
+ /* Email styles need to be inline */
7
+ </style>
8
+ </head>
9
+
10
+ <body>
11
+ <%= yield %>
12
+ </body>
13
+ </html>
@@ -0,0 +1 @@
1
+ <%= yield %>
data/dummy/bin/rails ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ APP_PATH = File.expand_path('../config/application', __dir__)
5
+ require_relative '../config/boot'
6
+ require 'rails/commands'
data/dummy/bin/rake ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../config/boot'
5
+ require 'rake'
6
+ Rake.application.run
data/dummy/bin/setup ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'fileutils'
5
+
6
+ # path to your application root.
7
+ APP_ROOT = File.expand_path('..', __dir__)
8
+
9
+ def system!(*args)
10
+ system(*args) || abort("\n== Command #{args} failed ==")
11
+ end
12
+
13
+ FileUtils.chdir APP_ROOT do
14
+ # This script is a way to set up or update your development environment automatically.
15
+ # This script is idempotent, so that you can run it at any time and get an expectable outcome.
16
+ # Add necessary setup steps to this file.
17
+
18
+ puts '== Installing dependencies =='
19
+ system! 'gem install bundler --conservative'
20
+ system('bundle check') || system!('bundle install')
21
+
22
+ # puts "\n== Copying sample files =="
23
+ # unless File.exist?('config/database.yml')
24
+ # FileUtils.cp 'config/database.yml.sample', 'config/database.yml'
25
+ # end
26
+
27
+ puts "\n== Preparing database =="
28
+ system! 'bin/rails db:prepare'
29
+
30
+ puts "\n== Removing old logs and tempfiles =="
31
+ system! 'bin/rails log:clear tmp:clear'
32
+
33
+ puts "\n== Restarting application server =="
34
+ system! 'bin/rails restart'
35
+ end