easy_app_helper 3.0.6 → 3.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ea069d461f143561b29ca40450dd1f2f2ca3ddb
4
- data.tar.gz: b96c99a38b96cdec7fe66338e5da0cdd72fd80c8
3
+ metadata.gz: 635c849e54c3e7484c113e625a6b7919b1d4608a
4
+ data.tar.gz: 5c0f578d46c51f52ce63871458155479f2bd09e7
5
5
  SHA512:
6
- metadata.gz: 828ec354346f85946ac7330c63652a007797bda27105bfb408d0a8e0c2b6fce700650cd933e7e371abab9de1160134ca23f874bfb69d9712fb149a822e40e067
7
- data.tar.gz: 81cdd985aab372438dfe47b59e9de0bbd6ee6f0caedc3f1dc46f7bc959d7b3a006573028cd982d755507deb2252701dc7cbd13cb112bf690e5fab2dd5ed05d52
6
+ metadata.gz: 123516bb890c57f720faeb44d34ceb600accd16d3807fe798a74ace6b0eff4029a3d0bd255ae0446b97380f7d0a7aca819d959e563f86d4166ba021e6e95e4bf
7
+ data.tar.gz: 06e01c697f545dc437a7e065be01c18b3922d0beff0ecc2c8cf462140f3e77c3b5284ff67caf2047c490983b17e84e4b9f38fa7641d7b125f9e42dc110f7a8c2
@@ -0,0 +1,51 @@
1
+ require 'open3'
2
+
3
+ module EasyAppHelper
4
+ module Processes
5
+
6
+ class Base
7
+
8
+ include EasyAppHelper::Processes::Command
9
+ include EasyAppHelper::Processes::TimeManagement
10
+
11
+ attr_reader :process_state, :exit_status, :last_pid, :mode
12
+ attr_accessor :show_output, :log_output
13
+
14
+ def initialize(command = nil, mode = :synchronous)
15
+ self.command = command
16
+ self.process_state = :not_started
17
+ self.mode = mode
18
+ self.creation_time = Time.now
19
+ end
20
+
21
+ def mode=(mode)
22
+ mode_processor = Object.const_get "EasyAppHelper::Processes::#{mode.to_s.capitalize}"
23
+ self.extend mode_processor
24
+ @mode = mode.to_sym
25
+ rescue
26
+ raise "Invalid process mode '#{mode}'"
27
+ end
28
+
29
+ private
30
+
31
+ attr_writer :process_state, :exit_status, :last_pid
32
+
33
+ def report(message, to_stdout = true)
34
+ if show_output
35
+ to_stdout ? puts(message) : STDERR.puts(message)
36
+ end
37
+ if log_output
38
+ log_line = "[subprocess #{last_pid}] - #{message}"
39
+ if to_stdout
40
+ EasyAppHelper.logger.debug log_line
41
+ else
42
+ EasyAppHelper.logger.warn log_line
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,15 @@
1
+ module EasyAppHelper
2
+ module Processes
3
+
4
+ module Command
5
+
6
+ attr_accessor :command
7
+
8
+ def valid?
9
+ File.exists? self.command.split(' ').first
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ module EasyAppHelper
2
+ module Processes
3
+
4
+ module Synchronous
5
+
6
+ def execute
7
+ self.exit_status = nil
8
+ self.last_pid = nil
9
+ self.process_state = :running
10
+ self.start_time = Time.now
11
+ Open3.popen3(command) do |stdin, stdout, stderr, wait_thread|
12
+ stdin.close
13
+ self.last_pid = wait_thread.pid
14
+ begin
15
+ monitored_streams = [stdout, stderr]
16
+ loop do
17
+ begin
18
+ readables, writables = IO.select(monitored_streams)
19
+ writables.each(&:close)
20
+ readables.each do |io|
21
+ begin
22
+ buffer = ''
23
+ buffer << io.read_nonblock(1) while buffer[-1] != "\n"
24
+ report buffer, io == stdout
25
+ rescue IO::WaitReadable
26
+ next
27
+ rescue EOFError => e
28
+ monitored_streams.delete io
29
+ monitored_streams.empty? ? raise(e) : next
30
+ end
31
+ end
32
+ rescue EOFError
33
+ report "End of process #{wait_thread.value.pid}"
34
+ break
35
+ end
36
+ end
37
+ rescue Errno::EAGAIN
38
+ retry
39
+ end
40
+ self.exit_status = wait_thread.value
41
+ return self.exit_status
42
+ end
43
+ ensure
44
+ self.end_time = Time.now
45
+ self.process_state = :terminated
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ require 'open3'
2
+
3
+ module EasyAppHelper
4
+ module Processes
5
+
6
+ module TimeManagement
7
+
8
+ attr_reader :creation_time, :start_time, :end_time
9
+
10
+ def duration
11
+ end_time - start_time
12
+ end
13
+
14
+ private
15
+
16
+ attr_writer :creation_time, :start_time, :end_time
17
+
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ require 'easy_app_helper/processes/time_management'
2
+ require 'easy_app_helper/processes/command'
3
+ require 'easy_app_helper/processes/synchronous'
4
+ require 'easy_app_helper/processes/base'
@@ -1,3 +1,3 @@
1
1
  module EasyAppHelper
2
- VERSION = '3.0.6'
2
+ VERSION = '3.0.7'
3
3
  end
@@ -10,18 +10,31 @@ require 'easy_app_helper/logger/initializer'
10
10
  require 'easy_app_helper/logger/wrapper'
11
11
  require 'easy_app_helper/managed_logger'
12
12
 
13
+ require 'easy_app_helper/processes'
14
+
15
+
13
16
  module EasyAppHelper
14
17
 
15
18
  def puts_and_logs(*args)
16
19
  logger.puts_and_logs *args
17
20
  end
18
21
 
19
- def safely_exec(message, *args, &block)
20
- if self[:simulate]
21
- puts_and_logs "SIMULATING: #{message}" unless message.nil?
22
+ def safely_exec_code(message, *args, &block)
23
+ if self.config[:simulate]
24
+ puts_and_logs "[SIMULATION MODE]: #{message}" unless message.nil?
22
25
  else
23
26
  puts_and_logs message
24
- yield(*args)
27
+ block.call *args
28
+ end
29
+ end
30
+
31
+ def safely_exec_command(message, command, show_output = false, log_output = true)
32
+ safely_exec_code message, command, show_output, log_output do |command, show_output, log_output|
33
+ process = EasyAppHelper::Processes::Base.new command
34
+ process.show_output = show_output
35
+ process.log_output = log_output
36
+ process.execute
37
+ process
25
38
  end
26
39
  end
27
40
 
@@ -3,7 +3,10 @@ require 'spec_helper'
3
3
 
4
4
  describe EasyAppHelper do
5
5
 
6
- %i(config logger puts_and_logs safely_exec).each do |m|
6
+ let (:message) { :a_message }
7
+ subject {described_class}
8
+
9
+ %i(config logger puts_and_logs safely_exec_code safely_exec_command).each do |m|
7
10
  it "should have a :#{m.to_s} module method" do
8
11
  expect(subject).to respond_to m
9
12
  end
@@ -17,7 +20,7 @@ describe EasyAppHelper do
17
20
  A.new
18
21
  }
19
22
 
20
- %i(config logger puts_and_logs safely_exec).each do |m|
23
+ %i(config logger puts_and_logs safely_exec_code safely_exec_command).each do |m|
21
24
  it "should have a :#{m.to_s} method " do
22
25
  expect(subject).to respond_to m
23
26
  end
@@ -25,4 +28,22 @@ describe EasyAppHelper do
25
28
  end
26
29
 
27
30
  end
31
+
32
+ it 'should allow to safely execute a block' do
33
+ expect do
34
+ subject.safely_exec_code message do
35
+ raise
36
+ end
37
+ end.to raise_error
38
+
39
+ subject.config[:simulate] = true
40
+ expect do
41
+ subject.safely_exec_code message do
42
+ raise
43
+ end
44
+ end.not_to raise_error
45
+
46
+ end
47
+
48
+
28
49
  end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe EasyAppHelper::Processes::Base do
4
+
5
+ subject {described_class.new}
6
+
7
+ before(:all) do
8
+ EasyAppHelper.config[:debug] = true
9
+ EasyAppHelper.config[:'log-level'] = 0
10
+ io = IO.new File.open('/tmp/pipo.log', 'w').fileno
11
+ io.sync
12
+ EasyAppHelper::Logger::Initializer.setup_logger Logger.new(io)
13
+ EasyAppHelper.logger.info 'Hi World'
14
+ end
15
+
16
+ it 'should be a synchronous job by default' do
17
+ expect(subject.mode).to eq :synchronous
18
+ end
19
+
20
+ context 'when the command is invalid' do
21
+ let (:command) {'blablabla'}
22
+ subject {described_class.new command}
23
+
24
+ it 'should raise an exception' do
25
+ expect { subject.execute }.to raise_error
26
+ end
27
+
28
+ end
29
+
30
+ context 'when the command can execute and returns a status' do
31
+
32
+ let (:true_command) { 'true' }
33
+ let (:false_command) { 'false' }
34
+
35
+ it 'should capture the command exit status' do
36
+ subject.command = true_command
37
+ expect(subject.execute.success?).to be_truthy
38
+ subject.command = false_command
39
+ expect(subject.execute.success?).to be_falsey
40
+ end
41
+
42
+ it 'should have a valid duration' do
43
+ subject.command = true_command
44
+ expect(subject.creation_time).not_to be_nil
45
+ expect(subject.start_time).to be_nil
46
+ expect(subject.end_time).to be_nil
47
+ subject.execute
48
+ expect(subject.start_time).not_to be_nil
49
+ expect(subject.end_time).not_to be_nil
50
+ expect(subject.duration).to be > 0
51
+ end
52
+
53
+ end
54
+
55
+ context 'when executing a job that has "out" and "err" outputs' do
56
+
57
+ let (:command_with_out) {File.expand_path('../../../test/process/test.sh', __FILE__)}
58
+
59
+ it 'should capture everything' do
60
+ subject.command = command_with_out
61
+ subject.show_output = true
62
+ expect(STDOUT).to receive(:puts).exactly(3).times
63
+ expect(STDERR).to receive(:puts).exactly(2).times
64
+ subject.execute
65
+ end
66
+
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+
3
+ echo "This is on the standard OUTPUT"
4
+ echo "This is on the standard ERROR" >&2
5
+ echo "This is on the standard OUTPUT too"
6
+ echo "This is on the standard ERROR too" >&2
7
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_app_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.6
4
+ version: 3.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - L.Briais
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-12 00:00:00.000000000 Z
11
+ date: 2016-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,6 +116,11 @@ files:
116
116
  - lib/easy_app_helper/logger/initializer.rb
117
117
  - lib/easy_app_helper/logger/wrapper.rb
118
118
  - lib/easy_app_helper/managed_logger.rb
119
+ - lib/easy_app_helper/processes.rb
120
+ - lib/easy_app_helper/processes/base.rb
121
+ - lib/easy_app_helper/processes/command.rb
122
+ - lib/easy_app_helper/processes/synchronous.rb
123
+ - lib/easy_app_helper/processes/time_management.rb
119
124
  - lib/easy_app_helper/tasks.rb
120
125
  - lib/easy_app_helper/version.rb
121
126
  - lib/tasks/easy_app_helper_tasks.rake
@@ -124,6 +129,7 @@ files:
124
129
  - spec/config_spec.rb
125
130
  - spec/easy_app_helper_spec.rb
126
131
  - spec/logger_spec.rb
132
+ - spec/processes/base_spec.rb
127
133
  - spec/spec_helper.rb
128
134
  - test/foo/Gemfile
129
135
  - test/foo/LICENSE.txt
@@ -132,6 +138,7 @@ files:
132
138
  - test/foo/foo.gemspec
133
139
  - test/foo/lib/foo.rb
134
140
  - test/foo/lib/foo/version.rb
141
+ - test/process/test.sh
135
142
  homepage: https://github.com/lbriais/easy_app_helper
136
143
  licenses:
137
144
  - MIT
@@ -161,6 +168,7 @@ test_files:
161
168
  - spec/config_spec.rb
162
169
  - spec/easy_app_helper_spec.rb
163
170
  - spec/logger_spec.rb
171
+ - spec/processes/base_spec.rb
164
172
  - spec/spec_helper.rb
165
173
  - test/foo/Gemfile
166
174
  - test/foo/LICENSE.txt
@@ -169,3 +177,4 @@ test_files:
169
177
  - test/foo/foo.gemspec
170
178
  - test/foo/lib/foo.rb
171
179
  - test/foo/lib/foo/version.rb
180
+ - test/process/test.sh