Class: HotPotato::AppTaskServer

Inherits:
Object
  • Object
show all
Includes:
Core
Defined in:
lib/hot_potato/app_task_server.rb

Overview

This is used internally.

Constant Summary

Constants included from Core

Core::CONFIG_PATH

Instance Method Summary collapse

Methods included from Core

#acquire_lock, #config, #log, #queue_inject, #queue_subscribe, #release_lock, #set_logger, #stat

Constructor Details

#initializeAppTaskServer

Returns a new instance of AppTaskServer.



12
13
14
15
16
17
# File 'lib/hot_potato/app_task_server.rb', line 12

def initialize
  @options = load_options
  @options.app_task_name, @options.mode = parse_options 
  trap("INT") { shutdown }
  self.send(@options.mode)
end

Instance Method Details

#load_optionsObject



80
81
82
83
84
85
86
87
88
# File 'lib/hot_potato/app_task_server.rb', line 80

def load_options
  options = OpenStruct.new
  options.mode          = :run
  options.hostname      = Socket.gethostname
  options.routes        = HotPotato::Route.routes
  options.app_task_name = ""
  
  return options
end

#parse_optionsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hot_potato/app_task_server.rb', line 50

def parse_options
  mode = :run
  app_task_name = ""
  op = OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} app_task_name [run|start]"
    opts.on_tail("-h", "--help", "Show this message") do
      puts op
      exit!
    end
  end
  begin
    op.parse!
    app_task_name = ARGV.shift
    mode = (ARGV.shift || "run").to_sym
    if ![:start, :run].include?(mode)
      puts op
      exit!
    end
  rescue
    puts op
    exit!
  end
  return app_task_name, mode
end

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hot_potato/app_task_server.rb', line 28

def run
  $0 = "Hot Potato AppTask [#{classify(@options.app_task_name)}]"
  log.info "Starting Hot Potato AppTask #{HotPotato::VERSION} #{classify(@options.app_task_name)}"
  app_task = @options.routes.find @options.app_task_name
  if app_task
    obj = Kernel.const_get(classify(app_task.classname)).new
    begin
      if app_task.source
        obj.start app_task.source
      else
        obj.start
      end
    rescue Exception
      log.fatal $!
      exit 1
    end
  else
    puts "Could not find #{@options.app_task_name} in the route definition"
    exit 1
  end
end

#shutdownObject



75
76
77
78
# File 'lib/hot_potato/app_task_server.rb', line 75

def shutdown
  log.info "Stopping AppTask..."
  exit!
end

#startObject



19
20
21
22
23
24
25
26
# File 'lib/hot_potato/app_task_server.rb', line 19

def start
  set_logger(:queue_logger, :classname => classify(app_task_name))
  Process.daemon
  STDIN.reopen '/dev/null'
  STDOUT.sync = true
  STDERR.sync = true
  run
end