Files
Umbraco-CMS/src/Umbraco.Web/Install/InstallStatusTracker.cs

146 lines
5.0 KiB
C#
Raw Normal View History

2014-02-26 18:25:59 +11:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
2014-02-26 18:25:59 +11:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
2014-03-04 16:21:45 +11:00
using Umbraco.Core;
using Umbraco.Core.IO;
2014-02-26 18:25:59 +11:00
using Umbraco.Web.Install.Models;
namespace Umbraco.Web.Install
{
/// <summary>
/// An internal in-memory status tracker for the current installation
/// </summary>
internal static class InstallStatusTracker
{
2014-03-04 16:21:45 +11:00
private static ConcurrentHashSet<InstallTrackingItem> _steps = new ConcurrentHashSet<InstallTrackingItem>();
2014-02-26 18:25:59 +11:00
2014-03-04 16:21:45 +11:00
private static string GetFile(Guid installId)
2014-02-26 18:25:59 +11:00
{
var file = IOHelper.MapPath("~/App_Data/TEMP/Install/"
2014-03-04 16:21:45 +11:00
+ "install_"
+ installId.ToString("N")
+ ".txt");
return file;
}
public static void Reset()
{
2014-03-04 16:21:45 +11:00
_steps = new ConcurrentHashSet<InstallTrackingItem>();
ClearFiles();
}
public static void ClearFiles()
{
var dir = IOHelper.MapPath("~/App_Data/TEMP/Install/");
if (Directory.Exists(dir))
{
var files = Directory.GetFiles(dir);
foreach (var f in files)
{
File.Delete(f);
}
}
else
2014-03-04 16:21:45 +11:00
{
Directory.CreateDirectory(dir);
2014-03-04 16:21:45 +11:00
}
}
public static IEnumerable<InstallTrackingItem> InitializeFromFile(Guid installId)
{
//check if we have our persisted file and read it
var file = GetFile(installId);
if (File.Exists(file))
{
var deserialized = JsonConvert.DeserializeObject<IEnumerable<InstallTrackingItem>>(
File.ReadAllText(file));
foreach (var item in deserialized)
{
_steps.Add(item);
}
}
else
{
throw new InvalidOperationException("Cannot initialize from file, the installation file with id " + installId + " does not exist");
}
return new List<InstallTrackingItem>(_steps);
}
2014-03-04 16:21:45 +11:00
public static IEnumerable<InstallTrackingItem> Initialize(Guid installId, IEnumerable<InstallSetupStep> steps)
{
//if there are no steps in memory
if (_steps.Count == 0)
2014-02-26 18:25:59 +11:00
{
//check if we have our persisted file and read it
2014-03-04 16:21:45 +11:00
var file = GetFile(installId);
if (File.Exists(file))
{
2014-03-04 16:21:45 +11:00
var deserialized = JsonConvert.DeserializeObject<IEnumerable<InstallTrackingItem>>(
File.ReadAllText(file));
foreach (var item in deserialized)
{
2014-03-04 16:21:45 +11:00
_steps.Add(item);
}
}
else
{
ClearFiles();
//otherwise just create the steps in memory (brand new install)
2014-03-04 16:21:45 +11:00
foreach (var step in steps.OrderBy(x => x.ServerOrder))
{
_steps.Add(new InstallTrackingItem(step.Name, step.ServerOrder));
}
//save the file
2014-03-04 16:21:45 +11:00
var serialized = JsonConvert.SerializeObject(new List<InstallTrackingItem>(_steps));
Directory.CreateDirectory(Path.GetDirectoryName(file));
File.WriteAllText(file, serialized);
}
}
else
{
//ensure that the file exists with the current install id
var file = GetFile(installId);
if (File.Exists(file) == false)
{
ClearFiles();
//save the correct file
var serialized = JsonConvert.SerializeObject(new List<InstallTrackingItem>(_steps));
Directory.CreateDirectory(Path.GetDirectoryName(file));
File.WriteAllText(file, serialized);
}
2014-02-26 18:25:59 +11:00
}
2014-03-04 16:21:45 +11:00
return new List<InstallTrackingItem>(_steps);
2014-02-26 18:25:59 +11:00
}
2014-03-04 16:21:45 +11:00
public static void SetComplete(Guid installId, string name, IDictionary<string, object> additionalData = null)
2014-02-26 18:25:59 +11:00
{
2014-03-04 16:21:45 +11:00
var trackingItem = _steps.Single(x => x.Name == name);
if (additionalData != null)
{
trackingItem.AdditionalData = additionalData;
}
2014-03-04 16:21:45 +11:00
trackingItem.IsComplete = true;
//save the file
2014-03-04 16:21:45 +11:00
var file = GetFile(installId);
var serialized = JsonConvert.SerializeObject(new List<InstallTrackingItem>(_steps));
File.WriteAllText(file, serialized);
2014-02-26 18:25:59 +11:00
}
2014-03-04 16:21:45 +11:00
public static IEnumerable<InstallTrackingItem> GetStatus()
2014-02-26 18:25:59 +11:00
{
2014-03-04 16:21:45 +11:00
return new List<InstallTrackingItem>(_steps).OrderBy(x => x.ServerOrder);
2014-02-26 18:25:59 +11:00
}
}
}