// GET /api/lra/{appId} ? jobId=GUID
        // where {appId} is the application ID.
        // Returns the status of the job.
        // Returns 404 if there is no such job or appId is incorrect.
        public HttpResponseMessage Get(Guid appId, Guid jobId)
        {
            var status = scheduler.TryGetStatus(appId, jobId);

            if (status != null)
            {
                var st = status.Value;
                switch (st.Tag)
                {
                case JobStatusWithInfo.Tags.Succeeded:
                    return(HttpResponses.PlainText(Request, "Succeeded", HttpStatusCode.OK /* 200 */));

                case JobStatusWithInfo.Tags.Queued:
                    return(HttpResponses.PlainText(Request, ((JobStatusWithInfo.Queued)st).position.ToString(), HttpStatusCode.Created /* 201 */));

                case JobStatusWithInfo.Tags.Executing:
                    var     x       = (JobStatusWithInfo.Executing)st;
                    int     elapsed = (int)(DateTimeOffset.Now.Subtract(x.started)).TotalMilliseconds;
                    JObject json    = new JObject(new object[] {
                        new JProperty("started", x.started.ToString("o")),
                        new JProperty("elapsed", elapsed)
                    });
                    return(HttpResponses.Json(Request, json.ToString(), HttpStatusCode.Accepted /* 202 */));

                case JobStatusWithInfo.Tags.Failed:
                    return(HttpResponses.PlainText(Request, ((JobStatusWithInfo.Failed)st).message, (HttpStatusCode)203));
                }
                return(Request.CreateResponse((HttpStatusCode)501, new HttpError("Unknown status")));
            }

            return(Request.CreateResponse(HttpStatusCode.NotFound, new HttpError("Job not found")));
        }
Пример #2
0
        protected async Task <HttpResponseMessage> ExecuteAsync(int timeoutMs)
        {
            var    log   = new DefaultLogService();
            string input = await Request.Content.ReadAsStringAsync();

            try
            {
                JobsRunner.JobResult result = JobsRunner.Job.RunToCompletion(Path.Combine(basePath, executableName), input, timeoutMs);

                if (result.Errors.Length > 0)
                {
                    var contents = new LogContents(null, result.Errors);
                    faultLogger.Add(DateTime.Now, typeof(JobController).Assembly.GetName().Version.ToString(), input, contents);
                }
                return(HttpResponses.Json(Request, result.Content));
            }
            catch (System.TimeoutException ex)
            {
                RegisterException(faultLogger, log, input, ex);
                return(HttpResponses.PlainText(Request, "Timeout while waiting for the execution to complete", HttpStatusCode.NoContent)); // status 204 if timeout
            }
            catch (Exception ex)
            {
                RegisterException(faultLogger, log, input, ex);
                throw ex;
            }
        }
        // GET /api/lra/{appId}/result ? jobId=GUID
        // where {appId} is the application ID.
        // Returns the status of the job.
        // Returns 404 if there is no such job or appId is incorrect.
        public HttpResponseMessage GetResult(Guid appId, Guid jobId)
        {
            var result = scheduler.TryGetResult(appId, jobId);

            if (result != null)
            {
                using (StreamReader reader = new StreamReader(result.Value))
                {
                    var s = reader.ReadToEnd();
                    return(HttpResponses.Json(Request, s));
                }
            }
            return(Request.CreateResponse(HttpStatusCode.NotFound, new HttpError("Job result is not available")));
        }