예제 #1
0
        public static LongRunningTaskStatusInfo WaitForOrderToComplete(IConciergeAPIService api, LongRunningTaskInfo info)
        {
            if (api == null)
            {
                throw new ArgumentNullException("api");
            }
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }


            // code for synchronous orders
            var statusUpdate = api.CheckLongRunningTaskStatus(info).ResultValue;

            if (statusUpdate.Status != LongRunningTaskStatus.Running)
            {
                return(statusUpdate);
            }


            var dtStart = DateTime.Now;

            var strWait = ConfigurationManager.AppSettings["OrderProcessingTimeOut"];

            int numberOfSecondsToWait;

            if (!int.TryParse(strWait, out numberOfSecondsToWait))
            {
                numberOfSecondsToWait = 45;
            }

            var waitTime = 5500; // we're going to back off of this


            do
            {
                Thread.Sleep(waitTime); // wait a second, in case it's ready right away

                statusUpdate = api.CheckLongRunningTaskStatus(info).ResultValue;

                if (statusUpdate.Status != LongRunningTaskStatus.Running)
                {
                    return(statusUpdate);
                }


                waitTime += 1000; // so it will be 1.5, 2.5, 3.5, 4.5, a linear back-off
            } while ((DateTime.Now - dtStart).TotalSeconds < numberOfSecondsToWait);


            return(statusUpdate); // nothing*/
        }
        public async Task <object> Post(AnalyzeDiskDriveRequest request)
        {
            Log.Debug("starting Post(AnalyzeDiskDriveRequest)");
            // Housekeeping setup for the task to be created
            // Create new Id for this LongRunningTask
            Id <LongRunningTaskInfo> longRunningTaskID = new Id <LongRunningTaskInfo>(Guid.NewGuid());
            var cancellationTokenSource   = new CancellationTokenSource();
            var cancellationTokenSourceId = new Id <CancellationTokenSource>(Guid.NewGuid());
            var cancellationToken         = cancellationTokenSource.Token;

            // Setup the instance
            var diskAnalysis       = new DiskDriveAnalysis(Log);
            var diskDriveSpecifier = request.AnalyzeDiskDriveRequestPayload.DiskDriveSpecifier;

            //ToDo: Create an association between the remote cancellation token and a new cancellation token, and record this in the RemoteToLocalCancellationToken dictionary in the Container

            // Setup the long-running task that will read one or more disk drives and CRUD a collection of DiskDriveInfoEx records, and optionally CRUD a DB Representation of that collection

            // Make a DB Representation of one or more disk drives and the partitions on those disk drives
            // Create multiple lambdas that will update SQLServerDB
            // The CRUD value comes in the request
            // There is a collection of DiskDriveInfoEx records in the ComputerInventory.ComputerHardware property of the BaseServicesData instance, which can be accessed through the DiskAnalysisServicesData.BaseServicesData property of the DiskAnalysisServicesData instance.

            // Reading the actual Disk Drives connected to a named computer (localhost or a remote computer) can create a collection of DiskDriveInfoEx records

            // Reading a file formated to follow the conventions of the Configuration data for ComputerHardware can create a collection of DiskDriveInfoEx records
            // ToDo: figure out library Logging so that we don't need to pass a Log instance to the library

            //ToDo: Better switch on exactly what to Walk, - local machine, remote machine, configuration data, hypothetical data.
            // if the request's data is null, analyze all physical disks, else analyze the list of physical disks sent with the request

            // Get the BaseServicesData and diskAnalysisServicesData instances that were injected into the DI container
            var baseServicesData         = HostContext.TryResolve <BaseServicesData>();
            var diskAnalysisServicesData = HostContext.TryResolve <DiskAnalysisServicesData>();

            // Create storage for the results and progress
            var analyzeDiskDriveResult = new AnalyzeDiskDriveResult();

            diskAnalysisServicesData.AnalyzeDiskDriveResultsCOD.Add(longRunningTaskID, analyzeDiskDriveResult);
            var analyzeDiskDriveProgress = new AnalyzeDiskDriveProgress();

            diskAnalysisServicesData.AnalyzeDiskDriveProgressCOD.Add(longRunningTaskID, analyzeDiskDriveProgress);

            // Define the lambda that describes the task
            var task = new Task(() => {
                diskAnalysis.AnalyzeDiskDrive(
                    request.AnalyzeDiskDriveRequestPayload.DiskDriveSpecifier,
                    analyzeDiskDriveResult, analyzeDiskDriveProgress,
                    cancellationToken,
                    (crud, r) => {
                    Log.Debug($"starting recordRoot Lambda, r = {r}");
                }
                    ).ConfigureAwait(false);
            });

            LongRunningTaskInfo longRunningTaskInfo = new LongRunningTaskInfo(longRunningTaskID, task, cancellationTokenSource);

            // baseServicesData.CancellationTokenSources.Add(cancellationTokenSourceId, cancellationTokenSource);
            // Record this task (plus additional information about it) in the longRunningTasks dictionary in the BaseServicesData found in the Container
            baseServicesData.LongRunningTasks.Add(longRunningTaskID, longRunningTaskInfo);
            // record the TaskID and task info into the LookupDiskDriveAnalysisResultsCOD
            diskAnalysisServicesData.LookupDiskDriveAnalysisResultsCOD.Add(longRunningTaskID, longRunningTaskInfo);

            // ToDo: Setup the SSE receiver that will monitor the long-running task
            // ToDo: return to the caller the callback URL and the longRunningTaskID to allow the caller to connect to the SSE that monitors the task and the data structures it updates
            // Start the task running
            try {
                baseServicesData.LongRunningTasks[longRunningTaskID].LRTask.Start();
            }
            catch (Exception e) when(e is InvalidOperationException || e is ObjectDisposedException)
            {
                Log.Debug($"Exception when trying to start the AnalyzeDiskDrive task, message is {e.Message}");
                // ToDo: need to be sure that the when.any loop and GetLongRunningTaskStatus can handle a LongRunningTaskTaskInfo in these states;
            }
            var longRunningTaskIds = new List <Id <LongRunningTaskInfo> >();

            longRunningTaskIds.Add(longRunningTaskID);
            var analyzeDiskDriveResponsePayload = new AnalyzeDiskDriveResponsePayload(longRunningTaskIds);
            var analyzeDiskDriveResponse        = new AnalyzeDiskDriveResponse(analyzeDiskDriveResponsePayload);

            Log.Debug($"Leaving Post(AnalyzeDiskDriveRequest), analyzeDiskDriveResponse = {analyzeDiskDriveResponse}");
            return(analyzeDiskDriveResponse);
        }
예제 #3
0
        public async Task <object> Post(AnalyzeFileSystemRequest request)
        {
            Log.Debug("starting Post(AnalyzeFileSystemRequest)");
            // Housekeeping setup for the task to be created
            // Create new Id for this LongRunningTask
            Id <LongRunningTaskInfo> longRunningTaskID = new Id <LongRunningTaskInfo>(Guid.NewGuid());
            var cancellationTokenSource   = new CancellationTokenSource();
            var cancellationTokenSourceId = new Id <CancellationTokenSource>(Guid.NewGuid());
            var cancellationToken         = cancellationTokenSource.Token;

            Log.Debug("in Post(AnalyzeFileSystemRequest) 1");
            // Get the BaseServicesData and diskAnalysisServicesData instances that were injected into the DI container
            var baseServicesData         = HostContext.TryResolve <BaseServicesData>();
            var diskAnalysisServicesData = HostContext.TryResolve <DiskAnalysisServicesData>();

            // Setup the instance. Use Configuration Data if the request payload is null
            var blockSize          = request.AnalyzeFileSystemRequestPayload.AsyncFileReadBlockSize >= 0? request.AnalyzeFileSystemRequestPayload.AsyncFileReadBlockSize : diskAnalysisServicesData.ConfigurationData.BlockSize;
            var fileSystemAnalysis = new FileSystemAnalysis(Log, diskAnalysisServicesData.ConfigurationData.BlockSize);

            Log.Debug("in Post(AnalyzeFileSystemRequest) 2");


            // Create storage for the results and progress
            var analyzeFileSystemResult = new AnalyzeFileSystemResult();

            diskAnalysisServicesData.AnalyzeFileSystemResultsCOD.Add(longRunningTaskID, analyzeFileSystemResult);
            var analyzeFileSystemProgress = new AnalyzeFileSystemProgress();

            diskAnalysisServicesData.AnalyzeFileSystemProgressCOD.Add(longRunningTaskID, analyzeFileSystemProgress);

            // Define the lambda that describes the task
            var task = new Task(() => {
                fileSystemAnalysis.AnalyzeFileSystem(
                    request.AnalyzeFileSystemRequestPayload.Root,
                    analyzeFileSystemResult, analyzeFileSystemProgress,
                    cancellationToken,
                    (crud, r) => {
                    Log.Debug($"starting recordRoot Lambda, r = {r}");
                }
                    ).ConfigureAwait(false);
            });

            LongRunningTaskInfo longRunningTaskInfo = new LongRunningTaskInfo(longRunningTaskID, task, cancellationTokenSource);

            // Record this task (plus additional information about it) in the longRunningTasks dictionary in the BaseServicesData found in the Container
            baseServicesData.LongRunningTasks.Add(longRunningTaskID, longRunningTaskInfo);
            // record the TaskID and task info into the LookupDiskDriveAnalysisResultsCOD
            diskAnalysisServicesData.LookupFileSystemAnalysisResultsCOD.Add(longRunningTaskID, longRunningTaskInfo);

            // ToDo: Setup the SSE receiver that will monitor the long-running task
            // ToDo: return to the caller the callback URL and the longRunningTaskID to allow the caller to connect to the SSE that monitors the task and the data structures it updates
            // Start the task running
            try {
                baseServicesData.LongRunningTasks[longRunningTaskID].LRTask.Start();
            }
            catch (Exception e) when(e is InvalidOperationException || e is ObjectDisposedException)
            {
                Log.Debug($"Exception when trying to start the AnalyzeDiskDrive task, message is {e.Message}");
                // ToDo: need to be sure that the when.any loop and GetLongRunningTasksStatus can handle a taskinfo in these states;
            }
            var analyzeFileSystemResponsePayload = new AnalyzeFileSystemResponsePayload(new List <Id <LongRunningTaskInfo> >()
            {
                longRunningTaskID
            });
            var analyzeFileSystemResponse = new AnalyzeFileSystemResponse(analyzeFileSystemResponsePayload);

            Log.Debug($"Leaving Post(AnalyzeFileSystemRequest), analyzeFileSystemResponse = {analyzeFileSystemResponse}");
            return(analyzeFileSystemResponse);
        }