assistor 0.2.0 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1fa2ddaa11e30636eb624ee437c724f2a1602fdf4951cf386d1039ccc7b4d664
4
- data.tar.gz: 52f0e901cb875e920d43e677592f11dd4f20b045f7dd8752b95f5dd20c9a23e4
3
+ metadata.gz: 559f602e1a1af5c5e5c8426d77a9de3f5ffb4a854dbf7128fc69b4edbf673fed
4
+ data.tar.gz: d1573a2fd09629f9ca03a874749ab3d6e3792078f40e672bfbf7a150ca65e68a
5
5
  SHA512:
6
- metadata.gz: 2c5f78349106336a9716bb07ab1835309ae587b9aef14da0a9bb9c84aabb4183e480a0f667e651d8fa2f3759178573084ae4bad7d1223cc4ea67086cb5b2a77b
7
- data.tar.gz: e0d5c02282a5929a2e5e0f89bb09ba1c77b084b6c168dc4b6a0dda723ac0ccee2c8c38d1ea2f44f5b03bd81fe9978bf3be924d645b825962fbec6634f89778e7
6
+ metadata.gz: 3497d498dcac79be25fa482b1d0dcb608e6cdd4d086567b45a1f2f48799f2af4f262c36e930e9983e835b4b4e0c0f98c2ecced41642343f20f965e697594172c
7
+ data.tar.gz: 78cd088e121929b6bc649eab84f85ae2e514017b9726eac43eb64187a9f127eddf1e897cb5c30c7cef0c5934ae19387fb123f256d076e02cee04bf1c2aaf492d
data/CHANGELOG.md CHANGED
@@ -0,0 +1,20 @@
1
+ # Changelog
2
+
3
+ ## 0.3.0
4
+
5
+ ### Changed
6
+ * **Breaking:** require Ruby >= 3.2.
7
+ * Add `logger` as a runtime dependency.
8
+ * Assistant pid files are stored in a private `assistor-<uid>` directory under the system tmpdir instead of directly in it.
9
+
10
+ ### Security
11
+ * Pid files and log files are created with `0600` permissions and never follow symlinks.
12
+ * The assistant runtime directory is verified to be owned by the current user and not accessible by others.
13
+
14
+ ### Fixed
15
+ * `Assistant#assists?` handles `EPERM` and always returns a boolean.
16
+ * `PidFile#delete` ignores an already missing file.
17
+ * `Daemon` survives a failing exception handler and removes its pid file after a fatal error.
18
+
19
+ ### Development
20
+ * Add specs, RuboCop, bundler-audit, Dependabot and GitHub Actions CI.
data/README.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  Simple background processing framework for Ruby
4
4
 
5
+ ## Requirements
6
+
7
+ * Ruby >= 3.2
8
+ * A Unix-like OS (the gem relies on `fork`, `Process.daemon` and signals, so Windows is not supported)
9
+
5
10
  ## Installation
6
11
 
7
12
  Add this line to your application's Gemfile:
@@ -73,7 +78,7 @@ configuration = Assistor::Configuration.new
73
78
  configuration.id = 'example'
74
79
  configuration.size = 3
75
80
  configuration.delay = 0.2
76
- configuration.exception_handler = Proc.new { |exception| ExceptionHandler.handle(exception) }_
81
+ configuration.exception_handler = Proc.new { |exception| ExceptionHandler.handle(exception) }
77
82
  configuration.log_file = File.join(File.expand_path('..', __FILE__), 'example.log')
78
83
  configuration.pid_file = File.join(File.expand_path('..', __FILE__), 'example.pid')
79
84
 
@@ -82,7 +87,7 @@ Assistor::Daemon.new(configuration).run(queue)
82
87
 
83
88
  ## Development
84
89
 
85
- 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.
90
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests, `bundle exec rubocop` to lint and `bundle exec bundle-audit check --update` to check dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
86
91
 
87
92
  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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
88
93
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Assistor
2
4
  class AbstractJob
3
5
  def id
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Assistor
2
4
  class AbstractQueue
3
5
  def pop
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Assistor
2
4
  class Assistant
3
5
  extend Forwardable
@@ -26,13 +28,14 @@ module Assistor
26
28
  stop
27
29
  end
28
30
  detach
29
- rescue => e
31
+ rescue StandardError => e
30
32
  log_file.error e
31
33
  end
32
34
 
33
35
  def assists?
34
36
  Process.kill(0, pid)
35
- rescue Errno::ESRCH
37
+ true
38
+ rescue Errno::ESRCH, Errno::EPERM
36
39
  false
37
40
  end
38
41
 
@@ -46,22 +49,23 @@ module Assistor
46
49
 
47
50
  def process
48
51
  with_pid_file { @job.run }
49
- rescue => e
52
+ rescue StandardError => e
50
53
  log_file.error e
51
54
  @configuration.exception_handler.call(e)
52
55
  end
53
56
 
54
57
  def stop
55
58
  return if pid_file.exist?
59
+
56
60
  exit!(0)
57
- rescue => e
61
+ rescue StandardError => e
58
62
  log_file.error e
59
63
  end
60
64
 
61
65
  def stop!
62
66
  pid_file.delete
63
67
  exit!(1)
64
- rescue => e
68
+ rescue StandardError => e
65
69
  log_file.error e
66
70
  end
67
71
 
@@ -100,7 +104,18 @@ module Assistor
100
104
  end
101
105
 
102
106
  def pid_file
103
- @pid_file ||= PidFile.new File.join(Dir.tmpdir, format('%s.pid', Process.pid))
107
+ @pid_file ||= PidFile.new File.join(runtime_dir, format('%s.pid', Process.pid))
108
+ end
109
+
110
+ def runtime_dir
111
+ dir = File.join(Dir.tmpdir, format('assistor-%s', Process.uid))
112
+ Dir.mkdir(dir, 0o700) unless File.directory?(dir)
113
+ stat = File.lstat(dir)
114
+ unless stat.directory? && stat.owned? && stat.mode.nobits?(0o077)
115
+ raise SecurityError, "Insecure runtime directory: #{dir}"
116
+ end
117
+
118
+ dir
104
119
  end
105
120
 
106
121
  def with_pid_file
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Assistor
2
4
  class AssistantGroup
3
5
  attr_reader :size
@@ -21,21 +23,17 @@ module Assistor
21
23
 
22
24
  def wait
23
25
  @assistants.each do |assistant|
24
- begin
25
- Process.wait(assistant.pid)
26
- rescue Errno::ECHILD
27
- next
28
- end
26
+ Process.wait(assistant.pid)
27
+ rescue Errno::ECHILD
28
+ next
29
29
  end
30
30
  end
31
31
 
32
32
  def stop
33
33
  @assistants.each do |assistant|
34
- begin
35
- Process.kill(:INT, assistant.pid)
36
- rescue Errno::ESRCH
37
- next
38
- end
34
+ Process.kill(:INT, assistant.pid)
35
+ rescue Errno::ESRCH
36
+ next
39
37
  end
40
38
  end
41
39
 
@@ -44,7 +42,7 @@ module Assistor
44
42
  end
45
43
 
46
44
  def decrease
47
- @size -= 1 if @size > 0
45
+ @size -= 1 if @size.positive?
48
46
  end
49
47
  end
50
48
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Assistor
2
4
  class Configuration
3
5
  attr_accessor :id, :size, :delay
@@ -11,12 +13,10 @@ module Assistor
11
13
  @pid_file = PidFile.new file
12
14
  end
13
15
 
14
- def exception_handler(exception_handler)
15
- @exception_handler = exception_handler
16
- end
16
+ attr_writer :exception_handler
17
17
 
18
18
  def exception_handler
19
- @exception_handler || Proc.new {}
19
+ @exception_handler || proc {}
20
20
  end
21
21
  end
22
22
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Assistor
2
4
  class Daemon
3
5
  extend Forwardable
@@ -17,9 +19,9 @@ module Assistor
17
19
  loop do
18
20
  sleep(delay) unless assist(queue)
19
21
  end
20
- rescue => e
21
- log_file.error e
22
- @configuration.exception_handler.call(e)
22
+ rescue StandardError => e
23
+ handle_exception(e)
24
+ pid_file.delete
23
25
  end
24
26
 
25
27
  private
@@ -37,9 +39,8 @@ module Assistor
37
39
  assistant_group.flush
38
40
  pid_file.delete
39
41
  exit!(0)
40
- rescue => e
41
- log_file.error e
42
- @configuration.exception_handler.call(e)
42
+ rescue StandardError => e
43
+ handle_exception(e)
43
44
  end
44
45
 
45
46
  def stop!
@@ -47,23 +48,28 @@ module Assistor
47
48
  assistant_group.flush
48
49
  pid_file.delete
49
50
  exit!(1)
50
- rescue => e
51
- log_file.error e
52
- @configuration.exception_handler.call(e)
51
+ rescue StandardError => e
52
+ handle_exception(e)
53
53
  end
54
54
 
55
55
  def assist(queue)
56
56
  assistant_group.flush
57
57
  return if assistant_group.filled?
58
- return unless job = queue.pop
58
+ return unless (job = queue.pop)
59
59
 
60
60
  assistant = Assistant.new(@configuration, job)
61
61
  assistant.assist
62
62
 
63
63
  assistant_group.add(assistant)
64
- rescue => e
64
+ rescue StandardError => e
65
+ handle_exception(e)
66
+ end
67
+
68
+ def handle_exception(error)
69
+ log_file.error error
70
+ @configuration.exception_handler.call(error)
71
+ rescue StandardError => e
65
72
  log_file.error e
66
- @configuration.exception_handler.call(e)
67
73
  end
68
74
 
69
75
  def daemonize
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Assistor
2
4
  class LogFile
3
5
  extend Forwardable
@@ -6,6 +8,7 @@ module Assistor
6
8
 
7
9
  def initialize(log_file)
8
10
  @log_file = log_file
11
+ create_private_file
9
12
  @logger = Logger.new(log_file)
10
13
  end
11
14
 
@@ -14,5 +17,12 @@ module Assistor
14
17
  $stdout.reopen $stderr
15
18
  $stdout.sync = $stderr.sync = true
16
19
  end
20
+
21
+ private
22
+
23
+ def create_private_file
24
+ flags = File::WRONLY | File::APPEND | File::CREAT | File::NOFOLLOW
25
+ File.open(@log_file, flags, 0o600) { nil }
26
+ end
17
27
  end
18
28
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Assistor
2
4
  class PidFile
3
5
  def initialize(pid_file)
@@ -5,11 +7,14 @@ module Assistor
5
7
  end
6
8
 
7
9
  def create(pid)
8
- File.write @pid_file, pid
10
+ flags = File::WRONLY | File::CREAT | File::TRUNC | File::NOFOLLOW
11
+ File.open(@pid_file, flags, 0o600) { |file| file.write(pid) }
9
12
  end
10
13
 
11
14
  def delete
12
15
  File.delete @pid_file
16
+ rescue Errno::ENOENT
17
+ nil
13
18
  end
14
19
 
15
20
  def exist?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Assistor
2
- VERSION = '0.2.0'.freeze
4
+ VERSION = '0.3.0'
3
5
  end
data/lib/assistor.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'logger'
2
4
  require 'tmpdir'
3
5
  require 'forwardable'
metadata CHANGED
@@ -1,51 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assistor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Kowalewski
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-05-25 00:00:00.000000000 Z
10
+ date: 2026-11-20 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
- name: bundler
13
+ name: logger
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
16
  - - ">="
18
17
  - !ruby/object:Gem::Version
19
18
  version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rspec
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
19
+ type: :runtime
49
20
  prerelease: false
50
21
  version_requirements: !ruby/object:Gem::Requirement
51
22
  requirements:
@@ -76,8 +47,8 @@ files:
76
47
  homepage: https://github.com/tkowalewski/assistor
77
48
  licenses:
78
49
  - MIT
79
- metadata: {}
80
- post_install_message:
50
+ metadata:
51
+ rubygems_mfa_required: 'true'
81
52
  rdoc_options: []
82
53
  require_paths:
83
54
  - lib
@@ -85,15 +56,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
56
  requirements:
86
57
  - - ">="
87
58
  - !ruby/object:Gem::Version
88
- version: '0'
59
+ version: '3.2'
89
60
  required_rubygems_version: !ruby/object:Gem::Requirement
90
61
  requirements:
91
62
  - - ">="
92
63
  - !ruby/object:Gem::Version
93
64
  version: '0'
94
65
  requirements: []
95
- rubygems_version: 3.1.2
96
- signing_key:
66
+ rubygems_version: 3.6.2
97
67
  specification_version: 4
98
68
  summary: Simple background processing framework for Ruby
99
69
  test_files: []