sidekiq-runner 0.1.3 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: bc538d49e4cd0c3dbd72dd4b32d124c427fe60c1
4
- data.tar.gz: e9753e622a05176942872caa99ea4397fe3d9ff2
2
+ SHA256:
3
+ metadata.gz: f1e949ab41ecd2f73ccc9e85385d55f9f3ff628fa8bb8f394e6b03adbe4c99ed
4
+ data.tar.gz: 4414b3247310b053ca7794cbe904f53ede34fbc29bb57a540097e6721cbd1cc9
5
5
  SHA512:
6
- metadata.gz: d511179b7968501b3a169969fcc3c53fc56950e8b1ef18ac9181f55d6a3b1921e78d5bad422f9cb00f465efef103c37fb0ea76e0abeaa4ffe7dff1016a01db8b
7
- data.tar.gz: 7203ffee3f9de1de0ff87871bdfd8b0c419b6dfa4b58b92f8fb6da58478a1e58891b34eef1f5b06934a6ca1c4f9f5514945c5dc36d09d45885a975f4cb3e32f4
6
+ metadata.gz: d6d2338d2396c0472603fbf456088afa8199379702e86333aa481ad07651eb4fb294248975a2c096e235e22e6bc24968e2d454423c000d1c1f620c9ed67f9c75
7
+ data.tar.gz: c7b8f252bf491f272e118399bf95ae58f93293b321ce35e54dd8e7a7c894706ca0281391a9f779bad7d1de8a52157266d242eddc5cd1cff8f2c73c69163e9ed9
@@ -17,7 +17,7 @@ module SidekiqRunner
17
17
 
18
18
  abort 'God is already running.' if god_alive?(god_config)
19
19
 
20
- run(:start, sidekiq_config) do
20
+ run(:start, sidekiq_config, god_config) do
21
21
  $0 = "SidekiqRunner/God (#{god_config.process_name})"
22
22
 
23
23
  puts 'Starting god.'
@@ -31,7 +31,7 @@ module SidekiqRunner
31
31
  def self.stop
32
32
  sidekiq_config, god_config = SidekiqConfiguration.get, GodConfiguration.get
33
33
 
34
- run(:stop, sidekiq_config) do
34
+ run(:stop, sidekiq_config, god_config) do
35
35
  God::EventHandler.load
36
36
 
37
37
  if god_alive?(god_config)
@@ -48,6 +48,17 @@ module SidekiqRunner
48
48
  end
49
49
  end
50
50
 
51
+ def self.restart(sidekiq_instances: [])
52
+ sidekiq_config = SidekiqConfiguration.get
53
+ god_config = GodConfiguration.get
54
+
55
+ return unless god_alive?(god_config)
56
+
57
+ sidekiq_config.each_key do |name|
58
+ God::CLI::Command.new('restart', god_config.options, ['', name]) if sidekiq_instances.empty? || sidekiq_instances.include?(name.to_sym)
59
+ end
60
+ end
61
+
51
62
  def self.running?
52
63
  god_alive? GodConfiguration.get
53
64
  end
@@ -69,7 +80,7 @@ module SidekiqRunner
69
80
  true
70
81
  end
71
82
 
72
- def self.run(action, sidekiq_config)
83
+ def self.run(action, sidekiq_config, god_config)
73
84
  begin
74
85
 
75
86
  # Use this flag to actually load all of the god infrastructure.
@@ -77,16 +88,21 @@ module SidekiqRunner
77
88
  require 'god'
78
89
  require 'god/cli/run'
79
90
 
91
+ if [:start, :stop].include? action
92
+ cb = god_config.send("before_#{action}_cb".to_sym)
93
+ cb.call if cb
94
+ end
95
+
80
96
  # Peform the action.
81
97
  yield if block_given?
82
98
 
83
99
  rescue SystemExit => e
84
- cb = e.success? ? "#{action}_success_cb" : "#{action}_error_cb"
100
+ sidekiq_cb = e.success? ? "#{action}_success_cb" : "#{action}_error_cb"
85
101
  ensure
86
102
  if [:start, :stop].include? action
87
- cb = "#{action}_success_cb" unless cb
88
- cb = sidekiq_config.send(cb.to_sym)
89
- cb.call if cb
103
+ sidekiq_cb = "#{action}_success_cb" unless sidekiq_cb
104
+ sidekiq_cb = sidekiq_config.send(sidekiq_cb.to_sym)
105
+ sidekiq_cb.call if sidekiq_cb
90
106
  end
91
107
  end
92
108
  end
@@ -15,9 +15,11 @@ module SidekiqRunner
15
15
  RUNNER_ATTRIBUTES = [:config_file, :daemonize, :port, :syslog, :events]
16
16
  RUNNER_ATTRIBUTES.each { |att| attr_accessor att }
17
17
 
18
- CONFIG_FILE_ATTRIBUTES = [:process_name, :interval, :stop_timeout, :log_file, :maximum_memory_usage]
18
+ CONFIG_FILE_ATTRIBUTES = [:process_name, :interval, :stop_timeout, :log_file, :log_level, :maximum_memory_usage, :pid]
19
19
  CONFIG_FILE_ATTRIBUTES.each { |att| attr_accessor att }
20
20
 
21
+ attr_reader :generic_watchers
22
+
21
23
  def initialize
22
24
  @process_name = 'sidekiq'
23
25
  @interval = 30
@@ -30,10 +32,18 @@ module SidekiqRunner
30
32
  @daemonize = true
31
33
  @syslog = true
32
34
  @events = true
35
+ @pid = nil
36
+ @log_level = :warn
33
37
 
34
- # This is going to be a part of the .sock file name e.g. "/tmp/god.17165.sock"
38
+ # This is going to be a part of the .sock file name e.g. "/tmp/god.17165.sock" and the pidfile name
35
39
  # Change this in the configuration file to be able to run multiple instances of god.
36
40
  @port = 17165
41
+
42
+ @generic_watchers = []
43
+ end
44
+
45
+ def add_generic(&blk)
46
+ @generic_watchers << blk
37
47
  end
38
48
 
39
49
  def options
@@ -45,7 +55,9 @@ module SidekiqRunner
45
55
  syslog: @syslog,
46
56
  events: @events,
47
57
  config: File.expand_path("../sidekiq.god", __FILE__),
48
- log: @log_file
58
+ log: @log_file,
59
+ pid: @pid,
60
+ log_level: @log_level
49
61
  }
50
62
  end
51
63
 
@@ -60,7 +72,15 @@ module SidekiqRunner
60
72
  end
61
73
 
62
74
  def create_directories!
63
- FileUtils.mkdir_p(File.dirname(log_file))
75
+ FileUtils.mkdir_p(File.dirname(log_file)) if log_file
76
+ end
77
+
78
+ %w(start stop).each do |action|
79
+ attr_reader "before_#{action}_cb".to_sym
80
+
81
+ define_method("before_#{action}") do |&block|
82
+ instance_variable_set("@before_#{action}_cb".to_sym, block)
83
+ end
64
84
  end
65
85
  end
66
86
  end
@@ -6,6 +6,12 @@ god_config = SidekiqRunner::GodConfiguration.get
6
6
 
7
7
  God.terminate_timeout = god_config.stop_timeout + 10
8
8
 
9
+ god_config.generic_watchers.each do |block|
10
+ God.watch do |w|
11
+ block.call(w)
12
+ end
13
+ end
14
+
9
15
  sidekiq_config.each do |name, skiq|
10
16
  God.watch do |w|
11
17
  w.name = name
@@ -13,13 +19,15 @@ sidekiq_config.each do |name, skiq|
13
19
  # Set start command.
14
20
  w.start = skiq.build_start_command
15
21
 
22
+ # Set logfile
23
+ w.log = skiq.logfile
24
+
16
25
  # Set stop command.
17
- w.stop = skiq.build_stop_command(god_config.stop_timeout)
18
26
  w.stop_timeout = god_config.stop_timeout
19
27
 
20
- # Make sure the pidfile is deleted as sidekiqctl does not delete stale pidfiles.
21
- w.pid_file = skiq.pidfile
22
- w.behavior(:clean_pid_file)
28
+ # Set uid/gid if requested.
29
+ w.uid = skiq.uid if skiq.uid
30
+ w.gid = skiq.gid if skiq.gid
23
31
 
24
32
  # Working directory has to be set properly.
25
33
  # Be aware that by default, God sets the working directory to / (root dir).
@@ -72,6 +80,10 @@ sidekiq_config.each do |name, skiq|
72
80
  end
73
81
  end
74
82
 
83
+ if skiq.config_blocks.length > 0
84
+ skiq.config_blocks.each { |blk| blk.call(w) }
85
+ end
86
+
75
87
  w.lifecycle do |on|
76
88
  on.condition(:flapping) do |c|
77
89
  c.to_state = [:start, :restart] # If this watch is started or restarted...
@@ -12,7 +12,7 @@ module SidekiqRunner
12
12
  def initialize
13
13
  @config_file =
14
14
  if defined?(Rails)
15
- File.join(Rails.root, 'config', 'sidekiq.yml')
15
+ File.join(Rails.root, 'config', 'sidekiq.yml')
16
16
  else
17
17
  File.join(Dir.pwd, 'config', 'sidekiq.yml')
18
18
  end
@@ -86,8 +86,6 @@ module SidekiqRunner
86
86
  end
87
87
 
88
88
  def merge_config_file!
89
- sidekiqs_common_config = {}
90
-
91
89
  yml = File.exist?(config_file) ? YAML.load_file(config_file) : {}
92
90
  yml = Hash[yml.map { |k, v| [k.to_sym, v] }]
93
91
 
@@ -96,8 +94,6 @@ module SidekiqRunner
96
94
 
97
95
  def sane?
98
96
  fail 'No sidekiq instances defined. Nothing to run.' if @sidekiqs.empty?
99
- fail 'Sidekiq instances with the same pidfile found.' if @sidekiqs.values.map(&:pidfile).uniq.size < @sidekiqs.size
100
- fail 'Sidekiq instances with the same logfile found.' if @sidekiqs.values.map(&:logfile).uniq.size < @sidekiqs.size
101
97
 
102
98
  @sidekiqs.each_value { |skiq| skiq.sane? }
103
99
  end
@@ -1,16 +1,15 @@
1
1
  module SidekiqRunner
2
2
  class SidekiqInstance
3
-
4
3
  RUNNER_ATTRIBUTES = [:bundle_env, :chdir, :requirefile]
5
4
  RUNNER_ATTRIBUTES.each { |att| attr_accessor att }
6
5
 
7
- CONFIG_FILE_ATTRIBUTES = [:concurrency, :verbose, :pidfile, :logfile, :tag, :rbtrace]
6
+ CONFIG_FILE_ATTRIBUTES = [:concurrency, :verbose, :pidfile, :logfile, :tag, :rbtrace, :uid, :gid]
8
7
  CONFIG_FILE_ATTRIBUTES.each { |att| attr_accessor att }
9
8
 
10
- attr_reader :name, :queues
9
+ attr_reader :name, :queues, :config_blocks
11
10
 
12
11
  def initialize(name)
13
- fail "No sidekiq instance name given!" if name.empty?
12
+ raise "No sidekiq instance name given!" if name.empty?
14
13
 
15
14
  @name = name
16
15
  @queues = []
@@ -24,15 +23,23 @@ module SidekiqRunner
24
23
  @verbose = false
25
24
  @tag = name
26
25
  @rbtrace = false
26
+ @uid = nil
27
+ @gid = nil
28
+ @config_blocks = []
27
29
  end
28
30
 
29
31
  def add_queue(queue_name, weight = 1)
30
- fail "Cannot add the queue. The name is empty!" if queue_name.empty?
31
- fail "Cannot add the queue. The weight is not an integer!" unless weight.is_a? Integer
32
- fail "Cannot add the queue. The queue with \"#{queue_name}\" name already exist" if @queues.any? { |q| q.first == queue_name }
32
+ raise "Cannot add the queue. The name is empty!" if queue_name.empty?
33
+ raise "Cannot add the queue. The weight is not an integer!" unless weight.is_a? Integer
34
+ raise "Cannot add the queue. The queue with \"#{queue_name}\" name already exist" if @queues.any? { |q| q.first == queue_name }
35
+
33
36
  @queues << [queue_name, weight]
34
37
  end
35
38
 
39
+ def god_config(&block)
40
+ @config_blocks << block
41
+ end
42
+
36
43
  def merge_config_file!(yml)
37
44
  # Get global configuration options.
38
45
  SidekiqInstance::CONFIG_FILE_ATTRIBUTES.each do |k|
@@ -40,7 +47,7 @@ module SidekiqRunner
40
47
  end
41
48
 
42
49
  # Override with instance-specific options.
43
- if (syml = yml[@name.to_sym]) && (syml.is_a?(Hash))
50
+ if (syml = yml[@name.to_sym]) && syml.is_a?(Hash)
44
51
  syml = Hash[syml.map { |k, v| [k.to_sym, v] }]
45
52
 
46
53
  SidekiqInstance::CONFIG_FILE_ATTRIBUTES.each do |k|
@@ -50,8 +57,8 @@ module SidekiqRunner
50
57
  end
51
58
 
52
59
  def sane?
53
- fail "No queues given for #{@name}!" if @queues.empty?
54
- fail "No requirefile given for #{@name} and not in Rails environment!" if !defined?(Rails) && !requirefile
60
+ raise "No queues given for #{@name}!" if @queues.empty?
61
+ raise "No requirefile given for #{@name} and not in Rails environment!" if !defined?(Rails) && !requirefile
55
62
  end
56
63
 
57
64
  def build_start_command
@@ -59,33 +66,21 @@ module SidekiqRunner
59
66
 
60
67
  cmd = []
61
68
  cmd << 'bundle exec' if bundle_env
62
- cmd << (rbtrace ? File.expand_path('../../../script/sidekiq_rbtrace', __FILE__) : 'sidekiq')
63
- cmd << '-d'
69
+ cmd << (rbtrace ? File.expand_path('../../script/sidekiq_rbtrace', __dir__) : 'sidekiq')
64
70
  cmd << "-c #{concurrency}"
65
71
  cmd << '-v' if verbose
66
- cmd << "-L #{logfile}"
67
72
  cmd << "-P #{pidfile}"
68
73
  cmd << "-e #{Rails.env}" if defined?(Rails)
69
74
  cmd << "-r #{requirefile}" if requirefile
70
75
  cmd << "-g '#{tag}'"
71
76
 
72
77
  queues.each do |q, w|
73
- cmd << "-q #{q},#{w.to_s}"
78
+ cmd << "-q #{q},#{w}"
74
79
  end
75
80
 
76
81
  cmd.join(' ')
77
82
  end
78
83
 
79
- def build_stop_command(timeout)
80
- cmd = []
81
- cmd << (bundle_env ? 'bundle exec sidekiqctl' : 'sidekiqctl')
82
- cmd << 'stop'
83
- cmd << pidfile
84
- cmd << timeout
85
-
86
- cmd.join(' ')
87
- end
88
-
89
84
  private
90
85
 
91
86
  def create_directories!
@@ -1,3 +1,3 @@
1
1
  module SidekiqRunner
2
- VERSION = '0.1.3'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - FlavourSys Technology GmbH
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-17 00:00:00.000000000 Z
11
+ date: 2021-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- version_requirements: !ruby/object:Gem::Requirement
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
15
16
  requirements:
16
- - - "~>"
17
+ - - ">="
17
18
  - !ruby/object:Gem::Version
18
- version: '10.3'
19
- name: rake
19
+ version: '0'
20
+ type: :runtime
20
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: god
21
29
  requirement: !ruby/object:Gem::Requirement
22
30
  requirements:
23
31
  - - "~>"
24
32
  - !ruby/object:Gem::Version
25
- version: '10.3'
33
+ version: '0.13'
26
34
  type: :runtime
27
- - !ruby/object:Gem::Dependency
35
+ prerelease: false
28
36
  version_requirements: !ruby/object:Gem::Requirement
29
37
  requirements:
30
38
  - - "~>"
31
39
  - !ruby/object:Gem::Version
32
40
  version: '0.13'
33
- name: god
34
- prerelease: false
41
+ - !ruby/object:Gem::Dependency
42
+ name: sidekiq
35
43
  requirement: !ruby/object:Gem::Requirement
36
44
  requirements:
37
45
  - - "~>"
38
46
  - !ruby/object:Gem::Version
39
- version: '0.13'
47
+ version: '6.0'
40
48
  type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '6.0'
41
55
  description: Provide an easy way to configure, start, and monitor all your Sidekiq
42
56
  processes
43
57
  email: technology@flavoursys.com
@@ -73,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
87
  version: '0'
74
88
  requirements: []
75
89
  rubyforge_project:
76
- rubygems_version: 2.4.5
90
+ rubygems_version: 2.7.6.2
77
91
  signing_key:
78
92
  specification_version: 4
79
93
  summary: Sidekiq configuration and rake tasks