Beispiel #1
0
        /// <summary>
        /// Marks the specified workflow as inactive and cancels all currently scheduled tasks for that
        /// </summary>
        /// <param name="id"></param>
        public async System.Threading.Tasks.Task <bool> Delete(string id)
        {
            var workflow = await _context.Workflows.Where(w => w.id == id).FirstOrDefaultAsync();

            if (workflow != null)
            {
                Log.Information("Cancelling Workflow: {0}, and associated Tasks.", id);
                var tasks = await _context.Tasks.Where(t => t.workflowID == id).ToListAsync();

                foreach (LimsServer.Entities.Task t in tasks)
                {
                    if (t.status == "SCHEDULED")
                    {
                        t.status  = "CANCELLED";
                        t.message = "Corresponding workflow cancelled.";
                        var newState = new Hangfire.States.DeletedState();
                        Log.Information("Task Cancelled. WorkflowID: {0}, ID: {1}, Hangfire ID: {2}", t.workflowID, t.id, t.taskID);

                        try
                        {
                            BackgroundJobClient backgroundClient = new BackgroundJobClient();
                            backgroundClient.ChangeState(t.taskID, newState);
                        }
                        catch (Exception)
                        {
                            Log.Warning("Error unable to change Hangfire background job to deleted state. Task ID: {0}", t.taskID);
                        }
                    }
                }
                Log.Information("Setting LIMS Workflow, ID: {0} to inactive", id);
                workflow.active = false;
                await _context.SaveChangesAsync();

                return(true);
            }
            else
            {
                Log.Information("Unable to cancel Workflow: {0}, ID not found.", id);
                return(false);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Updates the workflow provided by id
        /// </summary>
        /// <param name="workflow"></param>
        public async System.Threading.Tasks.Task <bool> Update(Workflow _workflow, bool bypass = false)
        {
            string id       = _workflow.id;
            var    workflow = await _context.Workflows.Where(w => w.id == id).FirstOrDefaultAsync();

            if (workflow != null)
            {
                workflow.Update(_workflow);
                await _context.SaveChangesAsync();

                Log.Information("Updating Workflow: {0}, and reschduling existing Tasks.", id);
                var tasks = await _context.Tasks.Where(t => t.workflowID == id).ToListAsync();

                bool taskRunning = false;
                foreach (LimsServer.Entities.Task t in tasks)
                {
                    if (t.status == "SCHEDULED" && workflow.active)
                    {
                        t.start = DateTime.Now.AddMinutes(workflow.interval);
                        await _context.SaveChangesAsync();

                        Log.Information("Task Rescheduled. WorkflowID: {0}, ID: {1}, Hangfire ID: {2}, Input Directory: {3}, Message: {4}", t.workflowID, t.id, t.taskID, workflow.inputFolder, "Workflow updated, task rescheduled to new workflow configuration.");
                        taskRunning = true;

                        try
                        {
                            var newSchedule = new Hangfire.States.ScheduledState(TimeSpan.FromMinutes(workflow.interval));
                            BackgroundJobClient backgroundClient = new BackgroundJobClient();
                            backgroundClient.ChangeState(t.taskID, newSchedule);
                        }
                        catch (Exception)
                        {
                            Log.Warning("Error rescheduling Hangfire background job. Job ID: {0}", t.taskID);
                        }
                    }
                    else
                    {
                        t.status  = "CANCELLED";
                        t.message = "Corresponding workflow updated.";
                        var newState = new Hangfire.States.DeletedState();
                        Log.Information("Task Cancelled. WorkflowID: {0}, ID: {1}, Hangfire ID: {2}", t.workflowID, t.id, t.taskID);

                        try
                        {
                            BackgroundJobClient backgroundClient = new BackgroundJobClient();
                            backgroundClient.ChangeState(t.taskID, newState);
                        }
                        catch (Exception)
                        {
                            Log.Warning("Error cancelling Hanfire background job. Job ID: {0}", t.taskID);
                        }
                    }
                }
                if (!taskRunning && workflow.active)
                {
                    string newId = System.Guid.NewGuid().ToString();
                    LimsServer.Entities.Task tsk = new Entities.Task(newId, workflow.id, workflow.interval);
                    TaskService ts   = new TaskService(this._context);
                    var         task = await ts.Create(tsk);

                    await _context.SaveChangesAsync();

                    Log.Information("Created new Task for updated Workflow ID: {0}, Updated Task ID: {1}, Hangfire ID: {2}", newId, tsk.id, tsk.taskID);
                }
                return(true);
            }
            else
            {
                Log.Information("Unable to cancel Workflow: {0}, ID not found.", id);
                return(false);
            }
        }