sneakers 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +1 -3
- data/lib/sneakers.rb +27 -6
- data/lib/sneakers/cli.rb +5 -6
- data/lib/sneakers/runner.rb +56 -1
- data/lib/sneakers/version.rb +1 -1
- data/spec/sneakers/cli_spec.rb +11 -1
- data/spec/sneakers/sneakers_spec.rb +66 -0
- metadata +4 -4
- data/lib/sneakers/runner_config.rb +0 -55
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -84,13 +84,11 @@ Let's test it out quickly from the command line:
|
|
84
84
|
|
85
85
|
|
86
86
|
```bash
|
87
|
-
sneakers work Processor --require boot.rb
|
87
|
+
sneakers work Processor --require boot.rb
|
88
88
|
```
|
89
89
|
|
90
90
|
We just told Sneakers to spawn a worker named `Processor`, but first `--require` a file that we dedicate to setting up environment, including workers and what-not.
|
91
91
|
|
92
|
-
For simplicity, we also told Sneakers to *not* daemonize and work at the `--front`.
|
93
|
-
|
94
92
|
If you go to your RabbitMQ admin now, you'll see a new queue named `logs` was created. Push a couple messages, and this is the output you should see at your terminal.
|
95
93
|
|
96
94
|
|
data/lib/sneakers.rb
CHANGED
@@ -20,14 +20,14 @@ require 'sneakers/publisher'
|
|
20
20
|
|
21
21
|
module Sneakers
|
22
22
|
|
23
|
-
|
23
|
+
DEFAULTS = {
|
24
24
|
# runner
|
25
25
|
:runner_config_file => nil,
|
26
26
|
:metrics => nil,
|
27
|
-
:daemonize =>
|
27
|
+
:daemonize => false,
|
28
28
|
:start_worker_delay => 0.2,
|
29
29
|
:workers => 4,
|
30
|
-
:log =>
|
30
|
+
:log => STDOUT,
|
31
31
|
:pid_path => 'sneakers.pid',
|
32
32
|
|
33
33
|
#workers
|
@@ -43,7 +43,9 @@ module Sneakers
|
|
43
43
|
:exchange => 'sneakers',
|
44
44
|
:exchange_type => :direct,
|
45
45
|
:hooks => {}
|
46
|
-
}
|
46
|
+
}.freeze
|
47
|
+
|
48
|
+
Config = DEFAULTS.dup
|
47
49
|
|
48
50
|
def self.configure(opts={})
|
49
51
|
# worker > userland > defaults
|
@@ -55,6 +57,21 @@ module Sneakers
|
|
55
57
|
@configured = true
|
56
58
|
end
|
57
59
|
|
60
|
+
def self.clear!
|
61
|
+
Config.clear
|
62
|
+
Config.merge!(DEFAULTS.dup)
|
63
|
+
@logger = nil
|
64
|
+
@publisher = nil
|
65
|
+
@configured = false
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.daemonize!(loglevel=Logger::INFO)
|
69
|
+
Config[:log] = 'sneakers.log'
|
70
|
+
Config[:daemonize] = true
|
71
|
+
setup_general_logger!
|
72
|
+
logger.level = loglevel
|
73
|
+
end
|
74
|
+
|
58
75
|
def self.logger
|
59
76
|
@logger
|
60
77
|
end
|
@@ -71,8 +88,12 @@ module Sneakers
|
|
71
88
|
private
|
72
89
|
|
73
90
|
def self.setup_general_logger!
|
74
|
-
|
75
|
-
|
91
|
+
if [:info, :debug, :error, :warn].all?{ |meth| Config[:log].respond_to?(meth) }
|
92
|
+
@logger = Config[:log]
|
93
|
+
else
|
94
|
+
@logger = Logger.new(Config[:log])
|
95
|
+
@logger.formatter = Sneakers::Support::ProductionFormatter
|
96
|
+
end
|
76
97
|
end
|
77
98
|
|
78
99
|
def self.setup_worker_concerns!
|
data/lib/sneakers/cli.rb
CHANGED
@@ -24,20 +24,19 @@ module Sneakers
|
|
24
24
|
BANNER = SNEAKERS
|
25
25
|
|
26
26
|
method_option :debug
|
27
|
-
method_option :
|
27
|
+
method_option :daemonize
|
28
28
|
method_option :require
|
29
29
|
|
30
30
|
desc "work FirstWorker,SecondWorker ... ,NthWorker", "Run workers"
|
31
31
|
def work(workers)
|
32
32
|
opts = {
|
33
|
-
:daemonize =>
|
33
|
+
:daemonize => !!options[:daemonize]
|
34
34
|
}
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
|
36
|
+
opts[:log] = opts[:daemonize] ? 'sneakers.log' : STDOUT
|
37
|
+
|
38
38
|
|
39
39
|
Sneakers.configure(opts)
|
40
|
-
puts Sneakers::Config
|
41
40
|
|
42
41
|
require_boot File.expand_path(options[:require]) if options[:require]
|
43
42
|
|
data/lib/sneakers/runner.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'serverengine'
|
2
2
|
require 'sneakers/workergroup'
|
3
|
-
require 'sneakers/runner_config'
|
4
3
|
|
5
4
|
module Sneakers
|
6
5
|
class Runner
|
@@ -17,4 +16,60 @@ module Sneakers
|
|
17
16
|
@se.stop
|
18
17
|
end
|
19
18
|
end
|
19
|
+
|
20
|
+
|
21
|
+
class RunnerConfig
|
22
|
+
def method_missing(meth, *args, &block)
|
23
|
+
if %w{ before_fork after_fork }.include? meth.to_s
|
24
|
+
@conf[meth] = block
|
25
|
+
elsif %w{ workers start_worker_delay amqp }.include? meth.to_s
|
26
|
+
@conf[meth] = args.first
|
27
|
+
else
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize(worker_classes)
|
33
|
+
@worker_classes = worker_classes
|
34
|
+
@conf = {}
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_h
|
38
|
+
@conf
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def reload_config!
|
43
|
+
Sneakers.logger.warn("Loading runner configuration...")
|
44
|
+
config_file = Sneakers::Config[:runner_config_file]
|
45
|
+
|
46
|
+
if config_file
|
47
|
+
begin
|
48
|
+
instance_eval(File.read(config_file), config_file)
|
49
|
+
Sneakers.logger.info("Loading config with file: #{config_file}")
|
50
|
+
rescue
|
51
|
+
Sneakers.logger.error("Cannot load from file '#{config_file}', #{$!}")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
config = make_serverengine_config
|
56
|
+
|
57
|
+
[:before_fork, :after_fork].each do | hook |
|
58
|
+
Sneakers::Config[:hooks][hook] = config.delete(hook) if config[hook]
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
Sneakers.logger.info("New configuration: #{config.inspect}")
|
63
|
+
config
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
def make_serverengine_config
|
68
|
+
Sneakers::Config.merge(@conf).merge({
|
69
|
+
:worker_type => 'process',
|
70
|
+
:worker_classes => @worker_classes
|
71
|
+
})
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
20
75
|
end
|
data/lib/sneakers/version.rb
CHANGED
data/spec/sneakers/cli_spec.rb
CHANGED
@@ -35,12 +35,22 @@ describe Sneakers::CLI do
|
|
35
35
|
out = capture_io{ Sneakers::CLI.start [
|
36
36
|
'work',
|
37
37
|
"TitleScraper",
|
38
|
-
"--front",
|
39
38
|
"--require=#{File.expand_path('../fixtures/require_worker.rb', File.dirname(__FILE__))}"
|
40
39
|
]}.join ''
|
41
40
|
|
42
41
|
out.must_match(/Log.*Console/)
|
43
42
|
end
|
43
|
+
|
44
|
+
it "should be able to run as daemonized process" do
|
45
|
+
out = capture_io{ Sneakers::CLI.start [
|
46
|
+
'work',
|
47
|
+
"TitleScraper",
|
48
|
+
"--daemonize",
|
49
|
+
"--require=#{File.expand_path('../fixtures/require_worker.rb', File.dirname(__FILE__))}"
|
50
|
+
]}.join ''
|
51
|
+
|
52
|
+
out.must_match(/sneakers.log/)
|
53
|
+
end
|
44
54
|
end
|
45
55
|
|
46
56
|
it "should fail when no workers found" do
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sneakers'
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
describe Sneakers do
|
7
|
+
before do
|
8
|
+
Sneakers.clear!
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'self' do
|
12
|
+
it 'should have defaults set up' do
|
13
|
+
Sneakers::Config[:log].must_equal(STDOUT)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should configure itself' do
|
17
|
+
Sneakers.configure
|
18
|
+
Sneakers.logger.wont_be_nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '.daemonize!' do
|
23
|
+
it 'should set a logger to a default info level and not daemonize' do
|
24
|
+
Sneakers.daemonize!
|
25
|
+
Sneakers::Config[:log].must_equal('sneakers.log')
|
26
|
+
Sneakers::Config[:daemonize].must_equal(true)
|
27
|
+
Sneakers.logger.level.must_equal(Logger::INFO)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should set a logger to a level given that level' do
|
31
|
+
Sneakers.daemonize!(Logger::DEBUG)
|
32
|
+
Sneakers.logger.level.must_equal(Logger::DEBUG)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '.clear!' do
|
37
|
+
it 'must reset dirty configuration to default' do
|
38
|
+
Sneakers::Config[:log].must_equal(STDOUT)
|
39
|
+
Sneakers.configure(:log => 'foobar.log')
|
40
|
+
Sneakers::Config[:log].must_equal('foobar.log')
|
41
|
+
Sneakers.clear!
|
42
|
+
Sneakers::Config[:log].must_equal(STDOUT)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
describe '#setup_general_logger' do
|
48
|
+
it 'should detect a string and configure a logger' do
|
49
|
+
Sneakers.configure(:log => 'sneakers.log')
|
50
|
+
Sneakers.logger.kind_of?(Logger).must_equal(true)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should detect a file-like thing and configure a logger' do
|
54
|
+
Sneakers.configure(:log => STDOUT)
|
55
|
+
Sneakers.logger.kind_of?(Logger).must_equal(true)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should detect an actual logger and configure it' do
|
59
|
+
logger = Logger.new(STDOUT)
|
60
|
+
Sneakers.configure(:log => logger)
|
61
|
+
Sneakers.logger.must_equal(logger)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sneakers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-11-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: serverengine
|
@@ -236,7 +236,6 @@ files:
|
|
236
236
|
- lib/sneakers/publisher.rb
|
237
237
|
- lib/sneakers/queue.rb
|
238
238
|
- lib/sneakers/runner.rb
|
239
|
-
- lib/sneakers/runner_config.rb
|
240
239
|
- lib/sneakers/support/production_formatter.rb
|
241
240
|
- lib/sneakers/support/queue_name.rb
|
242
241
|
- lib/sneakers/support/utils.rb
|
@@ -251,6 +250,7 @@ files:
|
|
251
250
|
- spec/sneakers/concerns/metrics.rb
|
252
251
|
- spec/sneakers/publisher_spec.rb
|
253
252
|
- spec/sneakers/queue_spec.rb
|
253
|
+
- spec/sneakers/sneakers_spec.rb
|
254
254
|
- spec/sneakers/worker_spec.rb
|
255
255
|
- spec/spec_helper.rb
|
256
256
|
homepage: ''
|
@@ -284,6 +284,6 @@ test_files:
|
|
284
284
|
- spec/sneakers/concerns/metrics.rb
|
285
285
|
- spec/sneakers/publisher_spec.rb
|
286
286
|
- spec/sneakers/queue_spec.rb
|
287
|
+
- spec/sneakers/sneakers_spec.rb
|
287
288
|
- spec/sneakers/worker_spec.rb
|
288
289
|
- spec/spec_helper.rb
|
289
|
-
has_rdoc:
|
@@ -1,55 +0,0 @@
|
|
1
|
-
module Sneakers
|
2
|
-
class RunnerConfig
|
3
|
-
def method_missing(meth, *args, &block)
|
4
|
-
if %w{ before_fork after_fork }.include? meth.to_s
|
5
|
-
@conf[meth] = block
|
6
|
-
elsif %w{ workers start_worker_delay amqp }.include? meth.to_s
|
7
|
-
@conf[meth] = args.first
|
8
|
-
else
|
9
|
-
super
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize(worker_classes)
|
14
|
-
@worker_classes = worker_classes
|
15
|
-
@conf = {}
|
16
|
-
end
|
17
|
-
|
18
|
-
def to_h
|
19
|
-
@conf
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
def reload_config!
|
24
|
-
Sneakers.logger.warn("Loading runner configuration...")
|
25
|
-
config_file = Sneakers::Config[:runner_config_file]
|
26
|
-
|
27
|
-
if config_file
|
28
|
-
begin
|
29
|
-
instance_eval(File.read(config_file), config_file)
|
30
|
-
Sneakers.logger.info("Loading config with file: #{config_file}")
|
31
|
-
rescue
|
32
|
-
Sneakers.logger.error("Cannot load from file '#{config_file}', #{$!}")
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
config = make_serverengine_config
|
37
|
-
|
38
|
-
[:before_fork, :after_fork].each do | hook |
|
39
|
-
Sneakers::Config[:hooks][hook] = config.delete(hook) if config[hook]
|
40
|
-
end
|
41
|
-
|
42
|
-
|
43
|
-
Sneakers.logger.info("New configuration: #{config.inspect}")
|
44
|
-
config
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
def make_serverengine_config
|
49
|
-
Sneakers::Config.merge(@conf).merge({
|
50
|
-
:worker_type => 'process',
|
51
|
-
:worker_classes => @worker_classes
|
52
|
-
})
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|