예제 #1
0
        public async Task <IActionResult> Delete(long id, CancellationToken cancellationToken)
        {
            // don't care if an instance post or not at this point
            var job = await DatabaseContext
                      .Jobs
                      .AsQueryable()
                      .Where(x => x.Id == id && x.Instance.Id == Instance.Id)
                      .FirstOrDefaultAsync(cancellationToken)
                      .ConfigureAwait(false);

            if (job == default)
            {
                return(NotFound());
            }

            if (job.StoppedAt != null)
            {
                return(Conflict(new ErrorMessage(ErrorCode.JobStopped)));
            }

            if (job.CancelRight.HasValue && job.CancelRightsType.HasValue && (AuthenticationContext.GetRight(job.CancelRightsType.Value) & job.CancelRight.Value) == 0)
            {
                return(Forbid());
            }

            var updatedJob = await jobManager.CancelJob(job, AuthenticationContext.User, false, cancellationToken).ConfigureAwait(false);

            return(updatedJob != null?Accepted(updatedJob.ToApi()) : Gone());
        }
예제 #2
0
        public Task <bool> NotifyAsync(TaskInpuModel inputModel, PerformContext context)
        {
            context.WriteLine("**** -------------------- Notifying Task -------------------- ****");

            if (inputModel == null || inputModel?.Id == 0)
            {
                var jobId  = context.BackgroundJob.Id;
                var result = jobManager.CancelJob(jobId);

                context.WriteLine(ConsoleTextColor.Red, $"Job {jobId} has been canceled.");
                context.WriteLine("**** ----------------------------------------------------- ****");

                return(Task.FromResult(result));
            }

            // Fake work
            Thread.Sleep(500);

            var now = dateService.GetNow();

            context.WriteLine("Notifying of successful execution...");
            context.WriteLine($"Date: {now}");
            context.WriteLine($"Task ID: {inputModel.Id}");

            if (inputModel.Tags != null)
            {
                var tags = string.Join(",", inputModel.Tags);
                context.WriteLine($"Tags: {tags}");
            }

            context.WriteLine();
            context.WriteLine("**** ----------------------------------------------------- ****");
            return(Task.FromResult(true));
        }
 public void Handle(JobStoppedMessage message)
 {
     if (string.IsNullOrEmpty(message.Id))
     {
         _manager.CancelAllJobs();
     }
     else
     {
         _manager.CancelJob(message.Id);
     }
 }
예제 #4
0
        /// <inheritdoc />
        public async Task OfflineInstance(Models.Instance metadata, Models.User user, CancellationToken cancellationToken)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }
            logger.LogInformation("Offlining instance ID {0}", metadata.Id);
            IInstance instance;

            lock (instances)
            {
                if (!instances.TryGetValue(metadata.Id, out instance))
                {
                    throw new InvalidOperationException("Instance not online!");
                }
                instances.Remove(metadata.Id);
            }

            try
            {
                // we are the one responsible for cancelling his jobs
                var tasks = new List <Task>();
                await databaseContextFactory.UseContext(async db =>
                {
                    var jobs = db
                               .Jobs
                               .AsQueryable()
                               .Where(x => x.Instance.Id == metadata.Id)
                               .Select(x => new Models.Job
                    {
                        Id = x.Id
                    });
                    await jobs.ForEachAsync(job =>
                    {
                        lock (tasks)
                            tasks.Add(jobManager.CancelJob(job, user, true, cancellationToken));
                    }, cancellationToken).ConfigureAwait(false);
                }).ConfigureAwait(false);

                await Task.WhenAll(tasks).ConfigureAwait(false);

                await instance.StopAsync(cancellationToken).ConfigureAwait(false);
            }
            finally
            {
                instance.Dispose();
            }
        }
        public async Task <IActionResult> Delete(long id, CancellationToken cancellationToken)
        {
            // don't care if an instance post or not at this point
            var job = await DatabaseContext.Jobs.Where(x => x.Id == id && x.Instance.Id == Instance.Id).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);

            if (job == default(Job))
            {
                return(NotFound());
            }

            if (job.StoppedAt != null)
            {
                return(StatusCode((int)HttpStatusCode.Gone));
            }

            if (job.CancelRight.HasValue && job.CancelRightsType.HasValue && (AuthenticationContext.GetRight(job.CancelRightsType.Value) & job.CancelRight.Value) == 0)
            {
                return(Forbid());
            }

            var cancelled = await jobManager.CancelJob(job, AuthenticationContext.User, false, cancellationToken).ConfigureAwait(false);

            return(cancelled ? (IActionResult)Accepted() : StatusCode((int)HttpStatusCode.Gone));
        }
예제 #6
0
        public override void Execute(
            JobDefinition job,
            Action<int, int> notifyProgress,
            Action<string> updateStatus,
            IRepository repositoryService,
            IJobManager jobManager)
        {
            updateStatus("Executing job...");
            JobExecutingView dialog = null;

            Owner.Synch(() => dialog = new JobExecutingView(string.Format("Executing job {0}...", job)));

            jobManager.OpenJob(job.Id);
            jobManager.StartJob();

            var isWorking = true;

            dialog.OnCancelled += (s, e) =>
            {
                lock (this)
                {
                    if (!isWorking)
                        return;
                    isWorking = false;
                }

                jobManager.CancelJob();
            };
            Owner.Synch(() => dialog.Show());

            var state = default(JobState);
            while (isWorking)
            {
                state = jobManager.QueryJobState(job.Id);
                if (state == JobState.Completed ||
                   state == JobState.Failed)
                {
                    lock (this)
                    {
                        lock (this)
                            isWorking = false;
                        break;
                    }
                }

                Thread.Sleep(100);
            }
            Owner.Synch(() => dialog.Close());
            if (state == JobState.Completed)
            {
                return;
                // MessageBox.Show(string.Format("Job {0} completed!", job));
            }
            if (state == JobState.Failed)
            {
                var errorReport = jobManager.GetErrorReport(job.Id);
                Owner.Synch(() => ErrorReportView.ShowReport(errorReport));
                throw new StageFailedException("Job failed!");
                //MessageBox.Show(string.Format("Job {0} failed!", job));
            }
        }