Esempio n. 1
0
        /// <summary>
        /// Commits all pending changes to this <see cref="CloudTask" /> to the Azure Batch service.
        /// </summary>
        /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are applied to the Batch service request after the <see cref="CustomBehaviors"/>.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</returns>
        /// <remarks>The commit operation runs asynchronously.</remarks>
        public async System.Threading.Tasks.Task CommitAsync(IEnumerable <BatchClientBehavior> additionalBehaviors = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            this.propertyContainer.IsReadOnly = true;

            // craft the behavior manager for this call
            BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors);

            if (BindingState.Unbound == this.propertyContainer.BindingState)
            {
                //TODO: Implement task submission via .Commit here
                UtilitiesInternal.ThrowOnUnbound(this.propertyContainer.BindingState);
            }
            else
            {
                Models.TaskConstraints protoTaskConstraints = UtilitiesInternal.CreateObjectWithNullCheck(this.Constraints, o => o.GetTransportObject());

                System.Threading.Tasks.Task <AzureOperationHeaderResponse <Models.TaskUpdateHeaders> > asyncTaskUpdate =
                    this.parentBatchClient.ProtocolLayer.UpdateTask(
                        this.parentJobId,
                        this.Id,
                        protoTaskConstraints,
                        bhMgr,
                        cancellationToken);

                await asyncTaskUpdate.ConfigureAwait(continueOnCapturedContext : false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Commits all pending changes to this <see cref="CloudJobSchedule" /> to the Azure Batch service.
        /// </summary>
        /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are applied to the Batch service request after the <see cref="CustomBehaviors"/>.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the asynchronous operation.</returns>
        /// <remarks>
        /// <para>
        /// Updates an existing <see cref="CloudJobSchedule"/> on the Batch service by replacing its properties with the properties of this <see cref="CloudJobSchedule"/> which have been changed.
        /// Unchanged properties are ignored.
        /// All changes since the last time this entity was retrieved from the Batch service (either via <see cref="Refresh"/>, <see cref="JobScheduleOperations.GetJobSchedule"/>,
        /// or <see cref="JobScheduleOperations.ListJobSchedules"/>) are applied.
        /// Properties which are explicitly set to null will cause an exception because the Batch service does not support partial updates which set a property to null.
        /// If you need to set a property to null, use <see cref="Commit"/>.
        /// </para>
        /// <para>This operation runs asynchronously.</para>
        /// </remarks>
        public async System.Threading.Tasks.Task CommitChangesAsync(
            IEnumerable <BatchClientBehavior> additionalBehaviors = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            UtilitiesInternal.ThrowOnUnbound(this.propertyContainer.BindingState);

            // after this no prop access is allowed
            this.propertyContainer.IsReadOnly = true;

            // craft the bahavior manager for this call
            BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors);

            Models.JobSpecification jobSpecification = this.propertyContainer.JobSpecificationProperty.GetTransportObjectIfChanged <JobSpecification, Models.JobSpecification>();
            Models.Schedule         schedule         = this.propertyContainer.ScheduleProperty.GetTransportObjectIfChanged <Schedule, Models.Schedule>();
            Models.MetadataItem[]   metadata         = this.propertyContainer.MetadataProperty.GetTransportObjectIfChanged <MetadataItem, Models.MetadataItem>();

            System.Threading.Tasks.Task asyncJobScheduleUpdate =
                this.parentBatchClient.ProtocolLayer.PatchJobSchedule(
                    this.Id,
                    jobSpecification,
                    metadata,
                    schedule,
                    bhMgr,
                    cancellationToken);

            await asyncJobScheduleUpdate.ConfigureAwait(continueOnCapturedContext : false);
        }
Esempio n. 3
0
        /// <summary>
        /// See <see cref="IFileStagingProvider.StageFilesAsync"/>.
        /// </summary>
        /// <param name="filesToStage">The instances of IFileStagingProvider to stage.</param>
        /// <param name="fileStagingArtifact">IFileStagingProvider specific staging artifacts including error/progress.</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</returns>
        public async System.Threading.Tasks.Task StageFilesAsync(List <IFileStagingProvider> filesToStage, IFileStagingArtifact fileStagingArtifact)
        {
            System.Threading.Tasks.Task taskForStaticStaging = FileToStage.StageFilesInternalAsync(filesToStage, fileStagingArtifact);

            await taskForStaticStaging.ConfigureAwait(continueOnCapturedContext : false);

            return;
        }
Esempio n. 4
0
        /// <summary>
        /// Extension to fire and forget an asynchronous task
        /// </summary>
        /// <param name="task">The Task-returning async method being started</param>
        /// <param name="continueOnCapturedContext">Optional boolean for ConfigureAwait</param>
        /// <param name="onException">Optional delegate for exception handling</param>
#pragma warning disable RECS0165
        public static async void SafeFireAndForget(this System.Threading.Tasks.Task task, bool continueOnCapturedContext = true, Action <Exception> onException = null)
#pragma warning restore RECS0165
        {
            try
            {
                await task.ConfigureAwait(continueOnCapturedContext);
            }
            catch (Exception ex) when(onException != null)
            {
                onException(ex);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Deletes this <see cref="CloudJobSchedule" />.
        /// </summary>
        /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are applied to the Batch service request after the <see cref="CustomBehaviors"/>.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the asynchronous operation.</returns>
        /// <remarks>
        /// <para>The delete operation requests that the job schedule be deleted.  The request puts the schedule in the <see cref="Common.JobScheduleState.Deleting"/> state.
        /// The Batch service will delete any existing jobs and tasks under the schedule, including any active job, and perform the actual job schedule deletion without any further client action.</para>
        /// <para>The delete operation runs asynchronously.</para>
        /// </remarks>
        public async System.Threading.Tasks.Task DeleteAsync(IEnumerable <BatchClientBehavior> additionalBehaviors = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            // throw if if this object is unbound
            UtilitiesInternal.ThrowOnUnbound(this.propertyContainer.BindingState);

            // craft the behavior manager for this call
            BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors);

            System.Threading.Tasks.Task asyncTask = this.parentBatchClient.ProtocolLayer.DeleteJobSchedule(this.Id, bhMgr, cancellationToken);

            await asyncTask.ConfigureAwait(continueOnCapturedContext : false);
        }
Esempio n. 6
0
        /// <summary>
        /// Safely execute the Task without waiting for it to complete before moving to the next line of code; commonly known as "Fire And Forget". Inspired by John Thiriet's blog post, "Removing Async Void": https://johnthiriet.com/removing-async-void/.
        /// </summary>
        /// <param name="task">Task.</param>
        /// <param name="continueOnCapturedContext">If set to <c>true</c> continue on captured context; this will ensure that the Synchronization Context returns to the calling thread. If set to <c>false</c> continue on a different context; this will allow the Synchronization Context to continue on a different thread</param>
        /// <param name="onException">If an exception is thrown in the Task, <c>onException</c> will execute. If onException is null, the exception will be re-thrown</param>
#pragma warning disable RECS0165 // Asynchronous methods should return a Task instead of void
        public static async void SafeFireAndForget(this System.Threading.Tasks.Task task, bool continueOnCapturedContext = true, System.Action <System.Exception> onException = null)
#pragma warning restore RECS0165 // Asynchronous methods should return a Task instead of void
        {
            try
            {
                await task.ConfigureAwait(continueOnCapturedContext);
            }
            catch (System.Exception ex) when(onException != null)
            {
                onException(ex);
            }
        }
Esempio n. 7
0
        public static void BindFuture <T> (this System.Threading.Tasks.Task <T> task, Future <T> future)
        {
            if (task.IsCompleted)
            {
                future.SetResultFrom(task);
                return;
            }

            task.ConfigureAwait(false).GetAwaiter().OnCompleted(() => {
                future.SetResultFrom(task);
            });
            future.RegisterOnDispose(CancelScopeForTask, task);
        }
Esempio n. 8
0
 // Allocate the async/await state machine only when needed for performance reason.
 // More info about the state machine: https://blogs.msdn.microsoft.com/seteplia/2017/11/30/dissecting-the-async-methods-in-c/
 static async Task ForgetAwaited(Task task)
 {
     try
     {
         // No need to resume on the original SynchronizationContext, so use ConfigureAwait(false)
         await task.ConfigureAwait(false);
     }
     catch (Exception ex)
     {
         MessageBoxHelper.ShowError($"Asynchronous Task failed: {ex.Message}\nSee output pane or log file for more details.", Resources.TaskFailed);
         s_log.Error("Asynchronous Task failed", ex);
     }
 }
 /// <summary>
 /// Starts a Task and lets it run in the background, while silently handles any exceptions.
 /// </summary>
 /// <remarks>
 /// This is similar to the <c>task.FileAndForget(string)</c> method introduced in 16.0, but this doesn't record
 /// telemetry on faults and it doesn't take a string parameter. This also works in all version of Visual Studio.
 /// </remarks>
 public static void FireAndForget(this System.Threading.Tasks.Task task)
 {
     ThreadHelper.JoinableTaskFactory.RunAsync(async() =>
     {
         try
         {
             await task.ConfigureAwait(false);
         }
         catch (Exception ex)
         {
             await ex.LogAsync();
         }
     });
 }
Esempio n. 10
0
        static StackObject *ConfigureAwait_3(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj)
        {
            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
            StackObject *ptr_of_this_method;
            StackObject *__ret = ILIntepreter.Minus(__esp, 2);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
            System.Boolean @continueOnCapturedContext = ptr_of_this_method->Value == 1;

            ptr_of_this_method = ILIntepreter.Minus(__esp, 2);
            System.Threading.Tasks.Task instance_of_this_method = (System.Threading.Tasks.Task) typeof(System.Threading.Tasks.Task).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            var result_of_this_method = instance_of_this_method.ConfigureAwait(@continueOnCapturedContext);

            return(ILIntepreter.PushObject(__ret, __mStack, result_of_this_method));
        }
Esempio n. 11
0
        /// <summary>
        /// Stages all files in the queue
        /// </summary>
        private async static System.Threading.Tasks.Task StageFilesAsync(List <IFileStagingProvider> filesToStage, SequentialFileStagingArtifact seqArtifacts)
        {
            foreach (IFileStagingProvider currentFile in filesToStage)
            {
                // for "retry" and/or "double calls" we ignore files that have already been staged
                if (null == currentFile.StagedFiles)
                {
                    FileToStage fts = currentFile as FileToStage;

                    if (null != fts)
                    {
                        System.Threading.Tasks.Task stageTask = StageOneFileAsync(fts, seqArtifacts);

                        await stageTask.ConfigureAwait(continueOnCapturedContext : false);
                    }
                }
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Starts an asynchronous call to stage the given files.
        /// </summary>
        private static async System.Threading.Tasks.Task StageFilesInternalAsync(List <IFileStagingProvider> filesToStage, IFileStagingArtifact fileStagingArtifact)
        {
            if (null == filesToStage)
            {
                throw new ArgumentNullException("filesToStage");
            }

            if (null == fileStagingArtifact)
            {
                throw new ArgumentNullException("filesStagingArtifact");
            }

            SequentialFileStagingArtifact seqArtifact = fileStagingArtifact as SequentialFileStagingArtifact;

            if (null == seqArtifact)
            {
                throw new ArgumentOutOfRangeException(ErrorMessages.FileStagingIncorrectArtifact);
            }

            // is there any work to do?
            if (null == FindAtLeastOne(filesToStage))
            {
                return;  // now work to do.  none of the files belong to this provider
            }

            // is there any work to do
            if ((null == filesToStage) || (filesToStage.Count <= 0))
            {
                return;  // we are done
            }

            // create a Run task to create the blob containers if needed
            System.Threading.Tasks.Task createContainerTask = System.Threading.Tasks.Task.Run(() => { CreateDefaultBlobContainerAndSASIfNeededReturn(filesToStage, seqArtifact); });

            // wait for container to be created
            await createContainerTask.ConfigureAwait(continueOnCapturedContext : false);

            // begin staging the files
            System.Threading.Tasks.Task stageTask = StageFilesAsync(filesToStage, seqArtifact);

            // wait for files to be staged
            await stageTask.ConfigureAwait(continueOnCapturedContext : false);
        }
Esempio n. 13
0
        public override async System.Threading.Tasks.Task CopyToStreamAsync(
            Stream stream,
            IEnumerable <BatchClientBehavior> additionalBehaviors = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // craft the behavior manager for this call
            BehaviorManager bhMgr = new BehaviorManager(base.CustomBehaviors, additionalBehaviors);

            System.Threading.Tasks.Task <AzureOperationResponse <Models.NodeFile, Models.FileGetFromComputeNodeHeaders> > asyncTask =
                this._poolOperations.ParentBatchClient.ProtocolLayer.GetNodeFileByNode(
                    _poolId,
                    _computeNodeId,
                    base.Name,
                    stream,
                    bhMgr,
                    cancellationToken);

            await asyncTask.ConfigureAwait(continueOnCapturedContext : false);
        }
Esempio n. 14
0
        public static void BindFuture(this tTask task, IFuture future)
        {
            if (task.IsCompleted && !task.IsFaulted)
            {
                future.Complete();
                return;
            }

            task.ConfigureAwait(false).GetAwaiter().OnCompleted(() => {
                // FIXME: ExceptionDispatchInfo?
                if (task.IsFaulted)
                {
                    future.Fail(task.Exception.InnerExceptions.Count == 1 ? task.Exception.InnerException : task.Exception);
                }
                else
                {
                    future.Complete();
                }
            });
            future.RegisterOnDispose(CancelScopeForTask, task);
        }
Esempio n. 15
0
        /// <summary>
        /// Commits this <see cref="CloudJobSchedule" /> to the Azure Batch service.
        /// </summary>
        /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are applied to the Batch service request after the <see cref="CustomBehaviors"/>.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the asynchronous operation.</returns>
        /// <remarks>
        /// <para>The commit operation runs asynchronously.</para>
        /// </remarks>
        public async System.Threading.Tasks.Task CommitAsync(
            IEnumerable <BatchClientBehavior> additionalBehaviors = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // after this no prop access is allowed
            this.propertyContainer.IsReadOnly = true;

            // craft the behavior manager for this call
            BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors);

            // fetch props with admin rights so we can make calls, etc.

            if (BindingState.Unbound == this.propertyContainer.BindingState)
            {
                // take all property changes and create a job schedule
                Models.JobScheduleAddParameter protoJobSchedule = this.GetTransportObject <Models.JobScheduleAddParameter>();

                System.Threading.Tasks.Task <AzureOperationHeaderResponse <Models.JobScheduleAddHeaders> > asyncTask =
                    this.parentBatchClient.ProtocolLayer.AddJobSchedule(protoJobSchedule, bhMgr, cancellationToken);

                await asyncTask.ConfigureAwait(continueOnCapturedContext : false);
            }
            else
            {
                Models.JobSpecification jobSpecification = UtilitiesInternal.CreateObjectWithNullCheck(this.JobSpecification, o => o.GetTransportObject());
                Models.Schedule         schedule         = UtilitiesInternal.CreateObjectWithNullCheck(this.Schedule, o => o.GetTransportObject());
                Models.MetadataItem[]   metadata         = UtilitiesInternal.ConvertToProtocolArray(this.Metadata);

                System.Threading.Tasks.Task <AzureOperationHeaderResponse <Models.JobScheduleUpdateHeaders> > asyncJobScheduleUpdate =
                    this.parentBatchClient.ProtocolLayer.UpdateJobSchedule(
                        this.Id,
                        jobSpecification,
                        metadata,
                        schedule,
                        bhMgr,
                        cancellationToken);

                await asyncJobScheduleUpdate.ConfigureAwait(continueOnCapturedContext : false);
            }
        }
        public override async System.Threading.Tasks.Task RefreshAsync(
            DetailLevel detailLevel = null,
            IEnumerable <BatchClientBehavior> additionalBehaviors = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // create the behavior managaer
            BehaviorManager bhMgr = new BehaviorManager(base.CustomBehaviors, additionalBehaviors, detailLevel);

            System.Threading.Tasks.Task <AzureOperationResponse <Models.NodeFile, Models.FileGetPropertiesFromComputeNodeHeaders> > asyncTask =
                this._poolOperations.ParentBatchClient.ProtocolLayer.GetNodeFilePropertiesByNode(
                    _poolId,
                    _computeNodeId,
                    base.Path,
                    bhMgr,
                    cancellationToken);

            AzureOperationResponse <Models.NodeFile, Models.FileGetPropertiesFromComputeNodeHeaders> response = await asyncTask.ConfigureAwait(continueOnCapturedContext : false);

            // immediately available to all threads
            System.Threading.Interlocked.Exchange(ref base.fileItemBox, new FileItemBox(response.Body));
        }
Esempio n. 17
0
        /// <summary>
        /// Refreshes the current <see cref="CloudJobSchedule"/>.
        /// </summary>
        /// <param name="detailLevel">The detail level for the refresh.  If a detail level which omits the <see cref="Id"/> property is specified, refresh will fail.</param>
        /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are applied to the Batch service request after the <see cref="CustomBehaviors"/>.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> representing the asynchronous refresh operation.</returns>
        public async System.Threading.Tasks.Task RefreshAsync(
            DetailLevel detailLevel = null,
            IEnumerable <BatchClientBehavior> additionalBehaviors = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // craft the behavior manager for this call
            BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors, detailLevel);

            // start call
            System.Threading.Tasks.Task <AzureOperationResponse <Models.CloudJobSchedule, Models.JobScheduleGetHeaders> > asyncTask =
                this.parentBatchClient.ProtocolLayer.GetJobSchedule(this.Id, bhMgr, cancellationToken);

            AzureOperationResponse <Models.CloudJobSchedule, Models.JobScheduleGetHeaders> response = await asyncTask.ConfigureAwait(continueOnCapturedContext : false);

            // get job schedule from response
            Models.CloudJobSchedule newProtocolJobSchedule = response.Body;

            this.propertyContainer = new PropertyContainer(newProtocolJobSchedule);
        }
Esempio n. 18
0
        /// <summary>
        /// Gets the specified <see cref="CloudJobSchedule"/>.
        /// </summary>
        /// <param name="jobScheduleId">The id of the job schedule to get.</param>
        /// <param name="detailLevel">A <see cref="DetailLevel"/> used for controlling which properties are retrieved from the service.</param>
        /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are applied to the Batch service request after the <see cref="CustomBehaviors"/> and <paramref name="detailLevel"/>.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
        /// <returns>A <see cref="CloudJobSchedule"/> containing information about the specified Azure Batch job schedule.</returns>
        /// <remarks>The get job schedule operation runs asynchronously.</remarks>
        public async System.Threading.Tasks.Task <CloudJobSchedule> GetJobScheduleAsync(
            string jobScheduleId,
            DetailLevel detailLevel = null,
            IEnumerable <BatchClientBehavior> additionalBehaviors = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // set up behavior manager
            BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors, detailLevel);

            using (System.Threading.Tasks.Task <AzureOperationResponse <Models.CloudJobSchedule, Models.JobScheduleGetHeaders> > asyncTask =
                       this.ParentBatchClient.ProtocolLayer.GetJobSchedule(jobScheduleId, bhMgr, cancellationToken))
            {
                AzureOperationResponse <Models.CloudJobSchedule, Models.JobScheduleGetHeaders> result = await asyncTask.ConfigureAwait(continueOnCapturedContext : false);

                // construct a new object bound to the protocol layer object
                CloudJobSchedule newWI = new CloudJobSchedule(this.ParentBatchClient, result.Body, this.CustomBehaviors);

                return(newWI);
            }
        }
Esempio n. 19
0
 public static void AssemblyCleanup()
 {
     System.Threading.Tasks.Task t = TraceBuffer.Flush();
     t.ConfigureAwait(false).GetAwaiter().GetResult();
 }
Esempio n. 20
0
        /// <summary>
        /// Attempts to download the file.
        /// </summary>
        /// <param name="uri">The parsed <see cref="Uri"/> of the request.</param>
        private void Download(Uri uri)
        {
            // The main reason to use HttpClient vs WebClient is because we can pass a message handler for unit tests to mock
            using (var client = new HttpClient(HttpMessageHandler ?? new HttpClientHandler(), disposeHandler: true))
            {
                // Only get the response without downloading the file so we can determine if the file is already up-to-date
                using (HttpResponseMessage response = client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, _cancellationTokenSource.Token)
                                                      .ConfigureAwait(continueOnCapturedContext: false)
                                                      .GetAwaiter()
                                                      .GetResult())
                {
                    try
                    {
                        response.EnsureSuccessStatusCode();
                    }
                    catch (HttpRequestException e)
                    {
                        // HttpRequestException does not have the status code so its wrapped and thrown here so that later on we can determine
                        // if a retry is possible based on the status code
                        throw new CustomHttpRequestException(e.Message, e.InnerException, response.StatusCode);
                    }

                    if (!TryGetFileName(response, out string filename))
                    {
                        Log.LogErrorWithCodeFromResources("DownloadFile.ErrorUnknownFileName", SourceUrl, nameof(DestinationFileName));
                        return;
                    }

                    DirectoryInfo destinationDirectory = Directory.CreateDirectory(DestinationFolder.ItemSpec);

                    var destinationFile = new FileInfo(Path.Combine(destinationDirectory.FullName, filename));

                    // The file is considered up-to-date if its the same length.  This could be inaccurate, we can consider alternatives in the future
                    if (ShouldSkip(response, destinationFile))
                    {
                        Log.LogMessageFromResources(MessageImportance.Normal, "DownloadFile.DidNotDownloadBecauseOfFileMatch", SourceUrl, destinationFile.FullName, nameof(SkipUnchangedFiles), "true");

                        DownloadedFile = new TaskItem(destinationFile.FullName);

                        return;
                    }

                    try
                    {
                        using (var target = new FileStream(destinationFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None))
                        {
                            Log.LogMessageFromResources(MessageImportance.High, "DownloadFile.Downloading", SourceUrl, destinationFile.FullName, response.Content.Headers.ContentLength);

                            Task task = response.Content.CopyToAsync(target);

                            task.ConfigureAwait(continueOnCapturedContext: false);

                            task.Wait(_cancellationTokenSource.Token);

                            DownloadedFile = new TaskItem(destinationFile.FullName);
                        }
                    }
                    finally
                    {
                        if (DownloadedFile == null)
                        {
                            // Delete the file if anything goes wrong during download.  This could be destructive but we don't want to leave
                            // partially downloaded files on disk either.  Alternatively we could download to a temporary location and copy
                            // on success but we are concerned about the added I/O
                            destinationFile.Delete();
                        }
                    }
                }
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Refreshes the current <see cref="CloudTask"/>.
        /// </summary>
        /// <param name="detailLevel">The detail level for the refresh.  If a detail level which omits the <see cref="Id"/> property is specified, refresh will fail.</param>
        /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are applied to the Batch service request after the <see cref="CustomBehaviors"/>.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the asynchronous operation.</returns>
        public async System.Threading.Tasks.Task RefreshAsync(
            DetailLevel detailLevel = null,
            IEnumerable <BatchClientBehavior> additionalBehaviors = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            UtilitiesInternal.ThrowOnUnbound(this.propertyContainer.BindingState);

            // create the behavior managaer
            BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors, detailLevel);

            System.Threading.Tasks.Task <AzureOperationResponse <Models.CloudTask, Models.TaskGetHeaders> > asyncTask =
                this.parentBatchClient.ProtocolLayer.GetTask(
                    this.parentJobId,
                    this.Id,
                    bhMgr,
                    cancellationToken);

            AzureOperationResponse <Models.CloudTask, Models.TaskGetHeaders> response = await asyncTask.ConfigureAwait(continueOnCapturedContext : false);

            // get task from response
            Models.CloudTask newProtocolTask = response.Body;

            // immediately available to all threads
            this.propertyContainer = new PropertyContainer(newProtocolTask);
        }
        /// <summary>
        /// Refreshes the current <see cref="ComputeNode"/>.
        /// </summary>
        /// <param name="detailLevel">The detail level for the refresh. If a detail level which omits the <see cref="Id"/> property is specified, refresh will fail.</param>
        /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are applied to the Batch service request after the <see cref="CustomBehaviors"/>.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous refresh operation.</returns>
        public async Task RefreshAsync(DetailLevel detailLevel = null, IEnumerable <BatchClientBehavior> additionalBehaviors = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            // create the behavior manager
            BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors, detailLevel);

            System.Threading.Tasks.Task <AzureOperationResponse <Models.ComputeNode, Models.ComputeNodeGetHeaders> > asyncTask =
                this.parentBatchClient.ProtocolLayer.GetComputeNode(this.parentPoolId, this.Id, bhMgr, cancellationToken);

            AzureOperationResponse <Models.ComputeNode, Models.ComputeNodeGetHeaders> response = await asyncTask.ConfigureAwait(continueOnCapturedContext : false);

            // get pool from response
            Models.ComputeNode newProtocolComputeNode = response.Body;

            this.propertyContainer = new PropertyContainer(newProtocolComputeNode);
        }
Esempio n. 23
0
        private static async Task WriteMessageCore(ValueTask writeMessageTask)
        {
            await writeMessageTask.ConfigureAwait(false);

            GrpcEventSource.Log.MessageSent();
        }
Esempio n. 24
0
        /// <summary>
        /// Stage a single file.
        /// </summary>
        private async static System.Threading.Tasks.Task StageOneFileAsync(FileToStage stageThisFile, SequentialFileStagingArtifact seqArtifacts)
        {
            StagingStorageAccount storecreds = stageThisFile.StagingStorageAccount;
            string containerName             = seqArtifacts.BlobContainerCreated;

            // TODO: this flattens all files to the top of the compute node/task relative file directory. solve the hiearchy problem (virt dirs?)
            string blobName = Path.GetFileName(stageThisFile.LocalFileToStage);

            // Create the storage account with the connection string.
            CloudStorageAccount storageAccount = new CloudStorageAccount(
                new WindowsAzure.Storage.Auth.StorageCredentials(storecreds.StorageAccount, storecreds.StorageAccountKey),
                blobEndpoint: storecreds.BlobUri,
                queueEndpoint: null,
                tableEndpoint: null,
                fileEndpoint: null);

            CloudBlobClient    client    = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = client.GetContainerReference(containerName);
            ICloudBlob         blob      = container.GetBlockBlobReference(blobName);
            bool doesBlobExist;

            try
            {
                // fetch attributes so we can compare file lengths
                System.Threading.Tasks.Task fetchTask = blob.FetchAttributesAsync();

                await fetchTask.ConfigureAwait(continueOnCapturedContext : false);

                doesBlobExist = true;
            }
            catch (StorageException scex)
            {
                // check to see if blob does not exist
                if ((int)System.Net.HttpStatusCode.NotFound == scex.RequestInformation.HttpStatusCode)
                {
                    doesBlobExist = false;
                }
                else
                {
                    throw;  // unknown exception, throw to caller
                }
            }

            bool mustUploadBlob = true; // we do not re-upload blobs if they have already been uploaded

            if (doesBlobExist)          // if the blob exists, compare
            {
                FileInfo fi = new FileInfo(stageThisFile.LocalFileToStage);

                // since we don't have a hash of the contents... we check length
                if (blob.Properties.Length == fi.Length)
                {
                    mustUploadBlob = false;
                }
            }

            if (mustUploadBlob)
            {
                // upload the file
                System.Threading.Tasks.Task uploadTask = blob.UploadFromFileAsync(stageThisFile.LocalFileToStage);

                await uploadTask.ConfigureAwait(continueOnCapturedContext : false);
            }

            // get the SAS for the blob
            string blobSAS      = ConstructBlobSource(seqArtifacts.DefaultContainerSAS, blobName);
            string nodeFileName = stageThisFile.NodeFileName;

            // create a new ResourceFile and populate it.  This file is now staged!
            stageThisFile.StagedFiles = new ResourceFile[] { ResourceFile.FromUrl(blobSAS, nodeFileName) };
        }
Esempio n. 25
0
        private void log(string level, string message)
        {
            //Try to get the threadId which is very useful when debugging
            string threadId = null;
            try
            {
#if NETFX_CORE
                threadId = Environment.CurrentManagedThreadId.ToString();
#else
                threadId = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
#endif
            }
            catch (Exception) { }

            string logStringToWrite;
            if (threadId != null)
                logStringToWrite = DateTime.Now.Hour.ToString() + "." + DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString() + "." + DateTime.Now.Millisecond.ToString() + " [" + threadId + " - " + level + "] - " + message;
            else
                logStringToWrite = DateTime.Now.Hour.ToString() + "." + DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString() + "." + DateTime.Now.Millisecond.ToString() + " [" + level + "] - " + message;

#if !NETFX_CORE
            if (_currentLogMode == LogMode.ConsoleAndLogFile || _currentLogMode == LogMode.ConsoleOnly)
                Console.WriteLine(logStringToWrite);
#endif

            if ((_currentLogMode == LogMode.ConsoleAndLogFile || _currentLogMode == LogMode.LogFileOnly) && LogFileLocationName != null)
            {
                try
                {
                    lock (_locker)
                    {

#if NETFX_CORE
                        System.Threading.Tasks.Task writeTask = new System.Threading.Tasks.Task(async () =>
                            {
                                while (true)
                                {
                                    try
                                    {
                                        Windows.Storage.StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
                                        Windows.Storage.StorageFile file = await folder.CreateFileAsync(LogFileLocationName, Windows.Storage.CreationCollisionOption.OpenIfExists);
                                        await Windows.Storage.FileIO.AppendTextAsync(file, logStringToWrite + "\n");
                                        break;
                                    }
                                    catch (Exception) { }
                                }
                            });

                        writeTask.ConfigureAwait(false);
                        writeTask.Start();
                        writeTask.Wait(); 
#else
                        using (var sw = new System.IO.StreamWriter(LogFileLocationName, true))
                            sw.WriteLine(logStringToWrite);
#endif                        
                    }
                }
                catch (Exception) { }
            }
        }
Esempio n. 26
0
        private void log(string level, string message)
        {
            //Try to get the threadId which is very useful when debugging
            string threadId = null;

            try
            {
#if NETFX_CORE
                threadId = Environment.CurrentManagedThreadId.ToString();
#else
                threadId = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
#endif
            }
            catch (Exception) { }

            string logStringToWrite;
            if (threadId != null)
            {
                logStringToWrite = DateTime.Now.Hour.ToString() + "." + DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString() + "." + DateTime.Now.Millisecond.ToString() + " [" + threadId + " - " + level + "] - " + message;
            }
            else
            {
                logStringToWrite = DateTime.Now.Hour.ToString() + "." + DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString() + "." + DateTime.Now.Millisecond.ToString() + " [" + level + "] - " + message;
            }

#if !NETFX_CORE
            if (_currentLogMode == LogMode.ConsoleAndLogFile || _currentLogMode == LogMode.ConsoleOnly)
            {
                Console.WriteLine(logStringToWrite);
            }
#endif

            if ((_currentLogMode == LogMode.ConsoleAndLogFile || _currentLogMode == LogMode.LogFileOnly) && LogFileLocationName != null)
            {
                try
                {
                    lock (_locker)
                    {
#if NETFX_CORE
                        System.Threading.Tasks.Task writeTask = new System.Threading.Tasks.Task(async() =>
                        {
                            while (true)
                            {
                                try
                                {
                                    Windows.Storage.StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
                                    Windows.Storage.StorageFile file     = await folder.CreateFileAsync(LogFileLocationName, Windows.Storage.CreationCollisionOption.OpenIfExists);
                                    await Windows.Storage.FileIO.AppendTextAsync(file, logStringToWrite + "\n");
                                    break;
                                }
                                catch (Exception) { }
                            }
                        });

                        writeTask.ConfigureAwait(false);
                        writeTask.Start();
                        writeTask.Wait();
#else
                        using (var sw = new System.IO.StreamWriter(LogFileLocationName, true))
                            sw.WriteLine(logStringToWrite);
#endif
                    }
                }
                catch (Exception) { }
            }
        }
Esempio n. 27
0
        /// <summary>
        /// Refreshes the current <see cref="CloudJob"/>.
        /// </summary>
        /// <param name="detailLevel">The detail level for the refresh.  If a detail level which omits the <see cref="Id"/> property is specified, refresh will fail.</param>
        /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are applied to the Batch service request after the <see cref="CustomBehaviors"/>.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous refresh operation.</returns>
        public async System.Threading.Tasks.Task RefreshAsync(
            DetailLevel detailLevel = null,
            IEnumerable <BatchClientBehavior> additionalBehaviors = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // create the behavior managaer
            BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors, detailLevel);

            // start operation
            System.Threading.Tasks.Task <AzureOperationResponse <Models.CloudJob, Models.JobGetHeaders> > asyncTask =
                this.parentBatchClient.ProtocolLayer.GetJob(this.Id, bhMgr, cancellationToken);

            AzureOperationResponse <Models.CloudJob, Models.JobGetHeaders> response = await asyncTask.ConfigureAwait(continueOnCapturedContext : false);

            // get job from response
            Models.CloudJob newProtocolJob = response.Body;

            PropertyContainer container = new PropertyContainer(newProtocolJob);

            // immediately available to all threads
            this.propertyContainer = container;
        }
                static async System.Threading.Tasks.Task <object> TaskCast <T>(System.Threading.Tasks.Task <T> task)
                {
                    var t = await task.ConfigureAwait(false);

                    return((object)t);
                }