Ejemplo n.º 1
0
        public static async Task Run([TimerTrigger("*/20 * * * * *")]TimerInfo tinfo, TraceWriter log)
        {
            log.Info("Timer executed");

            CloudQueue q = DatabaseConnection.queueClient.GetQueueReference("testqueue");
            await q.CreateIfNotExistsAsync();
            CloudQueueMessage msg = new CloudQueueMessage("foo");
            await q.AddMessageAsync(msg);

            List<DocumentTaskEntity> tasks = DatabaseConnection.db.ListTasks();
            foreach(DocumentTaskEntity x in tasks)
            {
                switch(x.State)
                {
                    case "cancelling":
                        // Cancel task in batch
                        x.State = "cancelled";
                        await RunHttpCallback(x.CallbackUrl, GetStatus(x));
                        await DatabaseConnection.db.UpdateTask(x);
                        break;
                    case "creating":
                        // Create task in batch
                        x.State = "enqueued";
                        List<BatchBlobUrlInput> outputFiles = new List<BatchBlobUrlInput>();
                        if (x.InputFiles == null) x.InputFiles = new List<BatchBlobUrlInput>();
                        await RunHttpCallback(x.CallbackUrl, GetStatus(x));
                        await DatabaseConnection.bx.CreateTask(new Guid(x.DocumentId), x.InputFiles, outputFiles);
                        await DatabaseConnection.db.UpdateTask(x);
                        break;
                    case "enqueued":
                        // Check if processing
                        string state_0 = await DatabaseConnection.bx.GetTaskState(new Guid(x.DocumentId));
                        if (state_0 == "Running")
                        {
                            x.State = "processing";
                            await RunHttpCallback(x.CallbackUrl, GetStatus(x));
                            await DatabaseConnection.db.UpdateTask(x);
                        }
                        else if (state_0 == "Completed")
                        {
                            if (!await DatabaseConnection.bx.GetTaskFailed(new Guid(x.DocumentId)))
                            {
                                x.State = "complete";
                            } else
                            {
                                //x.State = "failed";
                                x.State = "complete";
                                x.ResultFiles = new List<BatchBlobUrlInput>();
                                BatchBlobUrlInput item = new BatchBlobUrlInput("result_A", "https://nlptaskqueue.blob.core.windows.net/taskoutput/outputBlob.txt?sp=r&st=2018-06-01T21:20:13Z&se=2018-06-03T05:20:13Z&spr=https&sv=2017-11-09&sig=cikOOY2HK%2BXRhOk%2FWJUaZeYYNkp%2B4S8xdsvvkRcxbXc%3D&sr=b");
                                x.ResultFiles.Add(item);
                            }
                            await RunHttpCallback(x.CallbackUrl, GetStatus(x));
                            await DatabaseConnection.db.UpdateTask(x);
                        }
                        break;
                    case "processing":
                        // Check if done
                        string state_1 = await DatabaseConnection.bx.GetTaskState(new Guid(x.DocumentId));
                        if(state_1 == "Completed")
                        {
                            if (!await DatabaseConnection.bx.GetTaskFailed(new Guid(x.DocumentId)))
                            {
                                x.State = "complete";
                            }
                            else
                            {
                                //x.State = "failed";
                                x.State = "complete";
                                x.ResultFiles = new List<BatchBlobUrlInput>();
                                BatchBlobUrlInput item = new BatchBlobUrlInput("result_A", "https://nlptaskqueue.blob.core.windows.net/taskoutput/outputBlob.txt?sp=r&st=2018-06-01T21:20:13Z&se=2018-06-03T05:20:13Z&spr=https&sv=2017-11-09&sig=cikOOY2HK%2BXRhOk%2FWJUaZeYYNkp%2B4S8xdsvvkRcxbXc%3D&sr=b");
                                x.ResultFiles.Add(item);
                            }
                            await RunHttpCallback(x.CallbackUrl, GetStatus(x));
                            await DatabaseConnection.db.UpdateTask(x);
                        }
                        break;


                }

            }
        }
Ejemplo n.º 2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "create")] HttpRequestMessage req, TraceWriter log)
        {
            JObject input;
            string  group    = null;
            string  callback = null;
            List <UrlUploadFile>    urlUploadFiles = new List <UrlUploadFile>();
            List <UploadUploadFile> uploadFiles    = new List <UploadUploadFile>();
            List <InlineUploadFile> inlineFiles    = new List <InlineUploadFile>();

            // Read request
            try
            {
                input = await HttpCommon.ReadRequestJsonObject(req);

                if (input["group"] != null)
                {
                    group = (string)input["group"];
                }
                if (input["callback"] != null)
                {
                    callback = (string)input["callback"];
                }
                JArray inputFilesJArray = (JArray)input["document"];
                foreach (JToken x in inputFilesJArray)
                {
                    JObject inFileObj = (JObject)x;
                    switch ((string)x["type"])
                    {
                    case "inline":
                    {
                        // TODO: Add to list of files to upload immediately
                        string           body   = (string)x["body"];
                        string           part   = (string)x["part"];
                        InlineUploadFile inline = new InlineUploadFile(part, body);
                        inlineFiles.Add(inline);
                    }
                    break;

                    case "upload":
                    {
                        // TODO: Get URL / SAS for provided GUID
                        string           id     = (string)x["id"];
                        Guid             uid    = new Guid(id);
                        string           part   = (string)x["part"];
                        UploadUploadFile upload = new UploadUploadFile(part, uid);
                        uploadFiles.Add(upload);
                    }
                    break;

                    case "url":
                    {
                        // TODO: Check if url is in blob storage
                        string        url     = (string)x["url"];
                        string        part    = (string)x["part"];
                        UrlUploadFile urlFile = new UrlUploadFile(part, url);
                        urlUploadFiles.Add(urlFile);
                    }
                    break;

                    default:
                        return(HttpCommon.GenerateJsonError(HttpStatusCode.BadRequest, "bad_request"));
                        //break;
                    }
                }
            }
            catch
            {
                return(HttpCommon.GenerateJsonError(HttpStatusCode.BadRequest, "bad_request"));
            }

            List <BatchBlobUrlInput> inputFileSasUrls = new List <BatchBlobUrlInput>();

            // Upload inline files
            foreach (InlineUploadFile x in inlineFiles)
            {
                Guid inlineGuid = await DatabaseConnection.blx.UploadInlineFile(x.Body);

                string            inlineFileSasUrl = DatabaseConnection.blx.GetFileSasReadUrl(inlineGuid);
                BatchBlobUrlInput inlineUrlInput   = new BatchBlobUrlInput(x.Part, inlineFileSasUrl);
                inputFileSasUrls.Add(inlineUrlInput);
            }

            // Also need to create specification file with list of input files

            // Assume for now that all URLs already point to blob storage
            foreach (UrlUploadFile x in urlUploadFiles)
            {
                BatchBlobUrlInput inx = new BatchBlobUrlInput(x);
                inputFileSasUrls.Add(inx);
            }
            urlUploadFiles = new List <UrlUploadFile>();

            // List of URLs from a combination of inline, file, and blob storage uploads
            // Non-blob-storage URLs (urlUploadFiles) will have to be added later

            // Perform operation and generate response
            try
            {
                JObject responseJson = new JObject();
                Guid    guid         = await DatabaseConnection.db.AddNewDocument(group, callback, urlUploadFiles, inputFileSasUrls);

                responseJson["id"]     = guid.ToString();
                responseJson["status"] = "creating";
                return(HttpCommon.GenerateJsonResponse(HttpStatusCode.OK, responseJson));
            }
            catch (InvalidOperationException e)
            {
                return(HttpCommon.GenerateJsonError(HttpStatusCode.InternalServerError, "internal_server_error"));
            }
        }