Ejemplo n.º 1
0
        public IActionResult AddDataToBotStore([FromBody] BotStoreModel storeData)
        {
            using (var context = new tasktDbContext())
            {
                if (!context.Workers.Any(f => f.WorkerID == storeData.LastUpdatedBy))
                {
                    return(Unauthorized());
                }

                if (context.BotStore.Any(f => f.BotStoreName == storeData.BotStoreName))
                {
                    var existingItem = context.BotStore.Where(f => f.BotStoreName == storeData.BotStoreName).FirstOrDefault();
                    existingItem.BotStoreValue = storeData.BotStoreValue;
                    existingItem.LastUpdatedOn = DateTime.Now;
                    existingItem.LastUpdatedBy = storeData.LastUpdatedBy;
                }
                else
                {
                    storeData.LastUpdatedOn = DateTime.Now;
                    context.BotStore.Add(storeData);
                }

                context.SaveChanges();
                return(Ok(storeData));
            }
        }
Ejemplo n.º 2
0
        public IActionResult NewTask(Guid workerID, string taskName, string userName, string machineName)
        {
            //Todo: Add Auth Check, Change to HTTPPost and validate workerID is valid


            using (var context = new tasktDbContext())
            {
                var workerExists = context.Workers.Where(f => f.WorkerID == workerID).Count() > 0;

                if (!workerExists)
                {
                    //create new worker
                    var newWorker = new Worker();
                    newWorker.WorkerID    = workerID;
                    newWorker.UserName    = userName;
                    newWorker.MachineName = machineName;
                    newWorker.LastCheckIn = DateTime.Now;
                    newWorker.Status      = WorkerStatus.AutoPending;
                    context.Workers.Add(newWorker);
                    context.SaveChanges();

                    //return Unauthorized();
                }



                //close out any stale tasks
                var staleTasks = context.Tasks.Where(f => f.WorkerID == workerID && f.Status == "Running");
                foreach (var task in staleTasks)
                {
                    task.Status = "Closed";
                }

                var newTask = new Task();
                newTask.WorkerID      = workerID;
                newTask.UserName      = userName;
                newTask.MachineName   = machineName;
                newTask.TaskStarted   = DateTime.Now;
                newTask.Status        = "Running";
                newTask.ExecutionType = "Local";
                newTask.Script        = taskName;

                var entry = context.Tasks.Add(newTask);
                context.SaveChanges();
                return(Ok(newTask));
            }
        }
Ejemplo n.º 3
0
 public IActionResult AddAssignment([FromBody] Assignment assignment)
 {
     using (var context = new tasktDbContext())
     {
         context.Assignments.Add(assignment);
         context.SaveChanges();
         return(Ok(assignment));
     }
 }
Ejemplo n.º 4
0
 public IActionResult PublishScript([FromBody] PublishedScript script)
 {
     using (var context = new tasktDbContext())
     {
         if (script.OverwriteExisting)
         {
             var currentItem = context.PublishedScripts.Where(f => f.WorkerID == script.WorkerID && f.FriendlyName == script.FriendlyName).OrderByDescending(f => f.PublishedOn).FirstOrDefault();
             currentItem.PublishedOn = DateTime.Now;
             currentItem.ScriptData  = script.ScriptData;
             context.SaveChanges();
             return(Ok("The script has been updated on the server."));
         }
         else
         {
             script.PublishedOn = DateTime.Now;
             context.PublishedScripts.Add(script);
             context.SaveChanges();
             return(Ok("The script has been successfully published."));
         }
     }
 }
Ejemplo n.º 5
0
        public IActionResult ScheduleTask([FromBody] NewTaskRequest request)
        {
            //Todo: Add Auth Check, Change to HTTPPost and validate workerID is valid
            if (request is null)
            {
                return(BadRequest());
            }


            using (var context = new tasktDbContext())
            {
                var publishedScript = context.PublishedScripts.Where(f => f.PublishedScriptID == request.publishedScriptID).FirstOrDefault();

                if (publishedScript == null)
                {
                    return(BadRequest());
                }


                //find worker
                var workerRecord = context.Workers.Where(f => f.WorkerID == request.workerID).FirstOrDefault();

                //if worker wasnt found then search for pool

                if (workerRecord == null)
                {
                    //find from pool
                    var poolExists = context.WorkerPools.Any(s => s.WorkerPoolID == request.workerID);

                    //if pool wasnt found
                    if (!poolExists)
                    {
                        //return bad request
                        return(BadRequest());
                    }
                }


                //create new task
                var newTask = new Task();
                newTask.WorkerID      = request.workerID;
                newTask.TaskStarted   = DateTime.Now;
                newTask.Status        = "Scheduled";
                newTask.ExecutionType = "Remote";
                newTask.Script        = publishedScript.PublishedScriptID.ToString();
                newTask.Remark        = "Scheduled by tasktServer";

                var entry = context.Tasks.Add(newTask);
                context.SaveChanges();
                return(Ok(newTask));
            }
        }
Ejemplo n.º 6
0
 public IActionResult AddNewWorker(string userName, string machineName)
 {
     using (var context = new tasktDbContext())
     {
         var newWorker = new Worker();
         newWorker.UserName    = userName;
         newWorker.MachineName = machineName;
         newWorker.LastCheckIn = DateTime.Now;
         newWorker.Status      = WorkerStatus.Pending;
         context.Workers.Add(newWorker);
         context.SaveChanges();
         return(Ok(newWorker));
     }
 }
Ejemplo n.º 7
0
        public IActionResult UpdateTask(Guid taskID, string status, Guid workerID, string userName, string machineName, string remark)
        {
            //Todo: Needs Testing
            using (var context = new tasktDbContext())
            {
                var taskToUpdate = context.Tasks.Where(f => f.TaskID == taskID).FirstOrDefault();

                if (status == "Completed")
                {
                    taskToUpdate.TaskFinished = DateTime.Now;
                    taskToUpdate.TotalSeconds = (taskToUpdate.TaskFinished - taskToUpdate.TaskStarted).TotalSeconds;
                }

                taskToUpdate.UserName    = userName;
                taskToUpdate.MachineName = machineName;
                taskToUpdate.Remark      = remark;
                taskToUpdate.Status      = status;
                context.SaveChanges();
                return(Ok(taskToUpdate));
            }
        }
Ejemplo n.º 8
0
        public IActionResult CheckInWorker(Guid workerID, bool engineBusy)
        {
            using (var context = new tasktDbContext())
            {
                var targetWorker = context.Workers.Where(f => f.WorkerID == workerID).FirstOrDefault();

                if (targetWorker is null)
                {
                    return(BadRequest());
                }
                else
                {
                    targetWorker.LastCheckIn = DateTime.Now;
                    Task            scheduledTask   = null;
                    PublishedScript publishedScript = null;


                    if (!engineBusy)
                    {
                        scheduledTask = context.Tasks.Where(f => f.WorkerID == workerID && f.Status == "Scheduled").FirstOrDefault();


                        if (scheduledTask != null)
                        {
                            //worker directly scheduled

                            publishedScript = context.PublishedScripts.Where(f => f.PublishedScriptID.ToString() == scheduledTask.Script).FirstOrDefault();

                            if (publishedScript != null)
                            {
                                scheduledTask.Status = "Deployed";
                            }
                            else
                            {
                                scheduledTask.Status = "Deployment Failed";
                            }
                        }
                        else
                        {
                            //check if any pool tasks

                            var workerPools = context.WorkerPools
                                              .Include(f => f.PoolWorkers)
                                              .Where(f => f.PoolWorkers.Any(s => s.WorkerID == workerID)).ToList();

                            foreach (var pool in workerPools)
                            {
                                scheduledTask = context.Tasks.Where(f => f.WorkerID == pool.WorkerPoolID && f.Status == "Scheduled").FirstOrDefault();

                                if (scheduledTask != null)
                                {
                                    //worker directly scheduled

                                    publishedScript = context.PublishedScripts.Where(f => f.PublishedScriptID.ToString() == scheduledTask.Script).FirstOrDefault();

                                    if (publishedScript != null)
                                    {
                                        scheduledTask.Status = "Deployed";
                                    }
                                    else
                                    {
                                        scheduledTask.Status = "Deployment Failed";
                                    }

                                    break;
                                }
                            }
                        }
                    }

                    context.SaveChanges();


                    return(Ok(new CheckInResponse
                    {
                        Worker = targetWorker,
                        ScheduledTask = scheduledTask,
                        PublishedScript = publishedScript
                    }));
                }
            }
        }
Ejemplo n.º 9
0
        private void DoWork(object state)
        {
            tasktDbContext dbContext   = new tasktDbContext();
            var            assignments = dbContext.Assignments.Where(f => f.Enabled);

            //look through each assignment to see if anything is required to be executed
            foreach (var assn in assignments)
            {
                if (assn.NewTaskDue <= DateTime.Now)
                {
                    //create task
                    var newTask = new Shared.DbModels.Task();
                    newTask.WorkerID      = assn.AssignedWorker;
                    newTask.TaskStarted   = DateTime.Now;
                    newTask.Status        = "Scheduled";
                    newTask.ExecutionType = "Assignment";
                    newTask.Script        = assn.PublishedScriptID.ToString();
                    newTask.Remark        = "Scheduled by tasktServer";

                    dbContext.Tasks.Add(newTask);

                    //update database
                    switch (assn.Interval)
                    {
                    case Assignment.TimeInterval.Seconds:
                        assn.NewTaskDue = DateTime.Now.AddSeconds(assn.Frequency);
                        break;

                    case Assignment.TimeInterval.Minutes:
                        assn.NewTaskDue = DateTime.Now.AddMinutes(assn.Frequency);
                        break;

                    case Assignment.TimeInterval.Days:
                        assn.NewTaskDue = DateTime.Now.AddDays(assn.Frequency);
                        break;

                    case Assignment.TimeInterval.Months:
                        assn.NewTaskDue = DateTime.Now.AddMonths(assn.Frequency);
                        break;

                    default:
                        break;
                    }
                }
            }

            //look through stale tasks to expire after 10 minutes
            var staleTasks = dbContext.Tasks.Where(f => f.ExecutionType == "Assignment" && f.TaskStarted <= DateTime.Now.AddMinutes(-10) && f.Status == "Scheduled");

            foreach (var task in staleTasks)
            {
                task.Status       = "Closed";
                task.Remark       = "Closed by service due to unavailable client";
                task.TaskFinished = DateTime.Now;
            }



            dbContext.SaveChanges();


            _logger.LogInformation("Timed Background Service is working.");
        }