Class: Proselytism::Converters::OpenOffice::Server

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/proselytism/converters/open_office.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/proselytism/converters/open_office.rb', line 159

def available?
  `ps -o pid,stat,command |grep soffice`.match(/\d+\s(\w)/i)[1] == "S"
end

#ensure_availableObject

Make sure there will be an available instance



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/proselytism/converters/open_office.rb', line 164

def ensure_available
  start! unless running?
  restart! if stalled?
  begin
    SystemTimer.timeout_after config.oo_server_availability_delay do
      while !available?
        log :debug, ". Waiting OpenOffice server availability"
        sleep(0.5)
      end
    end
  rescue Timeout::Error
    raise Error, "OpenOffice Server unavailable"
  end
  true
end

#perform(&block) ⇒ Object

Run a block with a timeout and retry if the first execution fails



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/proselytism/converters/open_office.rb', line 58

def perform(&block)
  attempts = 1
  begin
    ensure_available
    Timeout::timeout(config.oo_conversion_max_time,&block)
  rescue Timeout::Error, Proselytism::Converters::OpenOffice::Error
    attempts += 1
    restart!
    retry unless attempts > config.oo_conversion_max_tries
    raise Error, "OpenOffice server perform timeout"
  end
end

#restart!Object

Restart if running or start new instance



72
73
74
75
# File 'lib/proselytism/converters/open_office.rb', line 72

def restart!
  stop! if running?
  start!
end

#running?Boolean

Is OpenOffice server running?

Returns:

  • (Boolean)


137
138
139
# File 'lib/proselytism/converters/open_office.rb', line 137

def running?
  !`pgrep soffice`.blank?
end

#stalled?Boolean

Is the current instance stuck ?

Returns:

  • (Boolean)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/proselytism/converters/open_office.rb', line 143

def stalled?
  begin
    SystemTimer.timeout_after config.oo_server_max_cpu_delay do
      loop do
        cpu_usage = `ps -Ao pcpu,pid,comm= | grep soffice`.split(/\n/).map{|usage| /^\s*\d+/.match(usage)[0].strip.to_i}
        break unless cpu_usage.all?{|usage| usage > config.oo_server_max_cpu }
        sleep(0.2)
      end
    end
    false
  rescue
    log :error, "OpenOffice server stalled : \n---\n" + `ps -Ao pcpu,pid,comm | grep soffice` + "\n---"
    true
  end
end

#start!Object

Start new instance



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/proselytism/converters/open_office.rb', line 78

def start!
  log :debug, "OpenOffice server started" do
    system "#{config.open_office_path} -headless -accept=\"socket,host=127.0.0.1,port=8100\;urp\;\" -nofirststartwizard -nologo -nocrashreport -norestore -nolockcheck -nodefault &"
    begin
      SystemTimer.timeout_after(3) do
        while !running?
          log :debug, ". Waiting OpenOffice server to run"
          sleep(0.1)
        end
      end
    rescue
      raise Error, "Could not start OpenOffice"
    end
    # OpenOffice needs some time to wake up
    sleep(config.oo_server_start_delay)
  end
  nil
end

#start_with_running_control!Object



97
98
99
100
101
102
103
# File 'lib/proselytism/converters/open_office.rb', line 97

def start_with_running_control!
  if running?
    log :debug, "OpenOffice server is allready running"
  else
    start_without_running_control!
  end
end

#stop!Object

Kill running instance



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/proselytism/converters/open_office.rb', line 107

def stop!
  #operating_system = `uname -s`
  #command = "killall -u `whoami` -#{operating_system == "Linux" ? 'q' : 'm'} soffice"
  begin
    Timeout::timeout(3) do
      loop do
        system("killall -9 soffice > /dev/null 2>&1")
        system("killall -9 soffice.bin > /dev/null 2>&1")
        break unless running?
      end
    end
  rescue Timeout::Error
    raise Error, "Could not kill OpenOffice !!"
  ensure
    # Remove user profile
    system("rm -rf ~/openoffice.org*")
    log :debug, "OpenOffice server stopped"
  end
end

#stop_with_running_control!Object



127
128
129
130
131
132
133
# File 'lib/proselytism/converters/open_office.rb', line 127

def stop_with_running_control!
  if !running?
    log :debug, "OpenOffice server is allready stoped"
  else
    stop_without_running_control!
  end
end