startback 0.11.4 → 0.11.5
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 +4 -4
- data/lib/startback/event/agent.rb +8 -0
- data/lib/startback/event/engine.rb +100 -0
- data/lib/startback/event.rb +2 -0
- data/lib/startback/support/env.rb +41 -0
- data/lib/startback/support/robustness.rb +1 -1
- data/lib/startback/support.rb +1 -0
- data/lib/startback/version.rb +1 -1
- data/spec/unit/support/test_env.rb +75 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa9e440ddba682a1c59c98a5635b0febafe08222e9cee2de68d3262c6da6d8d4
|
4
|
+
data.tar.gz: '09a4748015aa374687c4016341d7f6a699d80718da7f9b4a6449294d69cf7897'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f832ba94f865dfd66c372fcf19623c68f43c76019623fd49da96e88fa2c7355bc78916ae98f3ae32de7e4deb0f08d9ade5d7eb2b3fa9f9bebb5544c0de698d4
|
7
|
+
data.tar.gz: 6ec4e88b7f2bc2ffd58d6e0cddb5bfffc5411149b71a97d1a51e7195105bd907568b45afec40f47916c17dbc45dfe81a5e6432781fa821dbdb2b1ba47f614c86
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'webrick'
|
3
|
+
require 'startback'
|
4
|
+
module Startback
|
5
|
+
class Event
|
6
|
+
#
|
7
|
+
# This class runs an infinite loop using ServerEngine.
|
8
|
+
# It is intended to be used to run jobs that listen to
|
9
|
+
# a Startback Bus instance without having the main process
|
10
|
+
# terminating immediately.
|
11
|
+
#
|
12
|
+
# The Engine automatically runs a Webrick small webapp
|
13
|
+
# with a /healthcheck webservice. The class can be extended
|
14
|
+
# and method `on_health_check` overriden to run specific
|
15
|
+
# checks.
|
16
|
+
#
|
17
|
+
# This class goes hand in hand with the `startback:engine`
|
18
|
+
# docker image.
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
#
|
22
|
+
# # Dockerfile
|
23
|
+
# FROM enspirit/startback:engine-0.11
|
24
|
+
#
|
25
|
+
# # engine.rb
|
26
|
+
# require 'startback/event/engine'
|
27
|
+
# Startback::Event::Engine.run
|
28
|
+
#
|
29
|
+
class Engine
|
30
|
+
|
31
|
+
DEFAULT_OPTIONS = {
|
32
|
+
daemonize: false,
|
33
|
+
worker_type: 'process',
|
34
|
+
workers: 1
|
35
|
+
}
|
36
|
+
|
37
|
+
def initialize
|
38
|
+
require 'serverengine'
|
39
|
+
end
|
40
|
+
|
41
|
+
def on_health_check
|
42
|
+
"Ok"
|
43
|
+
end
|
44
|
+
|
45
|
+
def run(options = {})
|
46
|
+
options = DEFAULT_OPTIONS.merge(options)
|
47
|
+
health = Engine.build_health_check(self)
|
48
|
+
worker = Engine.build_worker(health)
|
49
|
+
se = ServerEngine.create(nil, worker, options)
|
50
|
+
se.run
|
51
|
+
se
|
52
|
+
end
|
53
|
+
|
54
|
+
class << self
|
55
|
+
def run(*args, &bl)
|
56
|
+
new.run(*args, &bl)
|
57
|
+
end
|
58
|
+
|
59
|
+
def build_health_check(engine)
|
60
|
+
Rack::Builder.new do
|
61
|
+
map '/health-check' do
|
62
|
+
health = Startback::Web::HealthCheck.new {
|
63
|
+
engine.on_health_check
|
64
|
+
}
|
65
|
+
run(health)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def build_worker(health)
|
71
|
+
Module.new do
|
72
|
+
include Support::Env
|
73
|
+
|
74
|
+
def initialize
|
75
|
+
@stop_flag = ServerEngine::BlockingFlag.new
|
76
|
+
end
|
77
|
+
|
78
|
+
define_method(:health) do
|
79
|
+
health
|
80
|
+
end
|
81
|
+
|
82
|
+
def run
|
83
|
+
until @stop_flag.set?
|
84
|
+
Rack::Handler::WEBrick.run(health, {
|
85
|
+
:Port => env('STARTBACK_ENGINE_PORT', '3000').to_i,
|
86
|
+
:Host => env('STARTBACK_ENGINE_LISTEN', '0.0.0.0')
|
87
|
+
})
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def stop
|
92
|
+
@stop_flag.set!
|
93
|
+
Rack::Handler::WEBrick.shutdown
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end # class << self
|
98
|
+
end # class Engine
|
99
|
+
end # class Event
|
100
|
+
end # module Startback
|
data/lib/startback/event.rb
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Startback
|
2
|
+
module Support
|
3
|
+
# This method provides the `env` and `env!` methods that
|
4
|
+
# help querying environment variables easily.
|
5
|
+
module Env
|
6
|
+
|
7
|
+
# Returns an environment variable or raise an error if
|
8
|
+
# not set.
|
9
|
+
#
|
10
|
+
# The result is always a String with no leading/trailing
|
11
|
+
# spaces.
|
12
|
+
#
|
13
|
+
# If a block is given, the environment variable is yield
|
14
|
+
# and the result of the block returned.
|
15
|
+
def env!(key, default = nil, &bl)
|
16
|
+
v = ENV[key].to_s.strip
|
17
|
+
raise Startback::Error, "Missing ENV var `#{key}`" if v.empty?
|
18
|
+
|
19
|
+
env(key, default, &bl)
|
20
|
+
end
|
21
|
+
module_function :env!
|
22
|
+
|
23
|
+
# Returns an environment variable or the default value
|
24
|
+
# passed as second argument.
|
25
|
+
#
|
26
|
+
# The result is always a String with no leading/trailing
|
27
|
+
# spaces.
|
28
|
+
#
|
29
|
+
# If a block is given, the environment variable is yield
|
30
|
+
# and the result of the block returned.
|
31
|
+
def env(key, default = nil, &bl)
|
32
|
+
v = ENV[key].to_s.strip
|
33
|
+
v = v.empty? ? default : v
|
34
|
+
v = bl.call(v) if bl && v
|
35
|
+
v
|
36
|
+
end
|
37
|
+
module_function :env
|
38
|
+
|
39
|
+
end # module Env
|
40
|
+
end # module Support
|
41
|
+
end # module Startback
|
@@ -43,7 +43,7 @@ module Startback
|
|
43
43
|
@@default_logger ||= begin
|
44
44
|
l = ::Logger.new(STDOUT)
|
45
45
|
l.formatter = LogFormatter.new
|
46
|
-
l.warn(op: "#{self}", op_data: { msg: "Using default logger
|
46
|
+
l.warn(op: "#{self}", op_data: { msg: "Using default logger to STDOUT" })
|
47
47
|
@@default_logger = l
|
48
48
|
end
|
49
49
|
@@default_logger
|
data/lib/startback/support.rb
CHANGED
data/lib/startback/version.rb
CHANGED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Startback
|
3
|
+
module Support
|
4
|
+
describe Env do
|
5
|
+
include Env
|
6
|
+
|
7
|
+
before do
|
8
|
+
ENV['FOO'] = 'BAR'
|
9
|
+
ENV['FOOL'] = ''
|
10
|
+
ENV['FOOLISH'] = ' BAR '
|
11
|
+
end
|
12
|
+
|
13
|
+
after do
|
14
|
+
ENV.delete('FOO')
|
15
|
+
ENV.delete('FOOL')
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "env" do
|
19
|
+
it 'returns an env variable' do
|
20
|
+
expect(env('FOO')).to eql('BAR')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns nil otherwise' do
|
24
|
+
expect(env('BAR')).to be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'strips the value' do
|
28
|
+
expect(env('FOOLISH')).to eql('BAR')
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'yields the block if any' do
|
32
|
+
expect(env('FOO'){|x| x.downcase }).to eql('bar')
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'support a default value' do
|
36
|
+
expect(env('BAR', 'BAZ')).to eql('BAZ')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'yields the block with the default if any' do
|
40
|
+
expect(env('BAR', 'BAZ'){|x| x.downcase }).to eql('baz')
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'returns nil when empty' do
|
44
|
+
expect(env('FOOL')).to be_nil
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'yields the block with the default if empty' do
|
48
|
+
expect(env('FOOL', 'BAZ'){|x| x.downcase }).to eql('baz')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "env!" do
|
53
|
+
it 'returns an env variable' do
|
54
|
+
expect(env!('FOO')).to eql('BAR')
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'strips the value' do
|
58
|
+
expect(env!('FOOLISH')).to eql('BAR')
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'raise otherwise' do
|
62
|
+
expect{ env!('BAR') }.to raise_error(Startback::Errors::Error, /BAR/)
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'raise on empty' do
|
66
|
+
expect{ env!('FOOL') }.to raise_error(Startback::Errors::Error, /FOOL/)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'yields the block if any' do
|
70
|
+
expect(env('FOO'){|x| x.downcase }).to eql('bar')
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: startback
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.11.
|
4
|
+
version: 0.11.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernard Lambeau
|
@@ -398,6 +398,8 @@ files:
|
|
398
398
|
- lib/startback/context/middleware.rb
|
399
399
|
- lib/startback/errors.rb
|
400
400
|
- lib/startback/event.rb
|
401
|
+
- lib/startback/event/agent.rb
|
402
|
+
- lib/startback/event/engine.rb
|
401
403
|
- lib/startback/ext.rb
|
402
404
|
- lib/startback/ext/date_time.rb
|
403
405
|
- lib/startback/ext/time.rb
|
@@ -405,6 +407,7 @@ files:
|
|
405
407
|
- lib/startback/operation/error_operation.rb
|
406
408
|
- lib/startback/operation/multi_operation.rb
|
407
409
|
- lib/startback/support.rb
|
410
|
+
- lib/startback/support/env.rb
|
408
411
|
- lib/startback/support/fake_logger.rb
|
409
412
|
- lib/startback/support/hooks.rb
|
410
413
|
- lib/startback/support/log_formatter.rb
|
@@ -439,6 +442,7 @@ files:
|
|
439
442
|
- spec/unit/support/hooks/test_before_hook.rb
|
440
443
|
- spec/unit/support/operation_runner/test_around_run.rb
|
441
444
|
- spec/unit/support/operation_runner/test_before_after_call.rb
|
445
|
+
- spec/unit/support/test_env.rb
|
442
446
|
- spec/unit/support/test_robusteness.rb
|
443
447
|
- spec/unit/support/test_transaction_manager.rb
|
444
448
|
- spec/unit/test_event.rb
|