get_process_start_time 0.1.1 → 0.1.2

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
  SHA1:
3
- metadata.gz: 1cd2ef30c76d390ea7cc18290d7631e23c9bc663
4
- data.tar.gz: 2ad5ce9af6d3c2da01f040ab122e2ca37dc10ffd
3
+ metadata.gz: ab5bf73c6fac856eb2b9ed2abf44c46d98a430e8
4
+ data.tar.gz: b6b02b5dab02ff52c7e1a03946131bc40dfed296
5
5
  SHA512:
6
- metadata.gz: 8151646777e80b5a72019a1f6990460b2bf44d9cb58e361fe6bbb5723f86899f129bdbf52c9b3f654577c67e38d9ed0c32c823c2abe8c9e0bce048ce9e665619
7
- data.tar.gz: f29810c54cba6f0e314b02accf1c226fc3367b3aeda7abd5f6f430bc15d885b45b780d301a33f78c98ecc3e511ca4e7b1e123ba09e035fb6fe25bee6e3af582e
6
+ metadata.gz: d7bc517a92ab95f76a8d8c5c15e6682d484e6f9143b22233a2a02a17194cca41857c6262b0fc767eb15ee3faf013fe402c83450eb9cc2d74573feb987138c9e3
7
+ data.tar.gz: 324b4166daa71cd5ddee30bbe5fd387fcb8c9417f596dfec354c1cc70cbb290aea8636145e8df862a452cc3b7df091e08fef3c922a15d9aedc5578c5ff87ee06
@@ -1,3 +1,10 @@
1
+ # 0.1.2 (2016-09-13)
2
+
3
+ Enhancements:
4
+
5
+ * Add `test_duration` subcommand
6
+
1
7
  # 0.1.1 (2016-09-13)
2
8
 
3
9
  First version
10
+
data/README.md CHANGED
@@ -23,8 +23,20 @@ Or install it yourself as:
23
23
  ### CLI
24
24
 
25
25
  ```
26
- $ get_process_start_time <PID>
27
- <unix timestamp>
26
+ Usage: get_process_start_time <PID>
27
+ Or: get_process_start_time test <PID> <COMPARTOR> <SECONDS>
28
+
29
+ Get starting time of a process with unix timestamp
30
+
31
+ $ get_process_start_time <PID>
32
+ <unix timestamp>
33
+
34
+ Test living duration of a process is less than (or greater than) a given seconds
35
+
36
+ $ get_process_start_time test_duration <PID> <COMPARTOR> <SECONDS>
37
+ $ echo $? # 0 or 1
38
+
39
+ COMPARTOR: -lt (less than) or -gt (greater than)
28
40
  ```
29
41
 
30
42
  ### Library
@@ -4,35 +4,88 @@ require 'optparse'
4
4
  class GetProcessStartTime
5
5
  class CLI
6
6
  def parse_options(argv = ARGV)
7
- op = OptionParser.new
8
-
9
7
  self.class.module_eval do
10
8
  define_method(:usage) do |msg = nil|
11
- puts op.to_s
12
- puts "error: #{msg}" if msg
9
+ puts "Usage: get_process_start_time <PID>"
10
+ puts "Or: get_process_start_time test <PID> <COMPARTOR> <SECONDS>"
11
+ puts ""
12
+ puts "Get starting time of a process with unix timestamp"
13
+ puts ""
14
+ puts " $ get_process_start_time <PID>"
15
+ puts " <unix timestamp>"
16
+ puts ""
17
+ puts "Test living duration of a process is less than (or greater than) a given seconds"
18
+ puts ""
19
+ puts " $ get_process_start_time test_duration <PID> <COMPARTOR> <SECONDS>"
20
+ puts " $ echo $? # 0 or 1"
21
+ puts ""
22
+ puts "COMPARTOR: -lt (less than) or -gt (greater than)"
23
+ if msg
24
+ puts ""
25
+ puts "ERROR: #{msg}"
26
+ end
13
27
  exit 1
14
28
  end
15
29
  end
16
30
 
17
- opts = {}
18
- op.banner += ' PID'
19
31
  begin
20
- args = op.parse(argv)
21
- rescue OptionParser::InvalidOption => e
32
+ case argv[0]
33
+ when '-h', '--help'
34
+ usage
35
+ when 'test_duration'
36
+ if argv.size < 4
37
+ usage 'number of arguments must be 4'
38
+ end
39
+ @subcommand = :test_duration
40
+ @pid = Integer(argv[1])
41
+ @comparator = validate_comparator!(argv[2])
42
+ @seconds = Float(argv[3])
43
+ else
44
+ if argv.size < 1
45
+ usage 'number of arguments must be at least 1'
46
+ end
47
+ @subcommand = :show
48
+ @pid = Integer(argv[0])
49
+ end
50
+ rescue => e
22
51
  usage e.message
23
52
  end
53
+ end
24
54
 
25
- if args.size < 1
26
- usage 'PID argument is required'
55
+ def run
56
+ parse_options
57
+ case @subcommand
58
+ when :show
59
+ show
60
+ when :test_duration
61
+ test_duration
62
+ else
63
+ assert(false)
27
64
  end
65
+ end
28
66
 
29
- [opts, args]
67
+ def show
68
+ puts GetProcessStartTime.start_time(@pid).to_f
30
69
  end
31
70
 
32
- def run
33
- opts, args = parse_options
34
- pid = args.first.to_i
35
- puts GetProcessStartTime.start_time(pid).to_f
71
+ def test_duration
72
+ start_time = GetProcessStartTime.start_time(@pid).to_f
73
+ living_duration = Time.now - start_time
74
+ case @comparator
75
+ when '-lt'
76
+ exit 0 if living_duration.to_f < @seconds
77
+ when '-gt'
78
+ exit 0 if living_duration.to_f > @seconds
79
+ end
80
+ $stderr.puts "test_duration failed (living duration:#{living_duration.to_f} sec)"
81
+ exit 1
82
+ end
83
+
84
+ def validate_comparator!(comparator)
85
+ unless %w[-lt -gt].include?(comparator)
86
+ raise "COMPARATOR must be -lt (less than) or -gt (greater than): #{comparator}"
87
+ end
88
+ comparator
36
89
  end
37
90
  end
38
91
  end
@@ -1,3 +1,3 @@
1
1
  class GetProcessStartTime
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: get_process_start_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo