示例#1
0
        private void execute(CloudQueueMessage currentMessage)
        {
            ProcessRequest myRequest = null;
            string         txt;

            Common.ResourceAccess.IButlerStorageManager storageManager = null;

            try
            {
                lock (myLock)
                {
                    currentProcessRunning += 1;
                }
                MediaButler.Common.ButlerRequest watcherRequest = Newtonsoft.Json.JsonConvert.DeserializeObject <ButlerRequest>(currentMessage.AsString);
                //Load Workflow's steps
                List <StepHandler> mysteps = BuildChain(watcherRequest.WorkflowName);

                myRequest = GetCurrentContext(watcherRequest.WorkflowName);
                myRequest.CurrentMessage = currentMessage;
                myRequest.ProcessTypeId  = watcherRequest.WorkflowName;
                //ProcessInstanceId:
                //Single File: MessageID Guid (random)
                //multiFile package: Container folder guid ID (set for client)
                myRequest.ProcessInstanceId = this.getProcessId(watcherRequest.ControlFileUri, watcherRequest.MessageId.ToString());

                myRequest.ProcessConfigConn = this.myProcessConfigConn;
                myRequest.IsResumeable      = (this.ReadConfigOrDefault(myRequest.ProcessTypeId + ".IsResumeable") == "1");

                //Restore Status
                storageManager = Common.ResourceAccess.BlobManagerFactory.CreateBlobManager(myRequest.ProcessConfigConn);
                myRequest      = restoreRequestOrCreateMetadata(myRequest, mysteps);

                //2.Execute Chain
                txt = string.Format("[{0}] Starting new Process, type {1} and ID {2}", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId);
                Trace.TraceInformation(txt);
                mysteps.FirstOrDefault().HandleRequest(myRequest);
                //FinishProcess();
                txt = string.Format("[{0}] Finish Process, type {1} and ID {2}", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId);
                Trace.TraceInformation(txt);
                //Finish Status
                myRequest.CurrentStepIndex = Configuration.successFinishProcessStep;
                storageManager.PersistProcessStatus(myRequest);

                lock (myLock)
                {
                    currentProcessRunning -= 1;
                }
            }
            catch (Exception xxx)
            {
                if (myRequest != null)
                {
                    //foreach (Exception item in myRequest.Exceptions)
                    foreach (string errorTxt in myRequest.Exceptions)
                    {
                        //Full Rollback?
                        txt = string.Format("[{0}] Error list process {1} intance {2} error: {3}", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId, errorTxt);
                        Trace.TraceError(txt);
                    }
                    txt = string.Format("[{0}] Error list process {1} intance {2} error: {3}", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId, xxx.Message);
                    myRequest.Exceptions.Add(txt);

                    //Update Status
                    myRequest.CurrentStepIndex = Configuration.failFinishProcessStep;
                    storageManager.PersistProcessStatus(myRequest);
                }
                else
                {
                    txt = string.Format("[{0}] Error {1} without context Request yet", this.GetType().FullName, xxx.Message);
                    Trace.TraceError(xxx.Message);
                }
                //Exception no Managed

                lock (myLock)
                {
                    currentProcessRunning -= 1;
                }
            }
            //3.return control
            if (myRequest != null)
            {
                myRequest.DisposeRequest();
            }
            else
            {
                Trace.TraceError("myRequest is null raw message " + currentMessage.AsString);
            }
            myRequest = null;

            Trace.Flush();
        }
        private void execute(CloudQueueMessage currentMessage)
        {
            ProcessRequest myRequest = null;
            string         txt;

            try
            {
                lock (myLock)
                {
                    currentProcessRunning += 1;
                }
                MediaButler.Common.ButlerRequest watcherRequest = Newtonsoft.Json.JsonConvert.DeserializeObject <ButlerRequest>(currentMessage.AsString);
                List <StepHandler> mysteps = BuildChain(watcherRequest.WorkflowName);
                myRequest = GetCurrentContext(watcherRequest.WorkflowName);
                myRequest.CurrentMessage    = currentMessage;
                myRequest.ProcessTypeId     = watcherRequest.WorkflowName;
                myRequest.ProcessInstanceId = watcherRequest.MessageId.ToString();
                myRequest.ProcessConfigConn = this.myProcessConfigConn;
                myRequest.IsResumeable      = (this.ReadConfigOrDefault(myRequest.ProcessTypeId + ".IsResumeable") == "1");
                //2.Execute Chain

                //lock (myLock)
                //{
                //    currentProcessRunning += 1;
                //}
                txt = string.Format("[{0}] Starting new Process, type {1} and ID {2}", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId);
                Trace.TraceInformation(txt);
                mysteps.FirstOrDefault().HandleRequest(myRequest);
                //FinishProcess();
                txt = string.Format("[{0}] Finish Process, type {1} and ID {2}", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId);
                Trace.TraceInformation(txt);
                lock (myLock)
                {
                    currentProcessRunning -= 1;
                }
            }
            catch (Exception xxx)
            {
                if (myRequest != null)
                {
                    foreach (Exception item in myRequest.Exceptions)
                    {
                        //Full Rollback?
                        txt = string.Format("[{0}] Error list process {1} intance {2} error: {3}", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId, item.Message);
                        Trace.TraceError(txt);
                    }
                }
                else
                {
                    txt = string.Format("[{0}] Error {1} without context Request yet", this.GetType().FullName, xxx.Message);
                    Trace.TraceError(xxx.Message);
                }
                //Exception no Managed

                lock (myLock)
                {
                    currentProcessRunning -= 1;
                }
                // throw(X);
            }
            //3.return control
            if (myRequest != null)
            {
                myRequest.DisposeRequest();
            }
            else
            {
                Trace.TraceError("myRequest is null raw message " + currentMessage.AsString);
            }
            myRequest = null;

            Trace.Flush();
        }
        /// <summary>
        /// Send back to watcher a "Posion messsage" and delete from in queue
        /// </summary>
        /// <param name="poisonMessage">the poison message</param>
        /// <returns>Sueccess or not</returns>
        private bool SendPoisonMessage(CloudQueueMessage poisonMessage)
        {
            bool sw = false;

            try
            {
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_Configuration.ProcessConfigConn);
                CloudQueueClient    queueClient    = storageAccount.CreateCloudQueueClient();
                CloudQueue          poisonQueue    = queueClient.GetQueueReference(_Configuration.poisonQueue);
                poisonQueue.CreateIfNotExists();

                MediaButler.Common.ButlerRequest  myButlerRequest  = Newtonsoft.Json.JsonConvert.DeserializeObject <ButlerRequest>(poisonMessage.AsString);
                MediaButler.Common.ButlerResponse myButlerResponse = new Common.ButlerResponse();

                myButlerResponse.MezzanineFiles = myButlerRequest.MezzanineFiles;
                ////Add to Mezzamine Files the control File URL if it exist
                //Becouse it is needed to move/delete the control file from processing to succes or fail
                if (!string.IsNullOrEmpty(myButlerRequest.ControlFileUri))
                {
                    myButlerResponse.MezzanineFiles.Add(myButlerRequest.ControlFileUri);
                }
                myButlerResponse.TimeStampProcessingCompleted = DateTime.Now.ToString();
                myButlerResponse.TimeStampProcessingStarted   = DateTime.Now.ToString();
                myButlerResponse.WorkflowName = myButlerRequest.WorkflowName;
                myButlerResponse.MessageId    = myButlerRequest.MessageId;
                myButlerResponse.TimeStampRequestSubmitted = myButlerRequest.TimeStampUTC;
                myButlerResponse.StorageConnectionString   = myButlerRequest.StorageConnectionString;
                myButlerResponse.Log = "Poison Message";

                //Lookin for Errors in Table Status
                CloudTableClient tableClient       = storageAccount.CreateCloudTableClient();
                CloudTable       table             = tableClient.GetTableReference(MediaButler.Common.Configuration.ButlerWorkflowStatus);
                TableOperation   retrieveOperation =
                    TableOperation.Retrieve <MediaButler.Common.workflow.ProcessSnapShot>(myButlerRequest.WorkflowName, myButlerRequest.MessageId.ToString());
                TableResult retrievedResult = table.Execute(retrieveOperation);
                if (retrievedResult.Result != null)
                {
                    //we have process info
                    var status = (MediaButler.Common.workflow.ProcessSnapShot)retrievedResult.Result;
                    try
                    {
                        var request = Newtonsoft.Json.JsonConvert.DeserializeObject <MediaButler.Common.workflow.ChainRequest>(status.jsonContext);
                        foreach (var error in request.Exceptions)
                        {
                            myButlerResponse.Log += "\r\n" + error.Message;
                        }
                    }
                    catch (Exception X)
                    {
                        Trace.TraceWarning("Unable to load Error LOG in response.log on poison message");
                        myButlerResponse.Log += "\r\n" + X.Message;
                        myButlerResponse.Log += "\r\n" + status.jsonContext;
                    }


                    //Delete register from Status table
                    TableOperation insertOperation = TableOperation.Delete(status);
                    table.Execute(insertOperation);
                }
                //Send Poison Mesagge
                CloudQueueMessage poison = new CloudQueueMessage(Newtonsoft.Json.JsonConvert.SerializeObject(myButlerResponse));
                poisonQueue.AddMessage(poison);
                sw = true;
            }
            catch (Exception X)
            {
                string txt = string.Format("[{0}] at {1} has an error: {2}", this.GetType().FullName, "GetNewMessage", X.Message);
                Trace.TraceError(txt);
            }
            return(sw);
        }