Class: TaskJuggler::Tj3Daemon

Inherits:
Tj3AppBase show all
Defined in:
lib/taskjuggler/apps/Tj3Daemon.rb

Instance Method Summary collapse

Methods inherited from Tj3AppBase

#main

Methods included from MessageHandler

#critical, #debug, #error, #fatal, #info, #warning

Constructor Details

#initializeTj3Daemon

Returns a new instance of Tj3Daemon.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/taskjuggler/apps/Tj3Daemon.rb', line 26

def initialize
  super
  @mandatoryArgs = '[<tjp file> [<tji file> ...] ...]'

  @mhi = MessageHandlerInstance.instance
  @mhi.logFile = File.join(Dir.getwd, "/#{AppConfig.appName}.log")
  @mhi.appName = AppConfig.appName
  # By default show only warnings and more serious messages.
  @mhi.outputLevel = :warning
  @daemonize = true
  @uriFile = File.join(Dir.getwd, '.tj3d.uri')
  @port = nil
  @webServer = false
  @webServerPort = 8080
  @webdPidFile = File.join(Dir.getwd, ".tj3webd-#{$$}.pid").untaint
end

Instance Method Details

#appMain(files) ⇒ Object



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
# File 'lib/taskjuggler/apps/Tj3Daemon.rb', line 85

def appMain(files)
  broker = ProjectBroker.new
  @rc.configure(self, 'global')
  @rc.configure(@mhi, 'global.log')
  @rc.configure(broker, 'global')
  @rc.configure(broker, 'daemon')

  # Set some config variables if corresponding data was provided via the
  # command line.
  broker.port = @port if @port
  broker.uriFile = @uriFile.untaint
  broker.projectFiles = sortInputFiles(files) unless files.empty?
  broker.daemonize = @daemonize
  # Create log files for standard IO for each child process if the daemon
  # is not disconnected from the terminal.
  broker.logStdIO = !@daemonize

  if @webServer
    webdCommand = "tj3webd --webserver-port #{@webServerPort} " +
                           "--pidfile #{@webdPidFile}"
    # Also start the web server as a separate process. We keep the PID, so
    # we can terminate that process again when we exit the daemon.
    begin
      `#{webdCommand}`
    rescue
      error('tj3webd_start_failed', "Could not start tj3webd: #{$!}")
    end
    info('web_server_started', "Web server started as '#{webdCommand}'")
  end

  broker.start

  if @webServer
    pid = nil
    begin
      # Read the PID of the web server from the PID file.
      File.open(@webdPidFile, 'r') do |f|
        pid = f.read.to_i
      end
    rescue
      warning('cannot_read_webd_pidfile',
              "Cannot read tj3webd PID file (#{@webdPidFile}): #{$!}")
    end
    # If we have started the web server, we are also trying to terminate
    # that process again.
    begin
      Process.kill("TERM", pid)
    rescue
      warning('tj3webd_term_failed',
              "Could not terminate web server: #{$!}")
    end
    info('web_server_terminated', "Web server with PID #{pid} terminated")
  end

  0
end

#processArguments(argv) ⇒ Object



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
# File 'lib/taskjuggler/apps/Tj3Daemon.rb', line 43

def processArguments(argv)
  super do
    @opts.banner.prepend(<<'EOT'
The TaskJuggler daemon can be used to quickly generate reports for a number
of scheduled projects that are resident in memory. Once the daemon has been
started tj3client can be used to control it.

EOT
	)
    @opts.on('-d', '--dont-daemonize',
             format("Don't put program into daemon mode. Keep it " +
                    'connected to the terminal and show debug output.')) do
      @daemonize = false
    end
    @opts.on('-p', '--port <NUMBER>', Integer,
             format('Use the specified TCP/IP port to serve tj3client ' +
                    'requests (Default: 8474).')) do |arg|
      @port = arg
    end
    @opts.on('--logfile <FILE NAME>', String,
                      format('Log daemon messages to the specified file.')) do |arg|
          @mhi.logFile = arg
          end
    @opts.on('--urifile <FILE NAME>', String,
             format('If the port is 0, use this file to store the URI ' +
                    'of the server.')) do |arg|
      @uriFile = arg
    end
    @opts.on('-w', '--webserver',
             format('Start a web server that serves the reports of ' +
                    'the loaded projects.')) do
      @webServer = true
    end
    @opts.on('--webserver-port <NUMBER>', Integer,
             format('Use the specified TCP/IP port to serve web browser ' +
                    'requests (Default: 8080).')) do |arg|
      @webServerPort = arg
    end

  end
end