File: ReportServer.rb

package info (click to toggle)
tj3 3.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,048 kB
  • sloc: ruby: 36,481; javascript: 1,113; sh: 19; makefile: 17
file content (248 lines) | stat: -rw-r--r-- 6,837 bytes parent folder | download | duplicates (4)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = ReportServer.rb -- The TaskJuggler III Project Management Software
#
# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
#               by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

require 'taskjuggler/daemon/ProcessIntercom'
require 'taskjuggler/TjException'
require 'taskjuggler/TjTime'

class TaskJuggler

  class ReportServer

    include ProcessIntercom

    attr_reader :uri, :authKey

    def initialize(tj, logConsole = false)
      initIntercom

      @pid = nil
      @uri = nil

      # A reference to the TaskJuggler object that holds the project data.
      @tj = tj
      # This is set to the ID(s) of the reports that should be generated.
      @reportId = 'unknown'

      @lastPing = TjTime.new

      # We've started a DRb server before. This will continue to live somewhat
      # in the child. All attempts to create a DRb connection from the child
      # to the parent will end up in the child again. So we use a Pipe to
      # communicate the URI of the child DRb server to the parent. The
      # communication from the parent to the child is not affected by the
      # zombie DRb server in the child process.
      rd, wr = IO.pipe

      if (@pid = fork) == -1
        fatal('rs_fork_failed', 'ReportServer fork failed')
      elsif @pid.nil?
        if logConsole
          # If the Broker wasn't daemonized, log stdout and stderr to PID
          # specific files.
          $stderr.reopen("tj3d.rs.#{$$}.stderr", 'w')
          $stdout.reopen("tj3d.rs.#{$$}.stdout", 'w')
        end
        begin
          # This is the child
          $SAFE = 1
          DRb.install_acl(ACL.new(%w[ deny all
                                      allow 127.0.0.1 ]))
          iFace = ReportServerIface.new(self)
          begin
            uri = DRb.start_service('druby://127.0.0.1:0', iFace).uri
            debug('', "Report server is listening on #{uri}")
          rescue
            error('rs_cannot_start_drb',
                  "ReportServer can't start DRb: #{$!}")
          end

          # Send the URI of the newly started DRb server to the parent process.
          rd.close
          wr.write uri
          wr.close

          # Start a Thread that waits for the @terminate flag to be set and does
          # other background tasks.
          startTerminator
          startWatchDog

          # Cleanup the DRb threads
          DRb.thread.join
          debug('', 'Report server terminated')
          exit 0
        rescue => exception
          # TjRuntimeError exceptions are simply passed through.
          if exception.is_a?(TjRuntimeError)
            raise TjRuntimeError, $!
          end

          error('rs_unexp_excp',
                "ReportServer caught unexpected exception: #{$!}")
        end
      else
        Process.detach(@pid)
        # This is the parent
        wr.close
        @uri = rd.read
        rd.close
      end
    end

    def ping
      @lastPing = TjTime.new
    end

    def addFile(file)
      begin
        @tj.parseFile(file, :reportPropertiesFile)
      rescue TjRuntimeError
        return false
      end
      restartTimer
      true
    end

    def generateReport(id, regExpMode, formats, dynamicAttributes)
      info('generating_report', "Generating report #{id}")
      startTime = Time.now
      @reportId = id
      begin
        if (ok = @tj.generateReport(id, regExpMode, formats, dynamicAttributes))
          info('report_id_generated',
               "Report #{id} generated in #{Time.now - startTime} seconds")
        else
          error('report_generation_failed', "Report generation of #{id} failed")
        end
      rescue TjRuntimeError
        return false
      end
      restartTimer
      ok
    end

    def listReports(id, regExpMode)
      info('listing_report_id', "Listing report #{id}")
      begin
        if (ok = @tj.listReports(id, regExpMode))
          debug('', "Report list for #{id} generated")
        else
          error('repor_list_comp_failed',
                "Report list compilation of #{id} failed")
        end
      rescue TjRuntimeError
        return false
      end
      restartTimer
      ok
    end

    def checkTimeSheet(sheet)
      info('check_time_sheet', "Checking time sheet #{sheet}")
      @reportId = 'timesheet'
      begin
        ok = @tj.checkTimeSheet(sheet)
        debug('', "Time sheet #{sheet} is #{ok ? '' : 'not '}ok")
      rescue TjRuntimeError
        return false
      end
      restartTimer
      ok
    end

    def checkStatusSheet(sheet)
      info('check_status_sheet', "Checking status sheet #{sheet}")
      @reportId = 'statussheet'
      begin
        ok = @tj.checkStatusSheet(sheet)
        debug('', "Status sheet #{sheet} is #{ok ? '' : 'not '}ok")
      rescue TjRuntimeError
        return false
      end
      restartTimer
      ok
    end

    private

    def startWatchDog
      Thread.new do
        loop do
          if TjTime.new - @lastPing > 120
            # Since the abort via error() is not thread safe, we issue a
            # warning and abort manually.
            warning('ps_heartbeat_lost',
                    "Report server (Project #{@tj.project['projectid']} " +
                    "report #{@reportId}) lost heartbeat " +
                    'from ProjectServer. Terminating.')
            exit 1
          end
          sleep 30
        end
      end
    end

  end

  class ReportServerIface

    include ProcessIntercomIface

    def initialize(server)
      @server = server
    end

    def ping(authKey)
      return false unless @server.checkKey(authKey, 'addFile')

      trap { @server.ping }
    end

    def addFile(authKey, file)
      return false unless @server.checkKey(authKey, 'addFile')

      trap { @server.addFile(file) }
    end

    def generateReport(authKey, reportId, regExpMode, formats,
                       dynamicAttributes)
      return false unless @server.checkKey(authKey, 'generateReport')

      trap do
        @server.generateReport(reportId, regExpMode, formats, dynamicAttributes)
      end
    end


    def listReports(authKey, reportId, regExpMode)
      return false unless @server.checkKey(authKey, 'generateReport')

      trap { @server.listReports(reportId, regExpMode) }
    end

    def checkTimeSheet(authKey, sheet)
      return false unless @server.checkKey(authKey, 'checkTimeSheet')

      trap { @server.checkTimeSheet(sheet) }
    end

    def checkStatusSheet(authKey, sheet)
      return false unless @server.checkKey(authKey, 'checkStatusSheet')

      trap { @server.checkStatusSheet(sheet) }
    end

  end

end