ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/WebFeeder.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/WebFeeder.java (file contents):
Revision 1.14 by tdb, Fri Mar 16 16:43:50 2001 UTC vs.
Revision 1.18 by tdb, Fri Mar 23 00:44:11 2001 UTC

# Line 47 | Line 47 | public class WebFeeder extends Thread {
47       * Return a reference to the single class.
48       * Construct it if it does not already exist, otherwise just return the reference.
49       */
50 <    public static WebFeeder getInstance() {
50 >    public synchronized static WebFeeder getInstance() {
51          if (_instance == null){
52              _instance = new WebFeeder();
53          }
# Line 148 | Line 148 | public class WebFeeder extends Thread {
148              
149              // list the files and delete as appropriate
150              File alertsDir = new File(rootPath, alertSubDir);
151 +            // get all the hostnames directories
152              File[] contents = alertsDir.listFiles();
153              for(int i=0; i < contents.length; i++) {
154 +                // get a single directory from the array..
155                  File hostdir = contents[i];
156 +                // ..and check it's a directory
157                  if(hostdir.isDirectory()) {
158 +                    // if this is set, we clean files older than it
159 +                    long deleteFiles = -1;
160 +                    // get all the contents of that directory
161                      File[] hostdirContents = hostdir.listFiles();
162                      for(int j=0; j < hostdirContents.length; j++) {
163                          File alertFile = hostdirContents[j];
164 +                        // get the filename..
165                          String filename = alertFile.getName();
166 +                        // ..and see if it ends with OK or FINAL
167                          if(filename.endsWith(Alert.alertLevels[0]) ||
168                             filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) {
169 <                            // it ends with either OK or FINAL
169 >                            // it does end with either OK or FINAL
170                              // ... so we can check it for deletion
171                              long lastModified = alertFile.lastModified();
172                              long age = System.currentTimeMillis() - lastModified;
173                              if(age > ((long) deleteOlderThan*1000)) {
174 +                                // if we're on a final heartbeat, we probably want to
175 +                                // clean up any stale alerts left behind
176 +                                // by setting this flag, we'll clean them up on leaving this loop
177 +                                if(filename.endsWith(".HB."+Alert.alertLevels[Alert.alertLevels.length-1])) {
178 +                                    // we do this so that delete files is set to the
179 +                                    // latest date of a HB.FINAL. There should only be
180 +                                    // one of them though :)
181 +                                    if(lastModified > deleteFiles) {
182 +                                        deleteFiles = lastModified;
183 +                                    }
184 +                                }
185                                  // it's also older than our age to delete older than
186                                  if(!alertFile.delete()) {
187                                      _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath());
# Line 170 | Line 189 | public class WebFeeder extends Thread {
189                              }
190                          }
191                      }
192 +                    // cleanup stale alerts
193 +                    if(deleteFiles >= 0) {
194 +                        File[] remainingHostdirContents = hostdir.listFiles();
195 +                        for(int j=0; j < remainingHostdirContents.length; j++) {
196 +                            File alertFile = remainingHostdirContents[j];
197 +                            if(alertFile.lastModified() < deleteFiles) {
198 +                                // alert file is older than the most recent
199 +                                // FINAL Heartbeat alert.
200 +                                if(alertFile.delete()) {
201 +                                    _logger.write(this.toString(), Logger.DEBUG, "Deleted stale alert file: "+alertFile.getPath());
202 +                                }
203 +                                else {
204 +                                    _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'stale' alert file: "+alertFile.getPath());
205 +                                }
206 +                            }
207 +                        }
208 +                    }
209 +                    // ---- RECAP ----
210 +                    // at this point, we have cleaned up any OK or FINAL alerts
211 +                    // that have passed our age limit. We have then cleaned up
212 +                    // any alerts older the most recent Heartbeat FINAL alert,
213 +                    // as these are probably stale. Any files left are valid and
214 +                    // active alerts. We are now in a position to remove the host
215 +                    // directory if it's empty.
216 +                    // ---------------
217 +                    // do a quick check to see if the directory is now empty
218 +                    File[] newHostdirContents = hostdir.listFiles();
219 +                    if(newHostdirContents.length == 0) {
220 +                        // it does seem to be, try and delete it
221 +                        // this will fail anyway if files still remain
222 +                        if(!hostdir.delete()) {
223 +                            _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath());
224 +                        }
225 +                    }
226                  }
227              }
228          }
# Line 240 | Line 293 | public class WebFeeder extends Thread {
293          File outFile;
294          // check if we're at a special "end case" (OK or FINAL)
295          if(alert.getLevel()==0 || alert.getLevel()==Alert.alertLevels.length-1) {
296 +            if(alert.getAttributeName().equals("Heartbeat") && alert.getLevel()==Alert.alertLevels.length-1) {
297 +                // new file is something like alert.nnnnnnnn.HB.FINAL
298 +                outFile = new File(rootPath, destFile+".HB."+Alert.alertLevels[alert.getLevel()]);
299 +            } else {
300 +                // new file is something like alert.nnnnnnnn.OK
301 +                outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]);
302 +            }
303              File oldFile = new File(rootPath, destFile);
244            outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]);
304              if(!oldFile.renameTo(outFile)) {
305                  _logger.write(this.toString(), Logger.WARNING, "Failed to rename old file, "+oldFile.getPath()+" to new file, "+outFile.getPath());
306              }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines