Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5327


Ignore:
Timestamp:
01/18/11 16:52:20 (14 years ago)
Author:
ascheibe
Message:

#1233 add auto cleanup to PluginCache

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/PluginCache.cs

    r5325 r5327  
    2626using System.Reflection;
    2727using System.Runtime.CompilerServices;
     28using System.Runtime.Serialization;
     29using System.Runtime.Serialization.Formatters.Binary;
    2830using System.Threading;
    2931using HeuristicLab.Services.Hive.Common.DataTransfer;
     
    3335    private static object locker = new object();
    3436    public const string ConfigFileName = "Sandbox.config";
     37    private const string lastUsedFileName = "lastUsed.dat";
     38
     39    //maximum number of days after which a plugin gets deleted if not used
     40    private const int maxAge = 5;
    3541
    3642    private static PluginCache instance = null;
     
    95101          string curPath = Path.Combine(PluginCacheDir, requestedPlugin.Id.ToString());
    96102          if (Directory.Exists(curPath)) {
     103            writeDateLastUsed(curPath);
     104
    97105            foreach (string file in Directory.GetFiles(curPath)) {
    98106              string fn = getFilenameFromPath(file);
    99               File.Copy(file, Path.Combine(targetDir, fn));
     107              if (fn != lastUsedFileName)
     108                File.Copy(file, Path.Combine(targetDir, fn));
    100109            }
    101110          }
     
    183192      }
    184193    }
     194
     195    /// <summary>
     196    /// creates a file in path with the current date;
     197    /// this can later be used to find plugins which are outdated
     198    /// </summary>   
     199    private void writeDateLastUsed(string path) {
     200      FileStream fs = new FileStream(Path.Combine(path, lastUsedFileName), FileMode.Create);
     201      BinaryFormatter formatter = new BinaryFormatter();
     202
     203      try {
     204        formatter.Serialize(fs, DateTime.Now);
     205      }
     206      catch (SerializationException) {
     207        //rethrow...
     208        throw;
     209      }
     210      finally {
     211        fs.Close();
     212      }
     213    }
     214
     215    /// <summary>
     216    /// checks the pluginCacheDirectory and deletes plugin folders which are not used anymore
     217    /// </summary>
     218    /// <param name="path"></param>
     219    private void cleanPluginCache() {
     220      FileStream fs = null;
     221      DateTime luDate;
     222      bool changed = false;
     223
     224      if (Directory.Exists(PluginCacheDir)) {
     225        foreach (string dir in Directory.EnumerateDirectories(PluginCacheDir)) {
     226          try {
     227            fs = new FileStream(Path.Combine(dir, lastUsedFileName), FileMode.Open);
     228            BinaryFormatter formatter = new BinaryFormatter();
     229            luDate = (DateTime)formatter.Deserialize(fs);
     230            fs.Close();
     231
     232            if (luDate.AddDays(maxAge) < DateTime.Now) {
     233              Directory.Delete(dir, true);
     234              changed = true;
     235            }
     236          }
     237          catch (SerializationException) {
     238            fs.Close();
     239            throw;
     240          }
     241          catch (System.IO.FileNotFoundException) {
     242            //nerver used
     243            Directory.Delete(dir, true);
     244            changed = true;
     245          }
     246          catch (Exception) {
     247            throw;
     248          }
     249        }
     250      }
     251      if (changed)
     252        DoUpdateRun();
     253    }
     254
    185255
    186256    internal void DeletePluginsForJob(Guid id) {
     
    203273        SlaveClientCom.Instance.ClientCom.LogMessage("failed while unloading " + id + " with exception " + ex);
    204274      }
     275      cleanPluginCache();
    205276    }
    206277  }
Note: See TracChangeset for help on using the changeset viewer.