Skip to content

Commit 1af3328

Browse files
committed
Use zeitwerk to load gem
1 parent e84eba0 commit 1af3328

File tree

12 files changed

+60
-81
lines changed

12 files changed

+60
-81
lines changed

crono.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ Gem::Specification.new do |s|
3030
s.bindir = 'exe'
3131
s.executables = ['crono']
3232

33+
s.add_dependency 'optparse'
3334
s.add_dependency 'rails', '>= 7.0'
35+
s.add_dependency 'zeitwerk'
3436
end

exe/crono

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
11
#!/usr/bin/env ruby
22
# frozen_string_literal: true
33

4-
require 'crono/cli'
5-
6-
def integrate_with_systemd(cli) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
7-
return unless ENV['NOTIFY_SOCKET']
8-
9-
Crono.logger.info 'Enabling systemd notification integration'
10-
11-
cli.config.on(:startup) do
12-
Crono.logger.debug 'Emit systemd startup event'
13-
Crono::SdNotify.ready
14-
end
15-
16-
cli.config.on(:shutdown) do
17-
Crono.logger.debug 'Emit systemd shutdown event'
18-
Crono::SdNotify.stopping
19-
end
20-
21-
if Crono::SdNotify.watchdog? # rubocop:disable Style/GuardClause
22-
Crono.logger.debug 'Start systemd watchdog'
23-
Crono.start_watchdog
24-
end
25-
end
4+
require_relative '../lib/crono'
265

276
# rubocop:disable Style/StderrPuts
287
begin
298
cli = Crono::CLI.instance
309
cli.parse
31-
32-
integrate_with_systemd(cli)
33-
10+
cli.integrate_with_systemd
3411
cli.run
3512
rescue => e
3613
raise e if $DEBUG

lib/crono.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# frozen_string_literal: true
22

3+
# require ruby dependencies
4+
require 'optparse'
5+
require 'socket'
6+
require 'stringio'
7+
require 'logger'
8+
39
# require external dependencies
410
require 'active_record'
511
require 'active_support/all'
6-
require 'socket'
12+
require 'zeitwerk'
713

8-
# require internal dependencies
9-
require_relative 'crono/util'
10-
require_relative 'crono/config'
11-
require_relative 'crono/cronotab'
12-
require_relative 'crono/launcher'
13-
require_relative 'crono/performer_proxy'
14-
require_relative 'crono/scheduler'
15-
require_relative 'crono/sd_notify'
16-
require_relative 'crono/version'
14+
# load zeitwerk
15+
Zeitwerk::Loader.for_gem.tap do |loader|
16+
loader.ignore("#{__dir__}/generators")
17+
loader.inflector.inflect('cli' => 'CLI')
18+
loader.setup
19+
end
1720

1821
module Crono
1922
require_relative 'crono/engine' if defined?(Rails)

lib/crono/cli.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
Thread.abort_on_exception = true
44

5-
# require external dependencies
6-
require 'optparse'
7-
8-
# require ourself
9-
require 'crono'
10-
115
module Crono
126
# Crono::CLI - The main class for the crono daemon exacutable `bin/crono`
13-
class CLI
7+
class CLI # rubocop:disable Metrics/ClassLength
148
include Singleton
159
include Util
1610

@@ -65,6 +59,28 @@ def run # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
6559
end
6660

6761

62+
def integrate_with_systemd # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
63+
return unless ENV['NOTIFY_SOCKET']
64+
65+
Crono.logger.info 'Enabling systemd notification integration'
66+
67+
cli.config.on(:startup) do
68+
Crono.logger.debug 'Emit systemd startup event'
69+
Crono::SdNotify.ready
70+
end
71+
72+
cli.config.on(:shutdown) do
73+
Crono.logger.debug 'Emit systemd shutdown event'
74+
Crono::SdNotify.stopping
75+
end
76+
77+
if Crono::SdNotify.watchdog? # rubocop:disable Style/GuardClause
78+
Crono.logger.debug 'Start systemd watchdog'
79+
Crono.start_watchdog
80+
end
81+
end
82+
83+
6884
private
6985

7086

lib/crono/interval.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# frozen_string_literal: true
22

3-
# require internal dependencies
4-
require_relative 'time_of_day'
5-
63
module Crono
74
# Interval describes a period between two specific times of day
85
class Interval

lib/crono/job.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
# frozen_string_literal: true
22

3-
# require external dependencies
4-
require 'stringio'
5-
require 'logger'
6-
73
module Crono
84
# Crono::Job represents a Crono job
95
class Job # rubocop:disable Metrics/ClassLength

lib/crono/performer_proxy.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
# frozen_string_literal: true
22

3-
# require internal dependencies
4-
require_relative 'job'
5-
require_relative 'period'
6-
73
module Crono
84
# Crono::PerformerProxy is a proxy used in cronotab.rb semantic
95
class PerformerProxy

lib/crono/period.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# frozen_string_literal: true
22

3-
# require internal dependencies
4-
require_relative 'interval'
5-
63
module Crono
74
# Period describe frequency of jobs
85
class Period

lib/crono/sd_notify.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
# The only changes made was "rehoming" it within the Crono module to avoid
2626
# namespace collisions and applying standard's code formatting style.
2727

28-
require 'socket'
29-
3028
# SdNotify is a pure-Ruby implementation of sd_notify(3). It can be used to
3129
# notify systemd about state changes. Methods of this package are no-op on
3230
# non-systemd systems (eg. Darwin).

spec/crono/assets/bad_cronotab.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
# This is an example of a bad cronotab for tests
44

5-
class TestJob
6-
def perform
7-
puts 'Test!'
8-
end
9-
end
10-
115
# This is an error, because you can use `on` options with
126
# a period less than 7 days.
137

0 commit comments

Comments
 (0)