public void CancelBatch(string batchId)
        {
            try
            {
                // Parameter validation
                GeresAssertionHelper.AssertNullOrEmpty(batchId, "batchId");

                // Core execution logic
                GeresEventSource.Log.WebApiBatchCancelReceived(batchId);
                _jobController.CancelBatch(batchId);
                GeresEventSource.Log.WebApiBatchCancelSuccessful(batchId);
            }
            catch (ArgumentException ex)
            {
                Trace.TraceWarning("WebAPI - JobController -- Invalid batch-parameter passed into system: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiInvalidCancelBatchParameterReceived(batchId, ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            catch (InvalidOperationException ex)
            {
                Trace.TraceWarning("WebAPI - JobController -- Batch could not be cancelled in system: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiInvalidCancelBatchOperation(batchId, ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                Trace.TraceError("WebAPI - JobController -- Unknown exception occured: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiUnknownExceptionOccured(ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
        public void CancelJob(string jobId)
        {
            try
            {
                // Parameter Validations
                GeresAssertionHelper.AssertNullOrEmpty(jobId, "jobId");

                // Cancels a job
                GeresEventSource.Log.WebApiCancelJobReceived(jobId);
                _jobController.CancelJob(jobId);
                GeresEventSource.Log.WebApiCancelJobSuccessful(jobId);
            }
            catch (ArgumentException ex)
            {
                Trace.TraceWarning("WebAPI - JobController -- Invalid jobId passed into system: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiCancelJobInvalidParameterSubmitted(jobId, ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            catch (InvalidOperationException ex)
            {
                Trace.TraceWarning("WebAPI - JobController -- Job could not be cancelled in system: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiCancelJobInvalidCancellation(jobId, ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                Trace.TraceError("WebAPI - JobController -- Unknown exception occured: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiUnknownExceptionOccured(ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
        public IQueryable <Job> GetBatchStatus(string batchId, int numOfRecords = 1000, string continuationToken = "")
        {
            try
            {
                // Parameter Validations
                GeresAssertionHelper.AssertNullOrEmpty(batchId, "batchId");

                // Log the query request
                GeresEventSource.Log.WebApiMonitoringQueryBatchReceived(batchId);

                using (var repo = RepositoryFactory.CreateJobsRepository(CloudConfigurationManager.GetSetting(GlobalConstants.STORAGE_CONNECTIONSTRING_CONFIGNAME)))
                {
                    var jobEntities = repo.GetJobs(batchId, numOfRecords, ref continuationToken);
                    if (jobEntities == null)
                    {
                        throw new HttpResponseException(HttpStatusCode.NotFound);
                    }

                    List <Job> jobs = new List <Job>();
                    foreach (JobEntity je in jobEntities)
                    {
                        Job job = JobEntity.ConvertToJob(je);
                        jobs.Add(job);
                    }

                    // If the continuation token is not null, add it to the HTTP Header
                    if (!string.IsNullOrEmpty(continuationToken))
                    {
                        System.Web.HttpContext.Current.Response.AddHeader("gerespagingtoken", continuationToken);
                    }

                    // Log the query completion
                    GeresEventSource.Log.WebApiMonitoringQueryBatchSuccessful(batchId, jobs.Count);

                    return(jobs.AsQueryable());
                }
            }
            catch (HttpResponseException ex)
            {
                throw ex;
            }
            catch (ArgumentException ex)
            {
                Trace.TraceWarning("WebAPI - JobMonitorController -- Invalid parameter passed into system: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiBatchMonitoringInvalidParameterReceived(batchId, ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                Trace.TraceError("WebAPI - JobMonitorController -- Unknown exception occured: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiUnknownExceptionOccured(ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
 private static void AssertJobParameter(Job newJob)
 {
     GeresAssertionHelper.AssertNull(newJob, "newJob");
     GeresAssertionHelper.AssertNullOrEmpty(newJob.JobName, "newJob.JobName");
     GeresAssertionHelper.AssertNull(newJob.Parameters, "newJob.Parameters");
     GeresAssertionHelper.AssertNullOrEmptyOrWhitespace(newJob.JobType, "newJob.JobType");
     GeresAssertionHelper.AssertNullOrEmptyOrWhitespace(newJob.JobProcessorPackageName, "newJob.JobProcessorPackageName");
     GeresAssertionHelper.AssertNullOrEmptyOrWhitespace(newJob.TenantName, "newJob.TenantName");
     GeresAssertionHelper.AssertLength(newJob.TenantName, "newJob.TenantName", 1, 15);
 }
        public Job GetJobStatus(string jobId, string batchId = "")
        {
            try
            {
                // Parameter Assertions
                GeresAssertionHelper.AssertNullOrEmpty(jobId, "jobId");

                // Logging the message
                GeresEventSource.Log.WebApiMonitoringQueryJobReceived(jobId);

                // Core execution logic
                using (var repo = RepositoryFactory.CreateJobsRepository(CloudConfigurationManager.GetSetting(GlobalConstants.STORAGE_CONNECTIONSTRING_CONFIGNAME)))
                {
                    JobEntity jobEntity = null;
                    if (!string.IsNullOrEmpty(batchId))
                    {
                        jobEntity = repo.GetJob(jobId, batchId);
                    }
                    else
                    {
                        jobEntity = repo.GetJob(jobId);
                    }

                    if (jobEntity == null)
                    {
                        throw new HttpResponseException(HttpStatusCode.NotFound);
                    }

                    Job job = JobEntity.ConvertToJob(jobEntity);

                    GeresEventSource.Log.WebApiMonitoringQueryJobSuccessful(job.JobId, job.JobType);

                    return(job);
                }
            }
            catch (HttpResponseException ex)
            {
                throw ex;
            }
            catch (ArgumentException ex)
            {
                Trace.TraceWarning("WebAPI - JobMonitorController -- Invalid parameter passed into system: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiJobMonitoringInvalidParameterReceived(jobId, ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                Trace.TraceError("WebAPI - JobMonitorController -- Unknown exception occured: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiUnknownExceptionOccured(ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
        public List <string> SubmitJobs([FromBody] List <Job> newJobs, string batchId = "")
        {
            try
            {
                // Parameter validations
                GeresAssertionHelper.AssertNull(newJobs, "newJobs");
                foreach (var j in newJobs)
                {
                    AssertJobParameter(j);
                }

                var logJobs = string.Join(", ", newJobs.Select(j => j.JobName).ToArray());
                GeresEventSource.Log.WebApiSubmitJobsReceived(newJobs.Count, logJobs, logJobs, batchId);
                var jobIds = _jobController.SubmitJobs(newJobs, batchId);
                GeresEventSource.Log.WebApiSubmitJobsSuccessful(newJobs.Count, string.Join(", ", jobIds.ToArray()), batchId);

                return(jobIds);
            }
            catch (ArgumentException ex)
            {
                Trace.TraceWarning("WebAPI - JobController -- Invalid job passed into system: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiSubmitJobsInvalidParameterSubmitted(batchId, ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            catch (InvalidOperationException ex)
            {
                Trace.TraceWarning("WebAPI - JobController -- Job could not be submitted to system: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                var logJobs = string.Join(", ", newJobs.Select(j => j.JobName).ToArray());
                GeresEventSource.Log.WebApiSubmitJobsFailed(newJobs.Count, logJobs, logJobs, batchId, ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                Trace.TraceError("WebAPI - JobController -- Unknown exception occured: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

                GeresEventSource.Log.WebApiUnknownExceptionOccured(ex.Message, ex.StackTrace);
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
        /// <summary>
        /// Allow client to request new jobs
        /// Add client to a single user group (as recommended)
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public async Task SubscribeToJob(string jobId)
        {
            try
            {
                // Parameter Validations
                GeresAssertionHelper.AssertNullOrEmpty(jobId, "jobId");

                // register the user so that they get notifications of progress
                GeresEventSource.Log.SignalRHubSubscribeToJobRequestReceived(jobId);
                await Groups.Add(Context.ConnectionId, jobId);

                GeresEventSource.Log.SignalRHubSubscribeToJobRequestSuccessful(jobId);
            }
            catch (ArgumentException ex)
            {
                GeresEventSource.Log.SignalRHubInvalidParameterReceived(jobId, ex.Message, ex.StackTrace);
            }
            catch (Exception ex)
            {
                GeresEventSource.Log.SignalRHubSubscribeToJobFailed(jobId, ex.Message, ex.StackTrace);
            }
        }
 private static void AssertBatchParameter(Batch batch)
 {
     GeresAssertionHelper.AssertNull(batch, "batch");
     GeresAssertionHelper.AssertNullOrEmpty(batch.BatchName, "batch.BatchName");
 }