/// <summary> /// /// </summary> /// <param name="settings"></param> /// <param name="udApiService"></param> /// <param name="adapterPlugin"></param> /// <param name="stateManager"></param> /// <param name="suspensionManager"></param> /// <param name="streamHealthCheckValidation"></param> /// <param name="fixtureValidation"></param> public static void Init( ISettings settings, IServiceFacade udApiService, IAdapterPlugin adapterPlugin, IStateManager stateManager, ISuspensionManager suspensionManager, IStreamHealthCheckValidation streamHealthCheckValidation, IFixtureValidation fixtureValidation) { _actorSystem = ActorSystem.Create("AdapterSystem"); var fileStoreProvider = new FileStoreProvider(settings.StateProviderPath); CreateFixtureStateActor(settings, fileStoreProvider); CreateStreamListenerManagerActor(settings, adapterPlugin, stateManager, suspensionManager, streamHealthCheckValidation, fixtureValidation); CreateSportProcessorRouterActor(settings, udApiService); CreateSportsProcessorActor(settings, udApiService); // Setup an actor that will handle deadletter type messages var deadletterWatchMonitorProps = Props.Create(() => new AdapterDeadletterMonitorActor()); var deadletterWatchActorRef = _actorSystem.ActorOf(deadletterWatchMonitorProps, "AdapterDeadletterMonitorActor"); // subscribe to the event stream for messages of type "DeadLetter" _actorSystem.EventStream.Subscribe(deadletterWatchActorRef, typeof(DeadLetter)); }
public void It_throws_InvalidOperationException_when_row_doesnt_have_2_columns() { var lines = new[] {"8.5 FS::C:\\blah"}; File.WriteAllLines(path, lines); var provider = new FileStoreProvider(path); Assert.Throws<InvalidOperationException>(() => provider.Revive()); }
public void It_throws_InvalidOperationException_when_row_doesnt_have_2_columns() { var lines = new[] { "8.5 FS::C:\\blah" }; File.WriteAllLines(path, lines); var provider = new FileStoreProvider(path); Assert.Throws <InvalidOperationException>(() => provider.Revive()); }
public void It_can_Revive_a_single_record_of_correctly_formatted_text() { var lines = new[] {"8.5\tFS::C:\\blah"}; File.WriteAllLines(path, lines); var provider = new FileStoreProvider(path); var db = provider.Revive(); db.Records.Count().ShouldEqual(1); db.Records.First().Weight.ShouldEqual(8.5M); }
public void It_can_Revive_a_record_with_invalid_weight() { var lines = new[] {"INVALID_WEIGHT\tFS::C:\\blah"}; File.WriteAllLines(path, lines); var provider = new FileStoreProvider(path); var db = provider.Revive(); db.Records.Count().ShouldEqual(1); db.Records.First().Weight.ShouldEqual(0M); db.Records.First().Path.ShouldEqual("C:\\blah"); }
public void It_can_Revive_a_single_record_of_correctly_formatted_text() { var lines = new[] { "8.5\tFS::C:\\blah" }; File.WriteAllLines(path, lines); var provider = new FileStoreProvider(path); var db = provider.Revive(); db.Records.Count().ShouldEqual(1); db.Records.First().Weight.ShouldEqual(8.5M); }
public void It_can_Revive_a_record_with_invalid_weight() { var lines = new[] { "INVALID_WEIGHT\tFS::C:\\blah" }; File.WriteAllLines(path, lines); var provider = new FileStoreProvider(path); var db = provider.Revive(); db.Records.Count().ShouldEqual(1); db.Records.First().Weight.ShouldEqual(0M); db.Records.First().Path.ShouldEqual("C:\\blah"); }
public void It_can_save_a_single_record() { var db = new Database(); db.Add(new Record("FS::C:\\data", 42M)); var provider = new FileStoreProvider(path); provider.Save(db); var contents = File.ReadAllText(path); contents.ShouldEqual("42\tFS::C:\\data\r\n"); }
/// <summary> /// Download a content patch file from a file store. Currently, Azure /// file stores are assumed. Both compressed and uncompressed versions /// of the file are tried in respective order. /// </summary> /// <param name="FileStorePath">Supplies the remote file name that /// designates the file to download.</param> /// <param name="LocalFileName">Supplies the local file name to /// download to.</param> /// <param name="ConnectionString">Supplies the file store connection /// string.</param> /// <param name="Script">Supplies the script object.</param> private static void DownloadContentPatchFromFileStore(string FileStorePath, string LocalFileName, string ConnectionString, ACR_ServerCommunicator Script) { if (String.IsNullOrEmpty(ConnectionString)) { throw new NotSupportedException(); } // // Initialize the file store provider. // FileStore UpdaterStore = FileStoreProvider.CreateAzureFileStore(ConnectionString); FileStoreContainer UpdaterContainer = UpdaterStore.GetContainerReference(FileStoreNamespace.ACRUpdater); FileStoreFile UpdaterFile = UpdaterContainer.GetFileReference(FileStorePath + ".gzip"); // // First attempt to retrieve a gzip compressed version of the file // to patch. If that fails then fall back to a plaintext version. // try { using (MemoryStream MemStream = new MemoryStream()) { UpdaterFile.Read(MemStream); MemStream.Position = 0; using (FileStream OutStream = File.Create(LocalFileName)) { using (GZipStream CompressedStream = new GZipStream(MemStream, CompressionMode.Decompress)) { CompressedStream.CopyTo(OutStream); } } } } catch (Exception e) { Script.WriteTimestampedLogEntry(String.Format("ModuleContentPatcher.DownloadContentPatchFromFileStore: Couldn't retrieve compressed file {0} from Azure, trying uncompressed file, due to exception: {1}", FileStorePath, e)); UpdaterFile = UpdaterContainer.GetFileReference(FileStorePath); using (FileStream OutStream = File.Create(LocalFileName)) { UpdaterFile.Read(OutStream); } } }
public void It_skips_blank_and_empty_lines() { var lines = new[] { "", "8.5\tFS::C:\\blah", " ", }; File.WriteAllLines(path, lines); var provider = new FileStoreProvider(path); var db = provider.Revive(); db.Records.Count().ShouldEqual(1); }
public void It_can_Revive_multiple_records_of_correctly_formatted_text() { var lines = new[] { "8.5\tFS::C:\\blah", "8.5\tFS::C:\\blah", "8.5\tFS::C:\\blah", }; File.WriteAllLines(path, lines); var provider = new FileStoreProvider(path); var db = provider.Revive(); db.Records.Count().ShouldEqual(3); }
private static void CreateFixtureStateActor(ISettings settings, FileStoreProvider fileStoreProvider) { try { _fixtureStateActor = ActorSystem.ActorOf( Props.Create(() => new FixtureStateActor( settings, fileStoreProvider) ), FixtureStateActor.ActorName); } catch (Exception e) { _logger.Fatal($"Error creating FixtureStateActor {e}"); throw; } }
/// <summary> /// /// </summary> /// <param name="settings"></param> /// <param name="udApiService"></param> /// <param name="adapterPlugin"></param> /// <param name="stateManager"></param> /// <param name="suspensionManager"></param> /// <param name="streamHealthCheckValidation"></param> /// <param name="fixtureValidation"></param> public static void Init( ISettings settings, IServiceFacade udApiService, IAdapterPlugin adapterPlugin, IStateManager stateManager, ISuspensionManager suspensionManager, IStreamHealthCheckValidation streamHealthCheckValidation, IFixtureValidation fixtureValidation) { _actorSystem = ActorSystem.Create("AdapterSystem"); var fileStoreProvider = new FileStoreProvider(settings.StateProviderPath); _fixtureStateActor = ActorSystem.ActorOf( Props.Create(() => new FixtureStateActor( settings, fileStoreProvider)), FixtureStateActor.ActorName); _streamListenerManagerActor = ActorSystem.ActorOf( Props.Create(() => new StreamListenerManagerActor( settings, adapterPlugin, stateManager, suspensionManager, streamHealthCheckValidation, fixtureValidation)), StreamListenerManagerActor.ActorName); _sportProcessorRouterActor = ActorSystem.ActorOf( Props.Create(() => new SportProcessorRouterActor(udApiService)) .WithRouter(new SmallestMailboxPool(settings.FixtureCreationConcurrency)), SportProcessorRouterActor.ActorName); _sportsProcessorActor = ActorSystem.ActorOf( Props.Create(() => new SportsProcessorActor( settings, udApiService, _sportProcessorRouterActor)), SportsProcessorActor.ActorName); }
/// <summary> /// Check if a file store file exists. /// </summary> /// <param name="FileStorePath">Supplies the remote file name that /// designates the file to check for existance.</param> /// <param name="ConnectionString">Supplies the file store connection /// string.</param> /// <returns>True if the file store file exists.</returns> private static bool ContentPatchFileStoreFileExists(string FileStorePath, string ConnectionString) { if (String.IsNullOrEmpty(ConnectionString)) { return(false); } // // Initialize the file store provider. // FileStore UpdaterStore = FileStoreProvider.CreateAzureFileStore(ConnectionString); FileStoreContainer UpdaterContainer = UpdaterStore.GetContainerReference(FileStoreNamespace.ACRUpdater); FileStoreFile UpdaterFile = UpdaterContainer.GetFileReference(FileStorePath + ".gzip"); try { if (UpdaterFile.Exists()) { return(true); } } catch { } UpdaterFile = UpdaterContainer.GetFileReference(FileStorePath); try { if (UpdaterFile.Exists()) { return(true); } } catch { } return(false); }
/// <summary> /// This thread function measures vault latency every /// VAULT_MEASUREMENT_INTERVAL milliseconds. /// /// Note that, because this function does not execute from a script /// context, it cannot call script functions. Instead, a companion /// DelayCommand continuation on the main server thread will check the /// current latency value and save it as appropriate. /// </summary> private static void VaultPingThreadRoutine() { string VaultPingFile = String.Format( "{0}Server{1}.txt", SystemInfo.GetCentralVaultPath(), LocalServerId); for (; ;) { // // Open the ping file on the vault. If we fail to open it then // something has gone wrong with the vault connection. // try { string ConnectionString = FileStoreProvider.DefaultVaultConnectionString; uint Tick = (uint)Environment.TickCount; using (StreamWriter PingFile = File.CreateText(VaultPingFile)) { PingFile.WriteLine( "Server {0} is up at {1}.", LocalServerId, DateTime.UtcNow); } if (!String.IsNullOrEmpty(ConnectionString)) { FileStore Store = FileStoreProvider.CreateAzureFileStore(ConnectionString); FileStoreContainer StoreContainer = Store.GetContainerReference(FileStoreNamespace.ServerVault); FileStoreFile StoreFile = StoreContainer.GetFileReference(String.Format("Server{0}.txt", LocalServerId)); using (MemoryStream MemStream = new MemoryStream()) { StoreFile.Write(MemStream); } } Tick = (uint)Environment.TickCount - Tick; // // Report the response time. // lock (CurrentLatencyLock) { CurrentVaultLatency = (int)Tick; } } catch (Exception e) { Logger.Log("LatencyMonitor.VaultPingThreadRoutine: Exception pinging server vault: {0}", e); // // Report a response time of -1 to indicate that no // measurement could be taken. // lock (CurrentLatencyLock) { CurrentVaultLatency = (int)-1; } } Thread.Sleep(VAULT_MEASUREMENT_INTERVAL); } }
static void Main(string[] args) { string ConnectionString = null; string DownloadTo = null; string UploadFrom = null; string DeleteFileName = null; bool IncludeAllFiles = false; Console.CancelKeyPress += Console_CancelKeyPress; Console.WriteLine("ALFA Azure Vault Management Tool v{0}", Assembly.GetExecutingAssembly().GetName().Version); Console.WriteLine(); for (int i = 0; i < args.Length; i += 1) { if (i + 1 < args.Length && args[i].ToLowerInvariant() == "-connectionstring") { ConnectionString = args[i + 1]; i += 1; continue; } else if (i + 1 < args.Length && args[i].ToLowerInvariant() == "-download") { DownloadTo = args[i + 1]; i += 1; continue; } else if (i + 1 < args.Length && args[i].ToLowerInvariant() == "-upload") { UploadFrom = args[i + 1]; i += 1; continue; } else if (i + 1 < args.Length && args[i].ToLowerInvariant() == "-delete") { DeleteFileName = args[i + 1]; i += 1; continue; } else if (args[i].ToLowerInvariant() == "-all") { IncludeAllFiles = true; continue; } Console.WriteLine("Unrecognized argument: {0}", args[i]); PrintUsage(); return; } if ((String.IsNullOrEmpty(ConnectionString)) || ((String.IsNullOrEmpty(DownloadTo) ? 0 : 1) + (String.IsNullOrEmpty(UploadFrom) ? 0 : 1) + (String.IsNullOrEmpty(DeleteFileName) ? 0 : 1) != 1)) { PrintUsage(); return; } try { FileStore Store = FileStoreProvider.CreateAzureFileStore(ConnectionString); FileStoreContainer Container = Store.GetContainerReference(FileStoreNamespace.ServerVault); if (!String.IsNullOrEmpty(DownloadTo)) { Console.WriteLine("Downloading from vault to {0} in (CTRL+C in 5 seconds to cancel)...", DownloadTo); Thread.Sleep(5000); foreach (FileStoreDirectory Directory in Container.GetDirectories()) { foreach (FileStoreFile File in Directory.GetFiles()) { string RemoteName; if (AbortProgram) { Console.WriteLine("Cancelled."); return; } // // Attempt to create the local file using the // original filename case. // File.FetchAttributes(); if (File.Metadata.TryGetValue("OriginalFileName", out RemoteName)) { if (RemoteName.ToLowerInvariant() != File.Name) { Console.WriteLine("* File {0} OriginalFileName {1} is malformed, ignoring.", File.Name, RemoteName); RemoteName = File.Name; } } else { RemoteName = File.Name; } string RemoteDirName = Path.GetDirectoryName(RemoteName); string RemoteFileName = Path.GetFileName(RemoteName); if (!IncludeAllFiles && !RemoteFileName.ToLowerInvariant().EndsWith(".bic")) { Console.WriteLine("* File {0} is not a character file, skipping...", RemoteName); continue; } if (!ALFA.SystemInfo.IsSafeFileName(RemoteFileName) || RemoteDirName.IndexOfAny(Path.GetInvalidPathChars()) != -1) { Console.WriteLine("* File {0} has an unsafe filename, skipping...", RemoteName); continue; } string FileName = String.Format("{0}{1}{2}", DownloadTo, Path.DirectorySeparatorChar, RemoteDirName); FileStream FsFile = null; DateTimeOffset?Offset = null; if (!System.IO.Directory.Exists(FileName)) { System.IO.Directory.CreateDirectory(FileName); } FileName = String.Format("{0}{1}{2}", FileName, Path.DirectorySeparatorChar, RemoteFileName); // // Download the file if it didn't exist locally, or // if the local file was older than the vault based // file. // try { try { FsFile = System.IO.File.Open(FileName, FileMode.Open, FileAccess.ReadWrite); Offset = System.IO.File.GetLastWriteTimeUtc(FileName); } catch (DirectoryNotFoundException) { } catch (FileNotFoundException) { } if (FsFile == null) { FsFile = System.IO.File.Open(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); Offset = null; } if (Offset != null) { Console.Write("Downloading file {0}...", FileName); try { try { File.ReadIfModifiedSince(FsFile, Offset.Value); } catch { FsFile.Dispose(); System.IO.File.SetLastWriteTimeUtc(FileName, Offset.Value.UtcDateTime); throw; } FsFile.Dispose(); System.IO.File.SetLastWriteTimeUtc(FileName, File.LastModified.Value.UtcDateTime); Console.WriteLine(" done."); } catch (FileStoreConditionNotMetException) { Console.WriteLine(" already up to date."); } } else { Console.Write("Downloading file {0}...", FileName); try { File.Read(FsFile); } catch { FsFile.Dispose(); System.IO.File.Delete(FileName); } FsFile.Dispose(); System.IO.File.SetLastWriteTimeUtc(FileName, File.LastModified.Value.UtcDateTime); Console.WriteLine("done."); } } finally { FsFile.Dispose(); } } } } else if (!String.IsNullOrEmpty(UploadFrom)) { Console.WriteLine("Uploading from {0} to vault (CTRL+C in 5 seconds to cancel)...", UploadFrom); Thread.Sleep(5000); Container.CreateIfNotExists(); // // Canonicalize the upload from base path so that relative // filenames can be ascertained for naming files that are // transferred to the vault. // UploadFrom = Path.GetFullPath(UploadFrom); if (UploadFrom.EndsWith(Path.DirectorySeparatorChar.ToString())) { UploadFrom = UploadFrom.Substring(0, UploadFrom.Length - 1); } foreach (string FsFileName in Directory.EnumerateFiles(UploadFrom, "*.bic", SearchOption.AllDirectories)) { string RemoteName; if (AbortProgram) { Console.WriteLine("Cancelled."); return; } if (!IncludeAllFiles && !FsFileName.ToLowerInvariant().EndsWith(".bic")) { Console.WriteLine("* File {0} is not a character file, skipping...", FsFileName); continue; } // // Upload files that did not exist or which were newer // on the local side. // RemoteName = FsFileName.Substring(UploadFrom.Length + 1); DateTimeOffset FsLastModified = System.IO.File.GetLastWriteTimeUtc(FsFileName); using (FileStream FsFile = System.IO.File.OpenRead(FsFileName)) { Console.Write("Uploading file {0}...", RemoteName); try { FileStoreFile RemoteFile = Container.GetFileReference(RemoteName.Replace('\\', '/').ToLowerInvariant()); try { RemoteFile.FetchAttributes(); if (RemoteFile.LastModified >= FsLastModified) { Console.WriteLine(" already up to date."); continue; } } catch { } RemoteFile.Metadata["OriginalFileName"] = RemoteName.Replace('\\', '/'); RemoteFile.WriteIfNotModifiedSince(FsFile, FsLastModified); Console.WriteLine(" done."); } catch (FileStoreConditionNotMetException) { Console.WriteLine(" already up to date."); } } } } else if (!String.IsNullOrEmpty(DeleteFileName)) { Console.Write("Deleting file {0} from vault (CTRL+C in 5 seconds to cancel)...", DeleteFileName); Thread.Sleep(5000); if (AbortProgram) { Console.WriteLine("Cancelled."); return; } // // Delete vault file if it existed. // FileStoreFile RemoteFile = Container.GetFileReference(DeleteFileName.Replace('\\', '/').ToLowerInvariant()); if (!RemoteFile.Exists()) { Console.Write(" file not found on the vault."); } else { RemoteFile.Delete(); Console.Write(" done"); } } } catch (Exception e) { Console.WriteLine("Exception: {0}", e); return; } }
public void It_can_save_a_single_record_with_weight_as_decimal_value() { var db = new Database(); db.Add(new Record("FS::C:\\data", 42.5M)); var provider = new FileStoreProvider(path); provider.Save(db); var contents = File.ReadAllText(path); contents.ShouldEqual("42.5\tFS::C:\\data\r\n"); }