Class: ProcessWanker::Service

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/service.rb

Direct Known Subclasses

DummyService, ProcessService, UpstartService

Constant Summary

Constants included from Log

Log::DEBUG, Log::ERROR, Log::INFO, Log::WARN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

debug, error, info, log, set_level, warn

Constructor Details

#initialize(iparams) ⇒ Service



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
# File 'lib/service.rb', line 63

def initialize(iparams)

# extract params
extract_params(
  iparams,
  [
    :name,
    :tags,
    :group_name,
    :min_action_delay_secs,
    :start_grace_secs,
    :stop_grace_secs,
    :stable_secs,
    :fail_trigger_count,
    :fail_suppress_secs,
    :initial_state,
    :dependencies,
    :log_file,
    :watchdog_file,
    :watchdog_timeout_secs
  ])

  # warn about extra params
iparams.keys.each do |k|
  warn "warning: ignoring unrecognized parameter: #{k.to_s}"
end

  # apply defaults
  @params={
    :min_action_delay_secs      =>    1,
    :stable_secs                =>    20,
    :fail_trigger_count         =>    5,
    :fail_suppress_secs         =>    300,
    :group_name                 =>    "default",
    :start_grace_secs           =>   5,
    :stop_grace_secs            =>    5,
    :dependencies               =>   [],
  }.merge(@params)

@params[:tags] ||= []

# convert necessary things to symbols
@params[:initial_state] = @params[:initial_state].to_sym if(@params[:initial_state])

  # set state values
  @last_action_time=Time.at(0)
  @last_action=nil
  @current_state=safe_do_ping()
  @want_state=@params[:initial_state] || @current_state
  @prev_state=safe_do_ping()
  @last_transition_time=Time.at(0)
  @attempt_count=0
  @last_fail_time=Time.at(0)
  @show_state=""
  @suppress=false
@want_state_mode=:boot
@stable=false

  # register with the manager
  ServiceMgr::register_service(self)
end

Instance Attribute Details

#attempt_countObject

Returns the value of attribute attempt_count.



31
32
33
# File 'lib/service.rb', line 31

def attempt_count
  @attempt_count
end

#config_nodeObject

Returns the value of attribute config_node.



37
38
39
# File 'lib/service.rb', line 37

def config_node
  @config_node
end

#current_stateObject

Returns the value of attribute current_state.



28
29
30
# File 'lib/service.rb', line 28

def current_state
  @current_state
end

#dependenciesObject

Returns the value of attribute dependencies.



34
35
36
# File 'lib/service.rb', line 34

def dependencies
  @dependencies
end

#ignoreObject

Returns the value of attribute ignore.



36
37
38
# File 'lib/service.rb', line 36

def ignore
  @ignore
end

#last_action_timeObject

Returns the value of attribute last_action_time.



27
28
29
# File 'lib/service.rb', line 27

def last_action_time
  @last_action_time
end

#last_fail_timeObject

Returns the value of attribute last_fail_time.



32
33
34
# File 'lib/service.rb', line 32

def last_fail_time
  @last_fail_time
end

#last_transition_timeObject

Returns the value of attribute last_transition_time.



30
31
32
# File 'lib/service.rb', line 30

def last_transition_time
  @last_transition_time
end

#paramsObject

current state



25
26
27
# File 'lib/service.rb', line 25

def params
  @params
end

#prev_stateObject

Returns the value of attribute prev_state.



29
30
31
# File 'lib/service.rb', line 29

def prev_state
  @prev_state
end

#show_stateObject

Returns the value of attribute show_state.



33
34
35
# File 'lib/service.rb', line 33

def show_state
  @show_state
end

#stableObject

Returns the value of attribute stable.



39
40
41
# File 'lib/service.rb', line 39

def stable
  @stable
end

#suppressObject

Returns the value of attribute suppress.



35
36
37
# File 'lib/service.rb', line 35

def suppress
  @suppress
end

#want_stateObject

Returns the value of attribute want_state.



26
27
28
# File 'lib/service.rb', line 26

def want_state
  @want_state
end

#want_state_modeObject

Returns the value of attribute want_state_mode.



38
39
40
# File 'lib/service.rb', line 38

def want_state_mode
  @want_state_mode
end

Instance Method Details

#check_action_delay(now, proposed_action) ⇒ Object

check the various timers, and see if we’re allowed to take action on a specific service at this point…



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/service.rb', line 437

def check_action_delay(now,proposed_action)

  elapsed=now - @last_action_time

  # check general-purpose between-action-delay
  return(false) if(elapsed < @params[:min_action_delay_secs])

  if(proposed_action == @last_action)

    # check grace periods
    return(false) if(@last_action == :start && elapsed < @params[:start_grace_secs])
    return(false) if(@last_action == :stop && elapsed < @params[:stop_grace_secs])

    # check failing suppression
    since_fail=now - @last_fail_time
    return(false) if(since_fail < @params[:fail_suppress_secs])
  end

  true
end

#do_pingObject

return state (:up, :down)



200
201
202
# File 'lib/service.rb', line 200

def do_ping
  raise "method not defined in base class"
end

#do_start(attempt_ct) ⇒ Object

start the service. should not block for a considerable amount of time



180
181
182
# File 'lib/service.rb', line 180

def do_start(attempt_ct)
  raise "method not defined in base class"
end

#do_stop(attempt_ct) ⇒ Object

stop the service - should not block for a considerable amount of time



190
191
192
# File 'lib/service.rb', line 190

def do_stop(attempt_ct)
  raise "method not defined in base class"
end

#extract_params(params, keys) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/service.rb', line 146

def extract_params(params,keys)
  @params ||= {}
  keys.each do |k|
    @params[k]=params[k] if(params.has_key?(k))
    params.delete(k)
  end
end

#group_nameObject

return the name of the group this process belongs to



170
171
172
# File 'lib/service.rb', line 170

def group_name
  @params[:group_name]
end

#matches_single(p, prev) ⇒ Object



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/service.rb', line 507

def matches_single(p,prev)

  if(p == "all")
    return(true)
  elsif(p[0..0] == "/")
    r=Regexp.new(p.split("/")[1])
    return(true) if(group_name =~ r)
    return(true) if(name =~ r)
  elsif(p[0..0] == "~")
    return(prev && !matches_single(p[1..-1],false))
  elsif(p[0..3] == "tag:")
    tag=p[4..-1]
      return(true) if(params[:tags] && params[:tags].include?(tag))
  elsif(p == name)
    return(true)
  elsif(p == group_name)
    return(true)
  end

  prev
end

#matches_spec(spec) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/service.rb', line 482

def matches_spec(spec)

  # ensure it's in array form
  spec=spec.split(",")

  # check for inversion on first item
  if(spec.first[0..0]=="~")
    # insert implicit "all" at front
    spec=["all"] + spec
  end

  matches=false
  spec.each do |p|
    matches = matches_single(p,matches)
  end
  matches

end

#nameObject

return the host-wide unique name of the service



160
161
162
# File 'lib/service.rb', line 160

def name
  @params[:name]
end

#resolve_dependenciesObject

resolve dependencies through name lookup



210
211
212
213
214
215
216
217
218
# File 'lib/service.rb', line 210

def resolve_dependencies

  @dependencies=[]
  params[:dependencies].each do |dep|
  ServiceMgr::instance.match_services(dep.service).each do |k,v|
    @dependencies << { :service => v, :dep => dep }
  end
  end
end

#safe_do(name) ⇒ Object

safe, exception-catching methods



226
227
228
229
230
# File 'lib/service.rb', line 226

def safe_do(name)
  ProcessWanker::with_logged_rescue("#{name} - safe_do") do
    yield
  end
end

#safe_do_pingObject



240
241
242
243
244
# File 'lib/service.rb', line 240

def safe_do_ping()
  p=:down
  safe_do("#{name}:do_start") { p=do_ping }
  p
end

#safe_do_start(attempt_ct) ⇒ Object



232
233
234
# File 'lib/service.rb', line 232

def safe_do_start(attempt_ct)
  safe_do("#{name}:do_start") { do_start(attempt_ct) }
end

#safe_do_stop(attempt_ct) ⇒ Object



236
237
238
# File 'lib/service.rb', line 236

def safe_do_stop(attempt_ct)
  safe_do("#{name}:do_start") { do_stop(attempt_ct) }
end

#set_want_state(state) ⇒ Object

set want state in response to a user request - clear all delay state, start with a clean slate.



465
466
467
468
469
470
471
472
473
474
# File 'lib/service.rb', line 465

def set_want_state(state)
  @want_state = state
@want_state_mode = :user
  @attempt_count = 0
  @last_action_time = Time.at(0)
  @last_fail_time = Time.at(0)
  if(@want_state != @current_state)
    @show_state = "received #{state.inspect}"
  end
end

#tickObject

main logic



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/service.rb', line 252

def tick

  #
  # get current time
  #

  now=Time.now

  #
  # get current state, check for change, record transition time
  #

  state = safe_do_ping()
  if(@current_state != state)
    @prev_state = @current_state
    @last_transition_time = now
    @current_state = state
  @stable = false
     @show_state = state.to_s
  end

  #
  # handle special :restart case
  #

  want=@want_state
  if(want == :restart)
    if(@current_state == :down)
      want = :up
      @want_state = :up
      @last_action_time = Time.now
    else
      want = :down
    end
  end

#
# check dependencies
#

deps_ok=true
@dependencies.each do |d|
  s=d[:service]
  if(s.current_state != :up)
    deps_ok=false
    next
  end
  tt=now - s.last_transition_time
  if(tt < d[:dep].up_for)
    deps_ok=false
  end
end
@suppress=(!deps_ok) && (@want_state != :down)
if(@suppress)
  want = :down
end

#
# have we been in the same state for a while?
#

stabilized=false
elapsed = now - @last_transition_time
   if(!@stable && elapsed >= @params[:stable_secs])
  stabilized=true
  @stable=true
end

#
# if we're up and stable, check watchdog timer
#

if(@stable && @current_state == :up && want == :up)
  if(@params[:watchdog_file] && @params[:watchdog_timeout_secs])
    timeout=true
    begin
      st = File.stat(@params[:watchdog_file])
      if((now - st.mtime) < @params[:watchdog_timeout_secs])
        timeout=false
      end
    rescue Exception => e
    end
    if(timeout)
      info("#{self.name}: watchdog file: #{@params[:watchdog_file]} has timed out")
      Event.dispatch("watchdog_timeout",self)
      @want_state = :restart
      want = :down
    end
  end
end

  #
  # are we in the desired state?
  #

  if(@current_state == want)

  # did we just stabilize?
  if(stabilized)

      @attempt_count = 0
      @show_state = @current_state.to_s + " [stable]"

    # was this request part of a user request? if not, notify
    if(@want_state_mode == :none && want == :up)
      Event::dispatch("restarted",self)
    end

    # clear request mode
    @want_state_mode=:none
    end

  # nothing more to do
    return
  end

#
# are we ignoring the process?
#

if(want == :ignore)
  @show_state = "#{@current_state.to_s} (ignored)"
  return
end

  #
  # is it too soon to do anything?
  #

proposed_action = { :up => :start , :down => :stop }[want]
return if(!check_action_delay(now,proposed_action))

  #
  # actually attempt to cause a change
  #

# update state
@attempt_count=0 if(proposed_action != @last_action)
@last_action=proposed_action
@last_action_time=now

# check for failing
   if(@attempt_count >= @params[:fail_trigger_count])
  info("#{self.name} has now had #{@attempt_count} attempts. considering it failed.")

     @show_state = "failing(#{proposed_action})"
     @last_fail_time = now
     @attempt_count = 0   # reset for next time

  Event::dispatch("fail-#{proposed_action.to_s}",self)

     return
   end

# was this request part of a user request? if not, notify
if(@want_state_mode == :none && proposed_action == :start && @attempt_count==0)
  Event::dispatch("restarting",self)
end

   # do it
@show_state = "#{proposed_action} [#{@attempt_count}]"
if(proposed_action == :start)

  Event::dispatch("pre-launch",self)

  info("calling do_start for #{self.name}")
  safe_do_start(@attempt_count)

else

  info("calling do_stop for #{self.name}")
  safe_do_stop(@attempt_count)

end
   @attempt_count += 1

end

#validate_paramsObject



131
132
133
134
135
136
137
138
# File 'lib/service.rb', line 131

def validate_params()
  raise "no name provided" if(!params[:name])
  @params.keys.each do |k|
    if(!keys.include?(k))
      warn "warning: unrecognized parameter #{k.to_s} ignored"
    end
  end
end