belated 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/main.yml +16 -0
- data/.gitignore +14 -0
- data/.rake_tasks~ +10 -0
- data/.rspec +4 -0
- data/.rubocop.yml +42 -0
- data/CHANGELOG.md +25 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +191 -0
- data/LICENSE.txt +21 -0
- data/README.md +135 -0
- data/Rakefile +12 -0
- data/belated.gemspec +40 -0
- data/bin/belated +38 -0
- data/bin/bundle +3 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/dummy/Rakefile +8 -0
- data/dummy/app/assets/config/manifest.js +3 -0
- data/dummy/app/assets/images/.keep +0 -0
- data/dummy/app/assets/stylesheets/application.css +15 -0
- data/dummy/app/channels/application_cable/channel.rb +6 -0
- data/dummy/app/channels/application_cable/connection.rb +6 -0
- data/dummy/app/controllers/application_controller.rb +5 -0
- data/dummy/app/controllers/concerns/.keep +0 -0
- data/dummy/app/helpers/application_helper.rb +4 -0
- data/dummy/app/javascript/packs/application.js +15 -0
- data/dummy/app/jobs/application_job.rb +9 -0
- data/dummy/app/mailers/application_mailer.rb +6 -0
- data/dummy/app/models/application_record.rb +5 -0
- data/dummy/app/models/concerns/.keep +0 -0
- data/dummy/app/models/user.rb +2 -0
- data/dummy/app/views/application/index.html.erb +4 -0
- data/dummy/app/views/layouts/application.html.erb +15 -0
- data/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/dummy/bin/rails +6 -0
- data/dummy/bin/rake +6 -0
- data/dummy/bin/setup +35 -0
- data/dummy/config.ru +8 -0
- data/dummy/config/application.rb +23 -0
- data/dummy/config/boot.rb +7 -0
- data/dummy/config/cable.yml +10 -0
- data/dummy/config/credentials.yml.enc +1 -0
- data/dummy/config/database.yml +25 -0
- data/dummy/config/environment.rb +7 -0
- data/dummy/config/environments/development.rb +78 -0
- data/dummy/config/environments/production.rb +122 -0
- data/dummy/config/environments/test.rb +51 -0
- data/dummy/config/initializers/application_controller_renderer.rb +9 -0
- data/dummy/config/initializers/backtrace_silencers.rb +10 -0
- data/dummy/config/initializers/content_security_policy.rb +29 -0
- data/dummy/config/initializers/cookies_serializer.rb +7 -0
- data/dummy/config/initializers/filter_parameter_logging.rb +8 -0
- data/dummy/config/initializers/inflections.rb +17 -0
- data/dummy/config/initializers/mime_types.rb +5 -0
- data/dummy/config/initializers/permissions_policy.rb +12 -0
- data/dummy/config/initializers/wrap_parameters.rb +16 -0
- data/dummy/config/locales/en.yml +33 -0
- data/dummy/config/master.key +1 -0
- data/dummy/config/puma.rb +45 -0
- data/dummy/config/routes.rb +5 -0
- data/dummy/config/storage.yml +34 -0
- data/dummy/db/migrate/20210613015406_create_users.rb +10 -0
- data/dummy/db/production.sqlite3 +0 -0
- data/dummy/db/schema.rb +20 -0
- data/dummy/db/test.sqlite3 +0 -0
- data/dummy/lib/assets/.keep +0 -0
- data/dummy/log/.keep +0 -0
- data/dummy/log/development.log +0 -0
- data/dummy/log/production.log +3 -0
- data/dummy/log/test.log +595 -0
- data/dummy/public/404.html +67 -0
- data/dummy/public/422.html +67 -0
- data/dummy/public/500.html +66 -0
- data/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/dummy/public/apple-touch-icon.png +0 -0
- data/dummy/public/favicon.ico +0 -0
- data/dummy/storage/.keep +0 -0
- data/dummy/tmp/.keep +0 -0
- data/dummy/tmp/development_secret.txt +1 -0
- data/dummy/tmp/pids/.keep +0 -0
- data/dummy/tmp/storage/.keep +0 -0
- data/hard_worker_dump +1 -0
- data/lib/belated.rb +147 -0
- data/lib/belated/client.rb +16 -0
- data/lib/belated/rails.rb +5 -0
- data/lib/belated/version.rb +5 -0
- data/lib/belated/worker.rb +27 -0
- 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
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
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
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
|
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
|
+
*/
|
File without changes
|
@@ -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
|
File without changes
|
@@ -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 @@
|
|
1
|
+
<%= yield %>
|
data/dummy/bin/rails
ADDED
data/dummy/bin/rake
ADDED
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
|