linemanager 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/linemanager.rb +98 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 136c1c780f4af21356e420bdb875653b54ca7516
|
4
|
+
data.tar.gz: bee42a66b681d1a001e5fbdb0fcfdc935282ff98
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec053510a692836b645bba1f99b025159a2726a5e229a3c2fee188e7b9d390c26b4d4896c41877de3d754bf7f28d66cec71f5318ab25dccd04817b978b54ac91
|
7
|
+
data.tar.gz: daeb0a59c4ec9a9a901f43268d454dd258c56532a08bdaabbc3d132ac8c72e8b9e2b513b7042353e287181a398ea19ba32eab35144b98a84b10f2f77da78d7a4
|
data/lib/linemanager.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'dotenv'
|
3
|
+
|
4
|
+
module LineManager
|
5
|
+
class Runner
|
6
|
+
PREFIX = "=== LineManager: "
|
7
|
+
STARTED_MSG = PREFIX + "started processess for environment: "
|
8
|
+
STOPPED_MSG = PREFIX + "stopped processess for environment: "
|
9
|
+
WAITING_MSG = PREFIX + "waiting for environment to be ready"
|
10
|
+
RUNNING_MSG = PREFIX +
|
11
|
+
"some or all processes already running for environment: "
|
12
|
+
ALREADY_RUNNING_RE = /SIGTERM to all processes/
|
13
|
+
|
14
|
+
def initialize(app_dir, environment, options={})
|
15
|
+
@app_dir = app_dir
|
16
|
+
@environment = environment
|
17
|
+
@debug = options.fetch(:debug, false)
|
18
|
+
Dotenv.load(environment_path) if options.fetch(:load_env, true)
|
19
|
+
end
|
20
|
+
|
21
|
+
def wait_for(pattern, options={})
|
22
|
+
return if !!ENV['LINEMANAGER_PID']
|
23
|
+
ENV['LINEMANAGER_PID'] = Process.pid.to_s
|
24
|
+
|
25
|
+
component = options.fetch(:only, nil)
|
26
|
+
timeout = options.fetch(:timeout, 10)
|
27
|
+
|
28
|
+
File.open(log_path) do |log|
|
29
|
+
log.seek(0, IO::SEEK_END)
|
30
|
+
self.foreman_pid = spawn_foreman(component)
|
31
|
+
scan(log, pattern, timeout)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def spawn_foreman(component=nil)
|
36
|
+
Process.spawn(
|
37
|
+
"foreman start #{component} -d #{app_dir} -e #{environment_path}",
|
38
|
+
[:out, :err] => [log_path, 'a']
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
def teardown
|
43
|
+
ENV['LINEMANAGER_PID'] = nil
|
44
|
+
return unless foreman_pid
|
45
|
+
|
46
|
+
Process.kill("TERM", foreman_pid)
|
47
|
+
Process.wait(foreman_pid)
|
48
|
+
tell(STOPPED_MSG + environment)
|
49
|
+
end
|
50
|
+
|
51
|
+
def environment_path
|
52
|
+
File.join(app_dir, 'environments', "#{environment}.env")
|
53
|
+
end
|
54
|
+
|
55
|
+
def log_path
|
56
|
+
File.join(app_dir, "log", "linemanager_#{environment}.log")
|
57
|
+
end
|
58
|
+
|
59
|
+
def scan(log, pattern, timeout)
|
60
|
+
match_re = Regexp.new(pattern)
|
61
|
+
time = Time.now.to_i
|
62
|
+
waited = 0
|
63
|
+
|
64
|
+
loop do
|
65
|
+
line = log.gets
|
66
|
+
|
67
|
+
if line =~ match_re
|
68
|
+
tell(STARTED_MSG + environment)
|
69
|
+
break
|
70
|
+
end
|
71
|
+
|
72
|
+
if line =~ ALREADY_RUNNING_RE
|
73
|
+
tell(RUNNING_MSG + environment)
|
74
|
+
self.foreman_pid = nil
|
75
|
+
break
|
76
|
+
end
|
77
|
+
|
78
|
+
break if waited >= timeout
|
79
|
+
|
80
|
+
if (Time.now.to_i - time > 1)
|
81
|
+
tell(WAITING_MSG)
|
82
|
+
time = Time.now.to_i
|
83
|
+
waited += 1
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def tell(message)
|
89
|
+
puts "\n#{message}" if debug
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
attr_reader :app_dir, :environment, :debug
|
95
|
+
attr_accessor :foreman_pid
|
96
|
+
end # Runner
|
97
|
+
end # LineManager
|
98
|
+
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: linemanager
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lorenzo Planas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: foreman
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: "LineManager is a thin wrapper over Foreman that allows\n programmatically
|
28
|
+
starting and stopping processes from an Procfile, using \n available Dotenv environments.
|
29
|
+
Its primary purpose is to execute specs in\n clean, separated process environments."
|
30
|
+
email: lorenzo@qindio.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- lib/linemanager.rb
|
36
|
+
homepage: http://github.com/qindio/linemanager
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.2.0
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Programmatically start and stop Procfile processes.
|
60
|
+
test_files: []
|