sproc 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2ccda7be2976e3c6daaa74c8a37c858fdc76f5d929e1cf87b77cc7411d9f72d
4
- data.tar.gz: ae73fcb466456779be5e91afc1bbe8e67b368bed7f3c834a0d523acaacfdd9a9
3
+ metadata.gz: 13cf3463eedf90a3e4d64a1bd60d7b9e49a6c08b49f2b957a0b8667ea2f8ef17
4
+ data.tar.gz: d29364e2b3738102c2eeadb6c5084eeea72ff35fc89c7ee2a7be0ebe1b2d420a
5
5
  SHA512:
6
- metadata.gz: abc0511633bf642dfaffd0a35f00f9d257bc38f316010418932b910bfeebbcb7d7c7f479aa70fed8aa31fe6be24eacc19f69a0108a17d766910c7145da3e194a
7
- data.tar.gz: 6d00c01bf0791f2919b37449cf76ee95a71dba9aa95713e0c0955dd37bcb4c9a41b7881e7a4e7058d8800c64963ea78bb804550e6bd66cf0e4e1fada40daf82f
6
+ metadata.gz: '08dd7e0860ae172a028855727e81855100454a059f6614e49a34565ade89f0edba2c72a1fc38f6accbd765d222dde2da71ea297a844ffbcb0d359126945ea198'
7
+ data.tar.gz: 9a1d55462170d19154cccbd938f92278e0bd521c0432b3bcdf04c8c6f09a645f601a1c5ee275bdc9b36d6122bcfd89f8ceb11699902ea41379f39b5baefa2704
data/lib/sproc/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SProc
4
- VERSION = "0.6.1"
4
+ VERSION = "0.7.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sproc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anders Rillbert
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-28 00:00:00.000000000 Z
11
+ date: 2021-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -44,7 +44,6 @@ files:
44
44
  - lib/sproc.rb
45
45
  - lib/sproc/core.rb
46
46
  - lib/sproc/osinfo.rb
47
- - lib/sproc/reporting.rb
48
47
  - lib/sproc/version.rb
49
48
  - sproc.gemspec
50
49
  homepage: https://github.com/rillbert/sproc
@@ -1,196 +0,0 @@
1
- require_relative 'core'
2
-
3
- # This module is written to provide sub-process
4
- # execution with some human readable logging of process start/stop/errors
5
- #
6
- # It wraps SProc instances and execution with calls to logging methods
7
- # that tries to make the resulting log user friendly
8
- module SProc
9
- module Reporting
10
- class << self
11
- attr_accessor :logger
12
- end
13
-
14
- def logger
15
- Reporting.logger
16
- end
17
-
18
- # Run a process synchronuously via the native shell and log
19
- # output suitable for a build log
20
- #
21
- # @param cmd_name a String containing a descriptive
22
- # name for what the process will do.
23
- # @param cmd, args, opts see SProc.exec_sync
24
- #
25
- # @return the SProc instance containing the completed process
26
- def report_sync(cmd_name, cmd, *args, **opts)
27
- p = create_proc_and_log(cmd_name,
28
- ShellType::NONE, :exec_sync,
29
- cmd, args, opts)
30
- report_completed(p)
31
- p
32
- end
33
-
34
- # Run a process asynchronuously via the native shell and log
35
- # output suitable for a build log
36
- #
37
- # @param cmd_name a String containing a descriptive
38
- # name for what the process will do.
39
- # @param cmd, args, opts see SProc.exec_sync
40
- #
41
- # @return the created SProc instance
42
- def report_async(cmd_name, cmd, *args, **opts)
43
- create_proc_and_log(cmd_name,
44
- ShellType::NONE, :exec_async,
45
- cmd, args, opts)
46
- end
47
-
48
- # Run a process asynchronuously via the Bash shell and log
49
- # output suitable for a build log
50
- #
51
- # @param cmd_name a String containing a descriptive
52
- # name for what the process will do.
53
- # @param cmd, args, opts see SProc.exec_sync
54
- #
55
- # @return the created SProc instance
56
- def report_async_within_bash(cmd_name, cmd, *args, **opts)
57
- create_proc_and_log(cmd_name,
58
- ShellType::BASH, :exec_async,
59
- cmd, args, opts)
60
- end
61
-
62
- # Log output from a completed/aborted process
63
- #
64
- # @param process the SProc instance that has run
65
- # @return true/false corresponding to process success
66
- def report_completed(process)
67
- friendly_name = if @log_friendly_name_map&.key?(process)
68
- "#{@log_friendly_name_map[process]}"
69
- else
70
- process.task_info[:cmd_str][0,10] + "..."
71
- end
72
- started_ok = true
73
- case process.execution_state
74
- when ExecutionState::COMPLETED
75
- process.exit_zero? && log_completed_ok(friendly_name, process.task_info)
76
- !process.exit_zero? && log_completed_err(friendly_name, process.task_info)
77
- when ExecutionState::ABORTED
78
- log_aborted(friendly_name, process.task_info)
79
- started_ok = false
80
- when ExecutionState::FAILED_TO_START
81
- log_failed_start(friendly_name, process.task_info)
82
- started_ok = false
83
- else
84
- log_unexpected(friendly_name, process.task_info)
85
- end
86
- started_ok && process.exit_zero?
87
- end
88
-
89
- private
90
-
91
- def create_proc_and_log(cmd_name, type, method, cmd, args, opts)
92
- log_start(cmd_name, type, method, cmd, args, **opts)
93
- p = SProc.new(type: type).send(method, cmd, args, **opts)
94
- @log_friendly_name_map ||= {}
95
- @log_friendly_name_map[p] = cmd_name
96
- p
97
- end
98
-
99
- def log_method_result_ok(friendly_name, delta)
100
- logger.info do
101
- "#{friendly_name} completed successfully after #{delta.round(3)}s"
102
- end
103
- end
104
-
105
- def log_method_result_error(friendly_name_method, delta, exc)
106
- logger.error do
107
- "#{friendly_name_method} aborted by #{exc.class} after #{delta.round(3)}s\n"\
108
- "Exception info: #{exc.message}"
109
- end
110
-
111
- logger.debug do
112
- exc.backtrace.to_s
113
- end
114
- end
115
-
116
- def log_start(cmd_name, type, method, cmd, *args, **opts)
117
- logger.info do
118
- async_str = method == :exec_async ? 'asynchronuously' : 'synchronuously'
119
- type_str = type == ShellType::NONE ? 'without shell' : 'within the bash shell'
120
- "'#{cmd_name}' executing #{async_str} #{type_str}..."
121
- end
122
- logger.debug do
123
- msg = String.new("Starting #{cmd}")
124
- msg << " with args: #{args.flatten.inspect}" unless args.nil? || args.empty?
125
- msg << " and opts: #{opts.inspect}" unless opts.nil? || opts.empty?
126
- msg
127
- end
128
- end
129
-
130
- def log_one_dll(regex, cmd_str, time)
131
- m = regex.match(cmd_str)
132
- s = m.nil? ? cmd_str : m[1]
133
- max = 45
134
- s = s.length > max ? s.slice(0..max - 1) : s.ljust(max)
135
- logger.info { "#{s} took #{time.round(3)}s." }
136
- end
137
-
138
- def log_aborted(friendly_name, p_info)
139
- logger.error do
140
- "'#{friendly_name}' aborted!\n"\
141
- "When running: #{p_info[:cmd_str]}\n"\
142
- "#{merge_proc_output(p_info)}"\
143
- "#{p_info[:process_status] unless p_info[:process_status].nil?}"
144
- end
145
- end
146
-
147
- def log_failed_start(friendly_name, p_info)
148
- logger.error do
149
- "'#{friendly_name}' not run!\n"\
150
- "Could not start process using: #{p_info[:cmd_str]}\n"\
151
- "#{merge_proc_output(p_info)}"
152
- end
153
- end
154
-
155
- def log_completed_ok(friendly_name, p_info)
156
- logger.info do
157
- "'#{friendly_name}' completed successfully after #{p_info[:wall_time].round(3)}s"
158
- end
159
- logger.debug do
160
- "Cmd: #{p_info[:cmd_str]}"
161
- end
162
- end
163
-
164
- def log_completed_err(friendly_name, p_info)
165
- logger.error do
166
- "'#{friendly_name}' completed with exit code "\
167
- "#{p_info[:process_status].exitstatus}\n"\
168
- "When running: #{p_info[:cmd_str]}\n"\
169
- "after #{p_info[:wall_time].round(3)}s\n"\
170
- "#{merge_proc_output(p_info)}"
171
- end
172
- end
173
-
174
- def log_unexpected(friendly_name, p_info)
175
- logger.error do
176
- "'#{friendly_name}' caused unexpected error!"\
177
- ' Trying to display info on a running process'\
178
- "(#{p_info[:cmd_str]})"
179
- end
180
- end
181
-
182
- # @return String with sections for each non-empty output stream
183
- # and exception messages
184
- def merge_proc_output(p_info)
185
- inf_str = %i[stdout stderr].collect do |sym|
186
- next('') if p_info[sym].empty?
187
-
188
- "--- #{sym} ---\n#{p_info[sym]}"
189
- end.join("\n")
190
-
191
- exc = p_info[:exception]
192
- inf_str << "--- exception ---\n#{exc}\n" unless exc.nil?
193
- inf_str
194
- end
195
- end
196
- end