File: basic_process.rb

package info (click to toggle)
ruby-aruba 2.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 968 kB
  • sloc: javascript: 6,850; ruby: 4,010; makefile: 5
file content (148 lines) | stat: -rw-r--r-- 3,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# frozen_string_literal: true

require 'aruba/platform'
require 'shellwords'

# Aruba
module Aruba
  # Processes
  module Processes
    # Basic Process
    #
    # `BasicProcess` is not meant for direct use - `BasicProcess.new` - by users.
    #
    # @private
    class BasicProcess
      attr_reader :exit_status, :environment, :working_directory, :main_class,
                  :io_wait_timeout, :exit_timeout, :startup_wait_time, :stop_signal

      def initialize(cmd, exit_timeout, io_wait_timeout, working_directory, # rubocop:disable Metrics/ParameterLists
                     environment = Aruba.platform.environment_variables.hash_from_env,
                     main_class = nil, stop_signal = nil, startup_wait_time = 0)
        @cmd               = cmd
        @working_directory = working_directory
        @environment       = environment
        @main_class        = main_class
        @exit_status       = nil
        @stop_signal       = stop_signal
        @startup_wait_time = startup_wait_time

        @exit_timeout    = exit_timeout
        @io_wait_timeout = io_wait_timeout

        @started         = false
        @timed_out       = false
      end

      # Return command line
      def commandline
        @cmd
      end

      # Output pid of process
      def pid
        raise NotImplementedError
      end

      # Output stderr and stdout
      def output(opts = {})
        stdout(opts) + stderr(opts)
      end

      def write(*)
        raise NotImplementedError
      end

      def stdin(*)
        raise NotImplementedError
      end

      def stdout(*)
        raise NotImplementedError
      end

      def stderr(*)
        raise NotImplementedError
      end

      def close_io(*)
        raise NotImplementedError
      end

      def send_signal(*)
        raise NotImplementedError
      end

      def filesystem_status
        raise NotImplementedError
      end

      def content
        raise NotImplementedError
      end

      def wait
        raise NotImplementedError
      end

      # Restart a command
      def restart
        stop
        start
      end

      # Was process already stopped
      def stopped?
        @started == false
      end

      # Was process already started
      def started?
        @started == true
      end

      # Does the process failed to stop in time
      def timed_out?
        @timed_out == true
      end

      # Hook which is run before command is run
      def before_run; end

      # Hook which is run after command is run
      def after_run; end

      def inspect
        out = truncate(stdout(wait_for_io: 0).inspect, 35)
        err = truncate(stderr(wait_for_io: 0).inspect, 35)

        fmt = '#<%s:%s commandline="%s": stdout=%s stderr=%s>'
        format fmt, self.class, object_id, commandline, out, err
      end

      alias to_s inspect

      def command
        Shellwords.split(commandline).first
      end

      def arguments
        return Shellwords.split(commandline)[1..] if Shellwords.split(commandline).size > 1

        []
      end

      def empty?
        false
      end

      private

      def truncate(string, max_length)
        return string if string.length <= max_length

        "#{string[0, max_length - 1]} ..."
      end
    end
  end
end