hard_worker 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +13 -0
  5. data/CHANGELOG.md +11 -1
  6. data/Gemfile +3 -0
  7. data/Gemfile.lock +133 -1
  8. data/README.md +48 -1
  9. data/bin/bundle +3 -0
  10. data/bin/hard_worker +32 -1
  11. data/dummy/Rakefile +8 -0
  12. data/dummy/app/assets/config/manifest.js +3 -0
  13. data/dummy/app/assets/images/.keep +0 -0
  14. data/dummy/app/assets/stylesheets/application.css +15 -0
  15. data/dummy/app/channels/application_cable/channel.rb +6 -0
  16. data/dummy/app/channels/application_cable/connection.rb +6 -0
  17. data/dummy/app/controllers/application_controller.rb +5 -0
  18. data/dummy/app/controllers/concerns/.keep +0 -0
  19. data/dummy/app/helpers/application_helper.rb +4 -0
  20. data/dummy/app/javascript/packs/application.js +15 -0
  21. data/dummy/app/jobs/application_job.rb +9 -0
  22. data/dummy/app/mailers/application_mailer.rb +6 -0
  23. data/dummy/app/models/application_record.rb +5 -0
  24. data/dummy/app/models/concerns/.keep +0 -0
  25. data/dummy/app/models/user.rb +2 -0
  26. data/dummy/app/views/application/index.html.erb +4 -0
  27. data/dummy/app/views/layouts/application.html.erb +15 -0
  28. data/dummy/app/views/layouts/mailer.html.erb +13 -0
  29. data/dummy/app/views/layouts/mailer.text.erb +1 -0
  30. data/dummy/bin/rails +6 -0
  31. data/dummy/bin/rake +6 -0
  32. data/dummy/bin/setup +35 -0
  33. data/dummy/config.ru +8 -0
  34. data/dummy/config/application.rb +23 -0
  35. data/dummy/config/boot.rb +7 -0
  36. data/dummy/config/cable.yml +10 -0
  37. data/dummy/config/credentials.yml.enc +1 -0
  38. data/dummy/config/database.yml +25 -0
  39. data/dummy/config/environment.rb +7 -0
  40. data/dummy/config/environments/development.rb +78 -0
  41. data/dummy/config/environments/production.rb +122 -0
  42. data/dummy/config/environments/test.rb +51 -0
  43. data/dummy/config/initializers/application_controller_renderer.rb +9 -0
  44. data/dummy/config/initializers/backtrace_silencers.rb +10 -0
  45. data/dummy/config/initializers/content_security_policy.rb +29 -0
  46. data/dummy/config/initializers/cookies_serializer.rb +7 -0
  47. data/dummy/config/initializers/filter_parameter_logging.rb +8 -0
  48. data/dummy/config/initializers/inflections.rb +17 -0
  49. data/dummy/config/initializers/mime_types.rb +5 -0
  50. data/dummy/config/initializers/permissions_policy.rb +12 -0
  51. data/dummy/config/initializers/wrap_parameters.rb +16 -0
  52. data/dummy/config/locales/en.yml +33 -0
  53. data/dummy/config/master.key +1 -0
  54. data/dummy/config/puma.rb +45 -0
  55. data/dummy/config/routes.rb +5 -0
  56. data/dummy/config/storage.yml +34 -0
  57. data/dummy/db/migrate/20210613015406_create_users.rb +10 -0
  58. data/dummy/db/production.sqlite3 +0 -0
  59. data/dummy/db/schema.rb +20 -0
  60. data/dummy/db/test.sqlite3 +0 -0
  61. data/dummy/lib/assets/.keep +0 -0
  62. data/dummy/log/.keep +0 -0
  63. data/dummy/log/development.log +0 -0
  64. data/dummy/log/production.log +3 -0
  65. data/dummy/log/test.log +31 -0
  66. data/dummy/public/404.html +67 -0
  67. data/dummy/public/422.html +67 -0
  68. data/dummy/public/500.html +66 -0
  69. data/dummy/public/apple-touch-icon-precomposed.png +0 -0
  70. data/dummy/public/apple-touch-icon.png +0 -0
  71. data/dummy/public/favicon.ico +0 -0
  72. data/dummy/storage/.keep +0 -0
  73. data/dummy/tmp/.keep +0 -0
  74. data/dummy/tmp/development_secret.txt +1 -0
  75. data/dummy/tmp/pids/.keep +0 -0
  76. data/dummy/tmp/storage/.keep +0 -0
  77. data/{hardwordker.gemspec → hard_worker.gemspec} +7 -0
  78. data/lib/hard_worker.rb +87 -9
  79. data/lib/hard_worker/client.rb +16 -0
  80. data/lib/hard_worker/rails.rb +5 -0
  81. data/lib/hard_worker/version.rb +1 -1
  82. data/lib/hard_worker/worker.rb +1 -1
  83. metadata +91 -4
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ .rails-default-error-page {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ .rails-default-error-page div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ .rails-default-error-page div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ .rails-default-error-page h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ .rails-default-error-page div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body class="rails-default-error-page">
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
File without changes
File without changes
File without changes
data/dummy/tmp/.keep ADDED
File without changes
@@ -0,0 +1 @@
1
+ 472e20581a3a96e1b857692e2f63ae72cf6be5d0e8de29df370dfc87b3bd085211fd32a43d00b66af3400b51f2d4b8f2fce29421b529a8e72b365560d9b61bc3
File without changes
File without changes
@@ -31,7 +31,14 @@ Gem::Specification.new do |spec|
31
31
 
32
32
  # Uncomment to register a new dependency of your gem
33
33
  spec.add_dependency 'drb'
34
+ spec.add_dependency 'dry-configurable'
34
35
  spec.add_development_dependency 'byebug'
36
+ spec.post_install_message = <<-MESSAGE
37
+ This gem is going to change it's name to Belated!
38
+ HardWorker will no longer be maintained.
39
+ If you happen to use this gem, please consider switching to Belated.
40
+ https://github.com/sampokuokkanen/belated
41
+ MESSAGE
35
42
 
36
43
  # For more information and examples about making a new gem, checkout our
37
44
  # guide at: https://bundler.io/guides/creating_gem.html
data/lib/hard_worker.rb CHANGED
@@ -4,39 +4,72 @@ require_relative 'hard_worker/version'
4
4
  require_relative 'hard_worker/worker'
5
5
  require 'drb'
6
6
  require 'yaml'
7
+ require 'singleton'
8
+ require 'dry-configurable'
9
+ require 'hard_worker/client'
10
+ require 'hard_worker/rails' if defined?(::Rails::Engine)
7
11
 
8
12
  # HardWorker is a pure Ruby job backend.
9
13
  # It has limited functionality, as it only accepts
10
14
  # jobs as procs, but that might make it useful if you don't
11
15
  # need anything as big as Redis.
12
- # Loses all jobs if restarted.
16
+ # Saves jobs into a file as YAML as long as they're not procs
17
+ # and reloads them when started again.
13
18
  class HardWorker
14
- URI = 'druby://localhost:8788'
19
+ extend Dry::Configurable
20
+ include Singleton unless $TESTING
21
+ URI = "druby://localhost:#{$TESTING ? Array.new(4) { rand(10) }.join : "8788"}"
15
22
  FILE_NAME = 'hard_worker_dump'
16
23
  @@queue = Queue.new
17
24
 
18
- def initialize(workers: 1, connect: false)
25
+ setting :rails, true
26
+ setting :rails_path, '.'
27
+ setting :workers, 1
28
+ setting :connect, true
29
+ setting :environment, 'development'
30
+
31
+ def start
32
+ boot_app
19
33
  load_jobs
20
34
  @worker_list = []
21
- workers.times do |_i|
35
+ HardWorker.config.workers.times do |_i|
22
36
  @worker_list << Thread.new { Worker.new }
23
37
  end
24
- return unless connect
38
+ return unless HardWorker.config.connect
25
39
 
26
- DRb.start_service(URI, @@queue)
27
- puts "listening on #{URI}"
40
+ DRb.start_service(URI, @@queue, verbose: true)
41
+ puts banner_and_info
28
42
  DRb.thread.join
29
43
  end
44
+ alias initialize start
45
+
46
+ def boot_app
47
+ return unless rails?
48
+
49
+ ENV['RAILS_ENV'] ||= HardWorker.config.environment
50
+ require File.expand_path("#{HardWorker.config.rails_path}/config/environment.rb")
51
+ require 'rails/all'
52
+ require 'hard_worker/rails'
53
+ end
54
+
55
+ def rails?
56
+ HardWorker.config.rails
57
+ end
30
58
 
31
59
  def load_jobs
32
60
  jobs = YAML.load(File.binread(FILE_NAME))
33
61
  jobs.each do |job|
34
62
  @@queue.push(job)
35
63
  end
64
+ File.delete(HardWorker::FILE_NAME) if File.exist?(HardWorker::FILE_NAME)
36
65
  rescue StandardError
37
66
  # do nothing
38
67
  end
39
68
 
69
+ def reload
70
+ load_jobs
71
+ end
72
+
40
73
  def stop_workers
41
74
  @worker_list.each do |worker|
42
75
  Thread.kill(worker)
@@ -50,8 +83,53 @@ class HardWorker
50
83
  File.open(FILE_NAME, 'wb') { |f| f.write(YAML.dump(class_array)) }
51
84
  end
52
85
 
53
- def reset_queue!
54
- @@queue = Queue.new
86
+ def self.stop_workers
87
+ @worker_list&.each do |worker|
88
+ Thread.kill(worker)
89
+ end
90
+ class_array = []
91
+ @@queue.size.times do |_i|
92
+ next if (klass_or_proc = @@queue.pop).instance_of?(Proc)
93
+
94
+ class_array << klass_or_proc
95
+ end
96
+ File.open(FILE_NAME, 'wb') { |f| f.write(YAML.dump(class_array)) }
97
+ end
98
+
99
+ # rubocop:disable Layout/LineLength
100
+ # rubocop:disable Metrics/MethodLength
101
+ def banner
102
+ <<-'BANNER'
103
+ .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------.
104
+ | .--------------. | .--------------. | .--------------. | .--------------. | .--------------. | .--------------. | .--------------. |
105
+ | | ______ | | | _________ | | | _____ | | | __ | | | _________ | | | _________ | | | ________ | |
106
+ | | |_ _ \ | | | |_ ___ | | | | |_ _| | | | / \ | | | | _ _ | | | | |_ ___ | | | | |_ ___ `. | |
107
+ | | | |_) | | | | | |_ \_| | | | | | | | | / /\ \ | | | |_/ | | \_| | | | | |_ \_| | | | | | `. \ | |
108
+ | | | __'. | | | | _| _ | | | | | _ | | | / ____ \ | | | | | | | | | _| _ | | | | | | | | |
109
+ | | _| |__) | | | | _| |___/ | | | | _| |__/ | | | | _/ / \ \_ | | | _| |_ | | | _| |___/ | | | | _| |___.' / | |
110
+ | | |_______/ | | | |_________| | | | |________| | | ||____| |____|| | | |_____| | | | |_________| | | | |________.' | |
111
+ | | | | | | | | | | | | | | | | | | | | | |
112
+ | '--------------' | '--------------' | '--------------' | '--------------' | '--------------' | '--------------' | '--------------' |
113
+ '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
114
+ BANNER
115
+ end
116
+
117
+ def banner_and_info
118
+ puts banner
119
+ puts 'HardWorker is going to change to Belated!'
120
+ puts "Currently running HardWorker version #{HardWorker::VERSION}"
121
+ puts %(HardWorker running #{@worker_list&.length.to_i} workers on #{URI}...)
122
+ end
123
+
124
+ def stats
125
+ {
126
+ jobs: @@queue.size,
127
+ workers: @worker_list&.length
128
+ }
129
+ end
130
+
131
+ def self.clear_queue!
132
+ @@queue.clear
55
133
  end
56
134
 
57
135
  def job_list
@@ -0,0 +1,16 @@
1
+ class HardWorker
2
+ class Client
3
+ attr_accessor :queue
4
+
5
+ def initialize
6
+ server_uri = HardWorker::URI
7
+ DRb.start_service
8
+ self.queue = DRbObject.new_with_uri(server_uri)
9
+ end
10
+
11
+ def perform_belated(job)
12
+ queue.push(job)
13
+ end
14
+ alias perform_later perform_belated
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ class HardWorker
2
+ class Rails < ::Rails::Engine
3
+ isolate_namespace HardWorker
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class HardWorker
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  end
@@ -17,7 +17,7 @@ class HardWorker
17
17
  end
18
18
 
19
19
  def call_job(job)
20
- if job.instance_of?(Proc)
20
+ if job.respond_to?(:call)
21
21
  pp job.call
22
22
  else
23
23
  pp job&.perform
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hard_worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sampo Kuokkanen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-16 00:00:00.000000000 Z
11
+ date: 2021-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: drb
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-configurable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: byebug
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -59,11 +73,80 @@ files:
59
73
  - LICENSE.txt
60
74
  - README.md
61
75
  - Rakefile
76
+ - bin/bundle
62
77
  - bin/console
63
78
  - bin/hard_worker
64
79
  - bin/setup
65
- - hardwordker.gemspec
80
+ - dummy/Rakefile
81
+ - dummy/app/assets/config/manifest.js
82
+ - dummy/app/assets/images/.keep
83
+ - dummy/app/assets/stylesheets/application.css
84
+ - dummy/app/channels/application_cable/channel.rb
85
+ - dummy/app/channels/application_cable/connection.rb
86
+ - dummy/app/controllers/application_controller.rb
87
+ - dummy/app/controllers/concerns/.keep
88
+ - dummy/app/helpers/application_helper.rb
89
+ - dummy/app/javascript/packs/application.js
90
+ - dummy/app/jobs/application_job.rb
91
+ - dummy/app/mailers/application_mailer.rb
92
+ - dummy/app/models/application_record.rb
93
+ - dummy/app/models/concerns/.keep
94
+ - dummy/app/models/user.rb
95
+ - dummy/app/views/application/index.html.erb
96
+ - dummy/app/views/layouts/application.html.erb
97
+ - dummy/app/views/layouts/mailer.html.erb
98
+ - dummy/app/views/layouts/mailer.text.erb
99
+ - dummy/bin/rails
100
+ - dummy/bin/rake
101
+ - dummy/bin/setup
102
+ - dummy/config.ru
103
+ - dummy/config/application.rb
104
+ - dummy/config/boot.rb
105
+ - dummy/config/cable.yml
106
+ - dummy/config/credentials.yml.enc
107
+ - dummy/config/database.yml
108
+ - dummy/config/environment.rb
109
+ - dummy/config/environments/development.rb
110
+ - dummy/config/environments/production.rb
111
+ - dummy/config/environments/test.rb
112
+ - dummy/config/initializers/application_controller_renderer.rb
113
+ - dummy/config/initializers/backtrace_silencers.rb
114
+ - dummy/config/initializers/content_security_policy.rb
115
+ - dummy/config/initializers/cookies_serializer.rb
116
+ - dummy/config/initializers/filter_parameter_logging.rb
117
+ - dummy/config/initializers/inflections.rb
118
+ - dummy/config/initializers/mime_types.rb
119
+ - dummy/config/initializers/permissions_policy.rb
120
+ - dummy/config/initializers/wrap_parameters.rb
121
+ - dummy/config/locales/en.yml
122
+ - dummy/config/master.key
123
+ - dummy/config/puma.rb
124
+ - dummy/config/routes.rb
125
+ - dummy/config/storage.yml
126
+ - dummy/db/migrate/20210613015406_create_users.rb
127
+ - dummy/db/production.sqlite3
128
+ - dummy/db/schema.rb
129
+ - dummy/db/test.sqlite3
130
+ - dummy/lib/assets/.keep
131
+ - dummy/log/.keep
132
+ - dummy/log/development.log
133
+ - dummy/log/production.log
134
+ - dummy/log/test.log
135
+ - dummy/public/404.html
136
+ - dummy/public/422.html
137
+ - dummy/public/500.html
138
+ - dummy/public/apple-touch-icon-precomposed.png
139
+ - dummy/public/apple-touch-icon.png
140
+ - dummy/public/favicon.ico
141
+ - dummy/storage/.keep
142
+ - dummy/tmp/.keep
143
+ - dummy/tmp/development_secret.txt
144
+ - dummy/tmp/pids/.keep
145
+ - dummy/tmp/storage/.keep
146
+ - hard_worker.gemspec
66
147
  - lib/hard_worker.rb
148
+ - lib/hard_worker/client.rb
149
+ - lib/hard_worker/rails.rb
67
150
  - lib/hard_worker/version.rb
68
151
  - lib/hard_worker/worker.rb
69
152
  homepage: https://github.com/sampokuokkanen/hard_worker
@@ -73,7 +156,11 @@ metadata:
73
156
  homepage_uri: https://github.com/sampokuokkanen/hard_worker
74
157
  source_code_uri: https://github.com/sampokuokkanen/hard_worker
75
158
  changelog_uri: https://github.com/sampokuokkanen/hard_worker/blob/main/CHANGELOG.md
76
- post_install_message:
159
+ post_install_message: |2
160
+ This gem is going to change it's name to Belated!
161
+ HardWorker will no longer be maintained.
162
+ If you happen to use this gem, please consider switching to Belated.
163
+ https://github.com/sampokuokkanen/belated
77
164
  rdoc_options: []
78
165
  require_paths:
79
166
  - lib