public void StartCopyOperations(int timesToRun) { if (this.passedValidation) { // setup the source and destinations for the copy threads // each source gets a thread pool foreach (SourceType source in this.sourceFolders) { VRASThreadManager t = new VRASThreadManager(new VRASCopyConfiguration(this.globalsettings, this.batchSettings, source, this.destFolders), timesToRun); this.copyThreadPools.Add(t); } // start the threads foreach (VRASThreadManager t in this.copyThreadPools) { t.StartThreads(); } } else { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Cannot start batch: " + this.batchSettings.BatchName + " it did not pass vailidation", System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.SetupError), VRASLogEvent.EventSourceDefault); } }
public void StartBatches() { try { foreach (Batch b in this.batches) { if (this.isService) { b.StartCopyOperations(-1); } else { b.StartCopyOperations(1); } } } catch (Exception ex) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Error: " + ex.Message, System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.FileCopyError), VRASLogEvent.EventSourceDefault); } }
/// <summary> /// OnStop(): Put your stop code here /// </summary> protected override void OnStop() { try { this.controler.StopAll(); // wait for the service timeout as defined then kill if necessary int timeSlept = 0; while (!this.controler.DoneYet() || timeSlept > this.controler.GetServiceTimeOutMs()) { timeSlept += 1000; Thread.Sleep(1000); } this.controler.Cleanup(); } catch (Exception ex) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Error: " + ex.Message, System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.ErrorID), VRASLogEvent.EventSourceDefault); } finally { base.OnStop(); } }
public bool ValidateNTFSPermissionsOnDestinations() { try { foreach (DestinationType d in this.destFolders) { if (!Directory.Exists(d.DestinationLocation)) { Directory.CreateDirectory(d.DestinationLocation); } this.hasRightsToDestFolders = this.MakeSureHaveWriteAccessToFolder(d.DestinationLocation); if (!this.hasRightsToDestFolders) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Error Accessing Destination: " + d.DestinationLocation, System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.SetupError), VRASLogEvent.EventSourceDefault); this.passedValidation = false; return(false); } } this.passedValidation = true; return(true); } catch { return(false); } }
public bool ValidateNTFSPermissionsOnSources() { try { foreach (SourceType s in this.sourceFolders) { this.hasRightsToSourceFolders = this.MakeSureHaveWriteAccessToFolder(s.SourceFolderPath); if (!this.hasRightsToSourceFolders) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Error VRAS doesn not have access to Source: " + s.SourceFolderPath, System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.SetupError), VRASLogEvent.EventSourceDefault); return(false); } } return(true); } catch { return(false); } }
/// <summary> /// OnStart(): Put startup code here /// </summary> /// <param name="args"></param> protected override void OnStart(string[] args) { try { VRASLogEvent.SetIsService(true); VRASLogEvent.EventSourceDefault = "VRAS Service"; VRASLogEvent.PrimeEventLogSource(VRASLogEvent.EventSourceDefault, VRASLogEvent.EventLogName); Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); // true for service this.loader = new VRASLoader(true); if (!File.Exists(VRAS.VRASLogEvent.ConfigFile)) { string txt = "Could not find the configuration file: " + VRAS.VRASLogEvent.ConfigFile; VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, txt, System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.SetupError), VRASLogEvent.EventSourceDefault); Environment.Exit(1); } // if the config is valid do some work if (this.loader.LoadConfiguration(VRASLogEvent.ConfigFile)) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Configuration Loaded starting Initialization.", System.Diagnostics.EventLogEntryType.Information, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.Starting), VRASLogEvent.EventSourceDefault); this.controler = this.loader.InitializeConfiguration(); if (this.controler == null) { throw new Exception("Error initializing the configuration"); } this.controler.ValidateBatches(); this.controler.StartBatches(); } } catch (Exception ex) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Error: " + ex.Message, System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.ErrorID), VRASLogEvent.EventSourceDefault); } }
// each source file might go to N destinations public void PerformCopy() { if (this.filesToCopy.Count > 0) { foreach (VRASCopyOperationHelper helper in this.filesToCopy) { if (!VRASLogEvent.IsService) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, String.Format("Processing File in Batch: {0}\n\tSource: {1}\n\tDest: {2}", this.batchSetName, helper.SourceFullString, helper.DestInfo.DestFullString), System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.ErrorID), VRASLogEvent.EventSourceDefault); } try { if (!this.CopyFile(helper)) { this.copyErrorOccured = true; throw new Exception(String.Format("Error occured in the copy file method.\n\tSource:{0}\n\tDestination:{1}", helper.SourceFullString, helper.DestInfo.DestFullString)); } } catch (Exception ex) { // Failed to copy the file to all destinations this.copyErrorOccured = true; VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, String.Format("Error Copying File: Batch:{0}\n\tError:{1}\n\tSource:{2}\n\tDest:{3}", ex.Message, this.batchSetName, helper.SourceFullString, helper.DestInfo.DestFullString), System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.FileCopyError), VRASLogEvent.EventSourceDefault); } } try { // See if we need to delete this file if (this.moveFiles && !this.copyErrorOccured) { // attempt to remove the first source File.Delete(this.filesToCopy[0].SourceFullString); } } catch (Exception ex) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, String.Format("Error Deleting Source File After the File was copied to each Destination. This file will be copied again the next batch run.\n\tBatch: {0}\n\tSource: {1}.\n\tError: {2}", this.batchSetName, this.filesToCopy[0].SourceFullString, ex.Message), System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.ErrorID), VRASLogEvent.EventSourceDefault); } } }
public static void DocommandLineWork(string configFile) { try { VRASLogEvent.SetIsService(false); // to test as a service would run run this line // VRASLoader loader = new VRASLoader(true); VRASLoader loader = new VRASLoader(false); VRASController controler; // if the config is valid do some work Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); if (loader.LoadConfiguration(configFile)) { controler = loader.InitializeConfiguration(); controler.ValidateBatches(); controler.StartBatches(); // wait for the batch to get done while (!controler.DoneYet()) { Thread.Sleep(5000); } controler.Cleanup(); } else { throw new Exception("Error loading config file"); } } catch (Exception ex) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, String.Format("Error: {0}", ex.Message), System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.FileCopyError), VRASLogEvent.EventSourceDefault); } }
public bool LoadConfiguration(string pathToConfig) { try { StreamReader str = new StreamReader(pathToConfig); XmlSerializer serializer = new XmlSerializer(typeof(VRASConfigurationType)); return((this.vrasXsdConfig = (VRASConfigurationType)serializer.Deserialize(str)) != null); } catch (Exception ex) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Error: " + ex.Message, System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.ErrorID), VRASLogEvent.EventSourceDefault); return(false); } }
// method the copy threads use to copy each file private void RunWorker() { try { while (!this.timeToQuit) { WaitQueueItem item = null; lock (this.queue) { if (this.queue.Count > 0) { item = this.queue.Dequeue(); } } // only run if valid and else if means running multple times from a command line >1 or it is set to service (-1) if (item != null) { this.workingCopyThreads++; ExecutionContext.Run(item.Context, new ContextCallback(item.Callback), item.State); this.workingCopyThreads--; } else if (this.timesToRunTheWorkFinder > 1 || this.timesToRunTheWorkFinder == -1) { this.workWaiting.WaitOne(this.copyConfig.SleepBetweenBatchesMs); } } } catch (Exception ex) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, String.Format("Error with copy thread: {0}", ex.Message), System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.FileCopyError), VRASLogEvent.EventSourceDefault); } }
// method the thread uses to find new files to copy private void RunJobFinder() { int timesRun = 0; while (!this.timeToQuit) { lock (this.queue) { if (this.queue.Count <= 0 && this.workingCopyThreads == 0) { try { List <VRASCopyBlob> blobList = VRASCopyBlob.GetFiles(this.copyConfig); foreach (VRASCopyBlob copyJob in blobList) { // need to make the workitems for the blob to know what to copy copyJob.PopulateCopyWork(); this.QueueUserWorkItem(new WaitCallback(copyJob.DoWork), new object()); } } catch (Exception ex) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Error while getting files: " + ex.Message + "...Worker Thread to find files is going to sleep for its scheduled duration.", System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.FileCopyError), VRASLogEvent.EventSourceDefault); } } } // if it is a service increase the run count if (this.timesToRunTheWorkFinder != -1) { timesRun++; } else { // sleep if it is a service (-1) times to run this.workWaiting.WaitOne(this.copyConfig.SleepBetweenBatchesMs); } // don't sleep if there are no more runs to do if ((this.timesToRunTheWorkFinder - timesRun) > 0) { this.workWaiting.WaitOne(this.copyConfig.SleepBetweenBatchesMs); } // see if it ran enough times -1 means forever. Then we can set the flag to quit to true. If we don't wait then the copy threads will all want to quit now. if (this.timesToRunTheWorkFinder != -1 && timesRun >= this.timesToRunTheWorkFinder) { // wait for the queue to drain bool stillWaiting = true; int count; while (stillWaiting) { lock (this.queue) { count = this.queue.Count; } if (count > 0) { Thread.Sleep(1000); } else { stillWaiting = false; } } this.timeToQuit = true; } } }
private void Init() { try { int cur = 0; string firstDestFileFullPath = String.Empty; foreach (DestinationType d in this.destinationInfo) { // need to keep track on the instance count cur++; // now populate the copy operation VRASCopyOperationHelper helper = new VRASCopyOperationHelper(); // figure out the source if (this.copyFromFirstDestinationFolder & cur > 1) { // was made the first run helper.SourceFullString = firstDestFileFullPath; } else { helper.SourceFullString = this.sourceFile.FullName; } // figure out the destination helper.DestInfo = this.DestinationFileGenerator(d.DestinationLocation, this.sourceFile.DirectoryName, this.sourceFile.FullName, this.sourceRootFolder, this.sourceFile.Name, d.ArchiveSettings, this.batchSetName, this.defaultRegExString); // assign this incase this is to be the new source for the copy. For the next run through the destination lists if (cur == 1) { firstDestFileFullPath = helper.DestInfo.DestFullString; } // need to hash compare? helper.HashCompare = d.HashCompareFlag; this.filesToCopy.Add(helper); // Delete all in process file in destination folder foreach (string file in Directory.GetFiles(d.DestinationLocation, "*" + this.tempFileExt)) { try { File.Delete(file); } catch { // Swallow exceptions if we cannot delete it. } } } } catch (Exception ex) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Error: " + ex.Message, System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.FileCopyError), VRASLogEvent.EventSourceDefault); } }