Ignore:
Timestamp:
06/21/11 12:52:38 (13 years ago)
Author:
ogrimm
Message:
Updates to Edd. History server now periodically saves histories to disk.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/Evidence/History.cc

    r10996 r11088  
    3131const string DEFAULT_MAX_SIZE_KB = "2000";
    3232const string DEFAULT_NUM_ENTRIES = "1000";      // Number of entries in each history buffer
     33const double MIN_SAVE_PERDIOD = 0.5;                    // Minimum period between saving history buffers in hours
    3334
    3435//
     
    4445          double MinAbsChange;
    4546          string Format;
     47          time_t LastSave;
    4648        };
    4749        map<string, struct Item> Map;
     
    5557        void AddService(string, const char *);
    5658        void RemoveService(string);
     59        void SaveHistory(string);
    5760        off_t FileSize(FILE *);
    5861        FILE *OpenFile(string, const char *);
     
    7376  GetConfig("maxsize_kb", DEFAULT_MAX_SIZE_KB);
    7477  GetConfig("numentries", DEFAULT_NUM_ENTRIES);
     78  GetConfig("saveperiod", "1");
    7579  GetConfig("exclude", "");
    7680
     
    9599  // Check if service available
    96100  if (!ServiceOK(I)) return;
    97 
     101 
    98102  // ====== Part A: Handle service subscriptions ===
    99103 
     
    143147  if (Map.count(Service) == 0 || I->getSize()==0 || I->getTimestamp()<=0) return;
    144148
     149  // Save history buffers periodically (in case of program crash)
     150  if (time(NULL)-Map[Service].LastSave > max(atof(GetConfig("saveperiod").c_str()),MIN_SAVE_PERDIOD)*3600) {
     151    Map[Service].LastSave = time(NULL);
     152        SaveHistory(Service);
     153  }
     154
    145155  // Resize buffer if necessary
    146156  int NEntries = atoi(GetConfig("numentries").c_str());
     
    266276  Map[Name].Format = Format;
    267277  Map[Name].MinAbsChange = 0.0;
     278  Map[Name].LastSave = 0.0;
    268279 
    269280  // Set minimum required change if given in configuratrion
     
    319330
    320331  // Save history buffer
     332  SaveHistory(Name);
     333
     334  Map.erase(Name);
     335}
     336
     337
     338//
     339// Save history buffer to file
     340//
     341void History::SaveHistory(string Name) {
     342
    321343  FILE *File = OpenFile(Name, "wb");
     344
    322345  if (File != NULL) {
    323346    fwrite(&Map[Name].Next, sizeof(Map[Name].Next), 1, File);                                   // Next pointer
     
    327350        // If error, try to delete (possibly erroneous) file
    328351        if (ferror(File) != 0) {
    329           if (remove(Name.c_str()) == -1) Message(WARN, "Error writing history file '%s' in RemoveService(), could also not delete file", Name.c_str());
    330           else Message(WARN, "Error writing history file '%s' in RemoveService(), deleted file", Name.c_str());
    331         }
    332     if (fclose(File) != 0) Message(WARN, "Error closing history file '%s' in RemoveService()", Name.c_str());;
    333   }
    334 
    335   Map.erase(Name);
    336 }
     352          if (remove(Name.c_str()) == -1) Message(WARN, "Error writing history file '%s', could also not delete file", Name.c_str());
     353          else Message(WARN, "Error writing history file '%s', deleted file", Name.c_str());
     354        }
     355    if (fclose(File) != 0) Message(WARN, "Error closing history file '%s'", Name.c_str());
     356  }
     357  else Message(WARN, "Could not open history file '%s' for writing", Name.c_str());;
     358}
     359
    337360
    338361//
Note: See TracChangeset for help on using the changeset viewer.