styoe 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- N2MwNWU2NDFiOTg0ZjZiZDQyMmMyZGQ1YjA0MmIyODFmNDc0YmZmNg==
4
+ NWYwZGQ2ODE3NzMwZjk5Y2JiYzM0NmRlMThkYTFlNTQwYzU4MDc2Nw==
5
5
  data.tar.gz: !binary |-
6
- NzIwZWY4ZTNhMzMwMWM0ZGQ5OTlhZGVlYWJjZjQ4ZTgwODExODc4ZA==
6
+ MWZmMTAyMzIwMGQ4ZGZhOTlhNTRlMjE0NWNiNjFiMTFlZDZmN2U4MQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YWY0MDE5OTdjZWYwNGU0OTM5ZWI0NGU1OWFiMThlMjI1YjUzYjMxMDc4NWFi
10
- NGUwNzk4YzhkMzExZTQzYzJiODU0ZDdhNmYxODk4YjQ5MGEzMGEyYzc0N2Nj
11
- MDIzMWMyZWIxYWIyNzBjOWQzNzY3Yjk1YzhlMWQ4ODVkZmVmMGY=
9
+ MTVhNzFmNmViZGYzYzI2MGRlZTBiMGE0Y2M4N2QxZWU2MWYxNTllZDNhMTM2
10
+ ZmYwMjM2OWFkMTVlY2U5ZWQ5ZDUyMzQ1MzAyYjdmZWYyYmZjZTg4ZDQ1OThk
11
+ YzM4YTE3MTAwNzljYTE2ZjhmNWE1NTBkNGYxMDkxNmRhZDhlZTM=
12
12
  data.tar.gz: !binary |-
13
- ODdmNDIzNjk5NmFiNTI2OWNhNzZkYTNmYTRhOWI2MGZlYjlhMzRmZGFmYzNj
14
- ZDgyZDU0YjRlM2RkYTE0YzQwNmY3ZTgxNTJhMGVhOTk4MTMyYzQyZTIwYzYx
15
- Mjk4OTEwOTYwZWYxMDg0N2JmMTE2NmUzODgwYTI1YmIxMTkwYjA=
13
+ NjVlMWRhZTk3ZWIzMWVjZmVjMmM1NDQ2MTI2ZjNmZjA0YjE5OTBhNGRlZmNl
14
+ M2M1NGExZWY1NGIzMDFlY2M5ZDNlNjJmZWYyODE0MDVkN2ZiZjhjYjljNzE1
15
+ OTM1MzJhZjM3NDk2MWY5NzJlZTczMDdmODdjYjAwMjk0NWIwNjg=
data/bin/styoe CHANGED
@@ -2,11 +2,16 @@
2
2
 
3
3
  require 'styoe'
4
4
 
5
- launcher = Styoe::Launcher.new
5
+ launcher = Styoe::Launcher.build('.engines', '.engines.pids')
6
6
 
7
- case ARGV[0]
8
- when '--stop'
9
- launcher.stop
10
- else
11
- launcher.start
7
+ begin
8
+ case ARGV[0]
9
+ when '--stop'
10
+ launcher.stop
11
+ else
12
+ launcher.start
13
+ end
14
+ rescue Styoe::ConfigurationNotFound
15
+ $stderr.puts 'No configuration found. Are you sure you have created a .engines file?'
16
+ exit(1)
12
17
  end
@@ -17,3 +17,4 @@ Feature: Applications closing
17
17
  When I successfully run `styoe`
18
18
  And I successfully run `styoe --stop`
19
19
  Then a file named "closed" should exist
20
+ And a file named ".engines.pids" should not exist
data/lib/styoe.rb CHANGED
@@ -3,20 +3,25 @@ require 'styoe/configuration_resolver'
3
3
  require 'styoe/process_launcher'
4
4
 
5
5
  module Styoe
6
+ class RunningProcess < Struct.new(:path, :pid)
7
+ end
8
+
6
9
  class Launcher
7
- def initialize(config_resolver = nil, process_launcher = nil)
8
- @config_resolver = config_resolver || ConfigurationResolver.new
9
- @process_launcher = process_launcher || ProcessLauncher.new
10
+ def initialize(config_resolver, process_launcher)
11
+ @config_resolver = config_resolver
12
+ @process_launcher = process_launcher
13
+ end
14
+
15
+ def self.build(dot_file, pid_file)
16
+ config_resolver = Styoe::ConfigurationResolver.new(dot_file, pid_file, Styoe::DotFileManager.new)
17
+ process_launcher = Styoe::ProcessLauncher.new
18
+
19
+ self.new(config_resolver, process_launcher)
10
20
  end
11
21
 
12
22
  def start
13
- begin
14
- processes = Hash[*run_processes]
15
- @config_resolver.dump_pids(processes)
16
- rescue ConfigurationNotFound
17
- $stderr.puts 'No configuration found. Are you sure you have created a .engines file?'
18
- exit(1)
19
- end
23
+ running_processes = run_processes!
24
+ @config_resolver.dump_processes(running_processes)
20
25
  end
21
26
 
22
27
  def stop
@@ -27,10 +32,11 @@ module Styoe
27
32
 
28
33
  private
29
34
 
30
- def run_processes
31
- @config_resolver.processes.map do |process|
32
- [process, @process_launcher.launch(process)]
33
- end.flatten
35
+ def run_processes!
36
+ @config_resolver.processes.map do |process_path|
37
+ pid = @process_launcher.launch(process_path)
38
+ RunningProcess.new(process_path, pid)
39
+ end
34
40
  end
35
41
  end
36
42
  end
@@ -1,48 +1,48 @@
1
1
  require 'yaml'
2
+ require 'styoe/dot_file_manager'
2
3
 
3
4
  module Styoe
4
- DOT_FILE = '.engines'
5
- PID_FILE = '.engines.pids'
6
-
7
- class ConfigurationNotFound < StandardError; end
8
-
9
5
  class ConfigurationResolver
10
- def initialize(options = {})
11
- @stop_at = options[:stop_at] || '/'
6
+ def initialize(dot_filename, pid_filename, dot_file_manager)
7
+ @dot_filename = dot_filename
8
+ @pid_filename = pid_filename
9
+ @dot_file_manager = dot_file_manager
12
10
  end
13
11
 
14
- def active_processes(path = PID_FILE)
15
- raise ConfigurationNotFound if stop?(path)
16
- return active_processes("../#{path}") unless File.exist?(path)
12
+ def active_processes
13
+ file_configuration = @dot_file_manager.find_recursively(@pid_filename)
17
14
 
18
- parse_values(path)
15
+ File.delete(file_configuration.path)
16
+ parse_contents(file_configuration.contents)
19
17
  end
20
18
 
21
- def processes(path = DOT_FILE)
22
- raise ConfigurationNotFound if stop?(path)
23
- return processes("../#{path}") unless File.exist?(path)
19
+ def processes
20
+ file_configuration = @dot_file_manager.find_recursively(@dot_filename)
24
21
 
25
- parse_values(path)
22
+ parse_contents(file_configuration.contents)
26
23
  end
27
24
 
28
- def dump_pids(pids, path = DOT_FILE)
29
- raise ConfigurationNotFound if stop?(path)
30
- return dump_pids(pids, "../#{path}") unless File.exist?(path)
25
+ def dump_processes(processes)
26
+ processes_dump = ProcessesDumper.dump(processes)
31
27
 
32
- file = File.open(path.gsub(DOT_FILE, PID_FILE), "w")
33
- file.write(YAML.dump pids)
34
- file.close
28
+ @dot_file_manager.save_near(@dot_filename,
29
+ new_name: @pid_filename,
30
+ contents: processes_dump)
35
31
  end
36
32
 
37
- private
38
-
39
- def parse_values(path)
40
- contents = File.read(path)
33
+ def parse_contents(contents)
41
34
  YAML.load(contents).values
42
35
  end
36
+ end
37
+
38
+ class ProcessesDumper
39
+ def self.dump(processes)
40
+ self.to_hash(processes).to_yaml
41
+ end
43
42
 
44
- def stop?(path)
45
- File.dirname(File.absolute_path path) == @stop_at
43
+ def self.to_hash(processes)
44
+ ary_processes = processes.map { |p| [p.path, p.pid] }.flatten
45
+ Hash[*ary_processes]
46
46
  end
47
47
  end
48
48
  end
@@ -0,0 +1,40 @@
1
+ module Styoe
2
+ class ConfigurationNotFound < StandardError; end
3
+
4
+ class DotFile < Struct.new(:path, :contents)
5
+ def self.read_from_path(path)
6
+ contents = File.read(path)
7
+ self.new(path, contents)
8
+ end
9
+ end
10
+
11
+ class DotFileManager
12
+ def find_recursively(path)
13
+ return DotFile.read_from_path(path) if File.exist?(path)
14
+ raise ConfigurationNotFound if reached_root?(path)
15
+
16
+ find_recursively("../#{path}")
17
+ end
18
+
19
+ def save_near(filename, new_name: "", contents: "")
20
+ near_file = find_recursively(filename)
21
+ path_to_save = filename_near(near_file.path, new_name)
22
+ write_file(path_to_save, contents)
23
+ end
24
+
25
+ private
26
+
27
+ def filename_near(path, name)
28
+ filename = File.basename(path)
29
+ path.gsub(filename, name)
30
+ end
31
+
32
+ def write_file(path, contents)
33
+ File.open(path, 'w') { |f| f.write(contents) }
34
+ end
35
+
36
+ def reached_root?(path)
37
+ File.dirname(File.absolute_path path) == '/'
38
+ end
39
+ end
40
+ end
data/lib/styoe/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Styoe
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,84 +1,62 @@
1
+ require 'styoe'
1
2
  require 'styoe/configuration_resolver'
2
3
 
3
4
  describe Styoe::ConfigurationResolver do
4
5
 
5
- let(:pids) { { application1: 1, application2: 2 } }
6
- let(:pid_file) { instance_double(File) }
6
+ let(:dot_filename) { '.engines' }
7
+ let(:pid_filename) { '.engines.pids' }
8
+ let(:dot_file_manager) { instance_double(Styoe::DotFileManager) }
7
9
 
8
- it 'resolves and parses a configuration file' do
9
- allow_file_to_exist_and_return("#{Styoe::DOT_FILE}", sample_configuration)
10
+ subject { described_class.new(dot_filename, pid_filename, dot_file_manager) }
10
11
 
11
- expect(subject.processes).to eq(['hello', 'world'])
12
- end
13
-
14
- it 'searches configuration recursively in parent directories' do
15
- allow_file_to_exist_and_return("../../#{Styoe::DOT_FILE}", sample_configuration)
12
+ it 'parses found configuration file' do
13
+ stub_found_dotfile(dot_filename, sample_configuration)
16
14
 
17
15
  expect(subject.processes).to eq(['hello', 'world'])
18
16
  end
19
17
 
20
- it 'stops searching configuration when root is reached' do
21
- subject = described_class.new(stop_at: '/')
22
- allow(File).to receive(:dirname).and_return('/')
18
+ it 'searches active processes and deletes related pidfile' do
19
+ stub_found_dotfile(pid_filename, sample_pids)
20
+ allow(File).to receive(:delete)
23
21
 
24
- expect { subject.processes }.to raise_error(Styoe::ConfigurationNotFound)
25
- end
26
-
27
- it 'searches active processes' do
28
- allow_file_to_exist_and_return("#{Styoe::PID_FILE}", sample_pids)
22
+ active_processes = subject.active_processes
29
23
 
30
- expect(subject.active_processes).to eq([ 1, 2 ])
24
+ expect(active_processes).to eq([ 1, 2 ])
25
+ expect(File).to have_received(:delete).with(pid_filename)
31
26
  end
32
27
 
33
- it 'searches active processes recursively' do
34
- allow_file_to_exist_and_return("../../#{Styoe::PID_FILE}", sample_pids)
28
+ it 'dumps active processes on the same path of dotfile' do
29
+ allow(dot_file_manager).to receive(:save_near)
30
+ subject.dump_processes([
31
+ Styoe::RunningProcess.new("application1", 1),
32
+ Styoe::RunningProcess.new("application2", 2)
33
+ ])
35
34
 
36
- expect(subject.active_processes).to eq([ 1, 2 ])
35
+ expect(dot_file_manager).to have_received(:save_near)
36
+ .with(dot_filename,
37
+ new_name: pid_filename,
38
+ contents: sample_pids)
37
39
  end
38
40
 
39
- it 'stops searching active processes when root is reached' do
40
- subject = described_class.new(stop_at: '/')
41
- allow(File).to receive(:dirname).and_return('/')
42
-
43
- expect { subject.active_processes }.to raise_error(Styoe::ConfigurationNotFound)
44
- end
45
-
46
- it "dumps active processes on the same path of #{Styoe::DOT_FILE}" do
47
- pid_file = double
48
- pids = { application1: 1, application2: 2 }
49
- allow(File).to receive(:exist?).and_return(false, false, true)
50
- allow(File).to receive(:open).with("../../#{Styoe::PID_FILE}", "w").and_return(pid_file)
51
- allow(YAML).to receive(:dump).with(pids).and_return(sample_pids)
52
- allow(pid_file).to receive(:write)
53
- allow(pid_file).to receive(:close)
54
-
55
- subject.dump_pids(pids)
56
-
57
- expect(pid_file).to have_received(:write).with(sample_pids)
58
- expect(pid_file).to have_received(:close)
59
- end
60
-
61
- def allow_file_to_exist_and_return(path, read_file)
62
- allow_file_to_exist(path)
63
- allow(File).to receive(:read).with(path).and_return(read_file)
64
- end
65
-
66
- def allow_file_to_exist(path)
67
- allow(File).to receive(:exist?).and_return(false)
68
- allow(File).to receive(:exist?).with(path).and_return(true)
69
- end
70
-
71
- def sample_configuration
72
- <<-config
73
- application1: hello
74
- application2: world
75
- config
76
- end
41
+ def sample_configuration
42
+ <<-pids
43
+ ---
44
+ application1: hello
45
+ application2: world
46
+ pids
47
+ end
77
48
 
78
49
  def sample_pids
79
50
  <<-pids
80
- application1: 1
81
- application2: 2
51
+ ---
52
+ application1: 1
53
+ application2: 2
82
54
  pids
83
55
  end
56
+
57
+ def stub_found_dotfile(path, contents)
58
+ configuration = Styoe::DotFile.new(path, contents)
59
+ allow(dot_file_manager).to receive(:find_recursively).with(path).and_return(configuration)
60
+ end
61
+
84
62
  end
@@ -3,20 +3,22 @@ require 'styoe'
3
3
  describe Styoe::Launcher do
4
4
  let(:configuration_resolver) { instance_double(Styoe::ConfigurationResolver) }
5
5
  let(:process_launcher) { instance_double(Styoe::ProcessLauncher) }
6
- let(:processes) { [ "app1", "app2", "app3" ] }
7
6
  let(:pids) { [ 1, 2, 3 ] }
8
- let(:running_processes) { Hash[*processes.zip(pids).flatten] }
9
7
 
10
8
  subject { Styoe::Launcher.new(configuration_resolver, process_launcher) }
11
9
 
12
10
  it "launches configuration processes and remembers open pids" do
13
- allow(configuration_resolver).to receive(:processes).and_return(processes)
14
- allow(configuration_resolver).to receive(:dump_pids)
15
- stub_processes_launch
11
+ allow(configuration_resolver).to receive(:processes).and_return(["app1", "app2", "app3"])
12
+ allow(configuration_resolver).to receive(:dump_processes)
13
+ stub_processes_launch("app1" => 1, "app2" => 2, "app3" => 3)
16
14
 
17
15
  subject.start
18
16
 
19
- expect(configuration_resolver).to have_received(:dump_pids).with(running_processes)
17
+ expect(configuration_resolver).to have_received(:dump_processes).with([
18
+ Styoe::RunningProcess.new("app1", 1),
19
+ Styoe::RunningProcess.new("app2", 2),
20
+ Styoe::RunningProcess.new("app3", 3)
21
+ ])
20
22
  end
21
23
 
22
24
  it 'stops active processes' do
@@ -30,8 +32,8 @@ describe Styoe::Launcher do
30
32
 
31
33
  private
32
34
 
33
- def stub_processes_launch
34
- processes.zip(pids).each do |process, pid|
35
+ def stub_processes_launch(processes)
36
+ processes.each do |process, pid|
35
37
  allow(process_launcher).to receive(:launch).with(process).and_return(pid)
36
38
  end
37
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: styoe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antonio Scandurra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-23 00:00:00.000000000 Z
11
+ date: 2014-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,12 +101,13 @@ files:
101
101
  - features/support/env.rb
102
102
  - lib/styoe.rb
103
103
  - lib/styoe/configuration_resolver.rb
104
+ - lib/styoe/dot_file_manager.rb
104
105
  - lib/styoe/process_launcher.rb
105
106
  - lib/styoe/version.rb
106
107
  - script/bootstrap
107
108
  - spec/.gitkeep
108
109
  - spec/configuration_resolver_spec.rb
109
- - spec/fire_spec.rb
110
+ - spec/styoe_spec.rb
110
111
  - styoe.gemspec
111
112
  homepage: https://github.com/as-cii/styoe
112
113
  licenses:
@@ -138,4 +139,4 @@ test_files:
138
139
  - features/support/env.rb
139
140
  - spec/.gitkeep
140
141
  - spec/configuration_resolver_spec.rb
141
- - spec/fire_spec.rb
142
+ - spec/styoe_spec.rb