hard_worker 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5610742592c993b450f98d73be0108ce618772b71b8d89f948ffdaf81e40348
4
- data.tar.gz: c1cf069bdc9a5ea5efc427e03db3ad894905376770ddbf2aef331c679719eb0d
3
+ metadata.gz: b52df482216a2a82f96d8bdaca1f10e7982cb5c897fb0488abb2294ecfa0e33a
4
+ data.tar.gz: 5b7368324bff956a7f31987a209f07ab685e0fdc9aed96dd4cec5e2bf20014eb
5
5
  SHA512:
6
- metadata.gz: 58f838abf8e944cde55c7335a6c09e5f33125de95cb19ee95ce9c1732774a379f80ffd36a8a2c072b9d2a9c44cad882cfd96d892758a839a5e4b92e27dcbdc4b
7
- data.tar.gz: 44d12718ef28aaa42bfdce9fb3dc339bfacb08820735cc861f1b54312dfb71372b8da631a4b1f89271985a8a24d090a6b63ce46faf3e37699a217e4faa745de5
6
+ metadata.gz: 680f5d641d281b19b5362ba5380a804582efa1d9a46be5c582f527639aea056351cc310768edeca85fc600770390f20d79198edda9856efc93aefe5beb030ec3
7
+ data.tar.gz: c4f544d238c9f2403fde1f88009b3c5f0d75001b5310d84c9cd97c6f1049d6e5618e08b274a1b3778feb2e83ae80a1311063d9128d0344325a9247757b92b9b2
data/.rubocop.yml CHANGED
@@ -20,4 +20,7 @@ Style/AsciiComments:
20
20
  Enabled: false
21
21
 
22
22
  Style/ClassVars:
23
+ Enabled: false
24
+
25
+ Security/YAMLLoad:
23
26
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -8,4 +8,9 @@ Things are almost working. You can enqueue jobs! That should be more than enough
8
8
 
9
9
  ## [0.0.2] - 2021-07-15
10
10
 
11
- - Bugfix: was requiring byebug.
11
+ - Bugfix: was requiring byebug.
12
+
13
+ ## [0.0.3] - 2021-07-16
14
+
15
+ - YAML Marshalled queue! So now the queue stays in memory even if restarted. Though kill and term signals still need catching.. that is not done yet.
16
+ - Also, now you can use classes! As long as the class has a perform-method.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hard_worker (0.0.2)
4
+ hard_worker (0.0.3)
5
5
  drb
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,12 +1,13 @@
1
1
  # Hardworker
2
2
 
3
- This is HardWorker, a new Ruby backend job library! It supports running procs in the background.
4
- However, it does not yet support classes. So only procs. Also, you lose all jobs if you restart the process.
3
+ This is HardWorker, a new Ruby backend job library! It supports running procs and classes in the background.
4
+ ~~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.
5
5
 
6
- But it uses dRuby to do the communication! Which is absolute great. No need for Redis or PostgreSQL, just Ruby standard libraries.
6
+ It uses dRuby to do the communication! Which is absolute great. No need for Redis or PostgreSQL, just Ruby standard libraries.
7
7
 
8
8
  TODO LIST:
9
- - Marshal the job queue into a file so you don't lose all progress
9
+ - ~~Marshal the job queue into a file so you don't lose all progress~~
10
+ (Ended up using YAML)
10
11
  - Support Rails and ActiveJob
11
12
  - Have a web UI
12
13
  - Do some performance testing
@@ -26,13 +27,13 @@ And then execute:
26
27
 
27
28
  Or install it yourself as:
28
29
 
29
- $ gem install hardworker
30
+ $ gem install hard_worker
30
31
 
31
32
  ## Usage
32
33
 
33
34
  Start up HardWorker!
34
35
 
35
- $ hardworker
36
+ $ hard_worker
36
37
 
37
38
  Then, in another program, connect to HardWorker and give it a job to do.
38
39
  Sample below:
@@ -47,8 +48,18 @@ class DummyWorker
47
48
  end
48
49
  end
49
50
 
51
+ class DumDum
52
+ # classes need to have a perform method
53
+ def perform
54
+ 5 / 4
55
+ end
56
+ end
57
+
58
+ # Need to start dRuby on the client side
59
+ DRb.start_service
50
60
  dummy = DummyWorker.new
51
61
  dummy.queue.push(proc { 2 / 1 })
62
+ dummy.queue.push(DumDum.new)
52
63
  ```
53
64
 
54
65
 
@@ -60,7 +71,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
60
71
 
61
72
  ## Contributing
62
73
 
63
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hardworker. 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).
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sampokuokkanen/hard_worker. 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).
64
75
 
65
76
  ## License
66
77
 
@@ -68,4 +79,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
68
79
 
69
80
  ## Code of Conduct
70
81
 
71
- Everyone interacting in the Hardworker 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).
82
+ Everyone interacting in the HardWorker 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/lib/hard_worker.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative 'hard_worker/version'
4
4
  require_relative 'hard_worker/worker'
5
5
  require 'drb'
6
+ require 'yaml'
6
7
 
7
8
  # HardWorker is a pure Ruby job backend.
8
9
  # It has limited functionality, as it only accepts
@@ -11,9 +12,11 @@ require 'drb'
11
12
  # Loses all jobs if restarted.
12
13
  class HardWorker
13
14
  URI = 'druby://localhost:8788'
15
+ FILE_NAME = 'hard_worker_dump'
14
16
  @@queue = Queue.new
15
17
 
16
18
  def initialize(workers: 1, connect: false)
19
+ load_jobs
17
20
  @worker_list = []
18
21
  workers.times do |_i|
19
22
  @worker_list << Thread.new { Worker.new }
@@ -25,10 +28,30 @@ class HardWorker
25
28
  DRb.thread.join
26
29
  end
27
30
 
31
+ def load_jobs
32
+ jobs = YAML.load(File.binread(FILE_NAME))
33
+ jobs.each do |job|
34
+ @@queue.push(job)
35
+ end
36
+ rescue StandardError
37
+ # do nothing
38
+ end
39
+
28
40
  def stop_workers
29
41
  @worker_list.each do |worker|
30
42
  Thread.kill(worker)
31
43
  end
44
+ class_array = []
45
+ @@queue.size.times do |_i|
46
+ next if (klass_or_proc = @@queue.pop).instance_of?(Proc)
47
+
48
+ class_array << klass_or_proc
49
+ end
50
+ File.open(FILE_NAME, 'wb') { |f| f.write(YAML.dump(class_array)) }
51
+ end
52
+
53
+ def reset_queue!
54
+ @@queue = Queue.new
32
55
  end
33
56
 
34
57
  def job_list
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class HardWorker
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
@@ -8,9 +8,20 @@ class HardWorker
8
8
 
9
9
  def start_working
10
10
  loop do
11
- pp HardWorker.fetch_job.call
11
+ job = HardWorker.fetch_job
12
+ next unless job
13
+
14
+ call_job(job)
12
15
  puts 'fetching jobs...'
13
16
  end
14
17
  end
18
+
19
+ def call_job(job)
20
+ if job.instance_of?(Proc)
21
+ pp job.call
22
+ else
23
+ pp job&.perform
24
+ end
25
+ end
15
26
  end
16
27
  end
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.2
4
+ version: 0.0.3
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-15 00:00:00.000000000 Z
11
+ date: 2021-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: drb