Esempio n. 1
0
 public Task Execute(IJobExecutionContext context)
 {
     return(Task.Run(() =>
     {
         using (var dbContext = new QuartzExecutionDataContext())
         {
             foreach (var job in dbContext.Jobs.ToList())
             {
                 if (!job.IsExecutingCorrectly)
                 {
                     LogsAppendersManager.Instance.Error("Il job " + job.Key + " del cliente " + job.Server.Name + " su ip " + job.Server.IP + " non sta funzionando correttamente");
                     try
                     {
                         WebSocketClient.SendMessage(new SocketMessage
                         {
                             Title = "Il job " + job.Key + " del cliente " + job.Server.Name + " su ip " + job.Server.IP + " non sta funzionando correttamente",
                             DataType = typeof(string),
                             IsImportant = true,
                             Data = ""
                         });
                     }
                     catch (Exception ex) { LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex); }
                 }
             }
         }
     }));
 }
Esempio n. 2
0
        public HttpResponseMessage CallsList([FromBody] ApiHttpRequest <DbFiltersCollection> request)
        {
            //var filtersString = HttpUtility.ParseQueryString(Request.RequestUri.Query)["filters"];
            //var filters = !string.IsNullOrEmpty(filtersString) ? filtersString.Deserialize<Dictionary<string, object>>() : new Dictionary<string, object>();
            var filters  = request.data;
            var response = new ApiHttpResponse <HttpDataList <Call> >();
            var username = Request.Username();
            var _context = new QuartzExecutionDataContext();

            try
            {
                var account = _context.Accounts
                              .FirstOrDefault(x => x.Username == username);
                var dbCalls = (from ser in _context.Servers
                               join job in _context.Jobs on ser.ServerID equals job.ServerID
                               join cal in _context.Calls on job.JobID equals cal.JobID
                               join accser in _context.AccountServers on ser.ServerID equals accser.ServerID
                               join acc in _context.Accounts on accser.AccountID equals acc.AccountID
                               where (acc.Username == username && account.Roles == RoleType.Client) ||
                               (account.Roles == RoleType.Admin || account.Roles == RoleType.Inspector)
                               select cal)
                              .Distinct()
                              .Filter(filters);
                response.data.result = dbCalls.ToList();
                response.Complete(ResultCode.OK, "");
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
        public HttpResponseMessage AccountsList([FromBody] ApiHttpRequest <Dictionary <string, object> > request)
        {
            var filters  = request.data;
            var _context = new QuartzExecutionDataContext();
            var response = new ApiHttpResponse <HttpDataList <Account> >();
            var username = Request.Username();

            try
            {
                //only admin can see accounts
                Action _canProceed = () =>
                {
                    var accounts = _context.Accounts.Filter(filters);
                    response.data.result = accounts.ToList();
                    response.Complete(ResultCode.OK, "");
                };
                Action _else = () => response.Complete(ResultCode.OK, "Only admin and inspector can see Accounts");;

                _context.Accounts
                .Where(x => x.Username == username)
                .SingleOrDefault()
                .ProceedIf(x => x.IsAdminOrIspector, _canProceed, _else);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
Esempio n. 4
0
        public HttpResponseMessage CallStart([FromUri] string jobKey, [FromBody] ApiHttpRequest <Call> request)
        {
            var response = new ApiHttpResponse <HttpDataSingle <bool> >();
            var call     = request.data;
            var _context = new QuartzExecutionDataContext();

            try
            {
                var appID    = Request.Username();
                var appToken = Request.Password();
                var dbServer = _context.Servers
                               .FirstOrDefault(x => x.AppId == appID && x.AppToken == appToken);

                if (dbServer != null)
                {
                    var dbJob = dbServer.Jobs.FirstOrDefault(x => x.Key == jobKey);
                    if (dbJob != null)
                    {
                        dbJob.Calls.Add(call);
                        dbJob.Executing = true;
                        _context.SaveChanges();
                        response.data.result = true;
                        response.Complete(ResultCode.OK, "");

                        try
                        {
                            WebSocketClient.SendMessage(new SocketMessage
                            {
                                Title       = "Job " + call.Job?.Key + " in esecuzione con instance ID : " + call.FireInstenceID,
                                DataType    = call.GetType(),
                                IsImportant = false,
                                Data        = call
                            });
                        }
                        catch (Exception ex) { LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex); }
                    }
                    else
                    {
                        throw new EntityNotExistsException("Job with key " + jobKey + " not exists");
                    }
                }
                else
                {
                    throw new EntityNotExistsException("Server with key " + Request.Username() + " not exists");
                }
            }
            catch (EntityNotExistsException ex)
            {
                LogsAppendersManager.Instance.Debug(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message);
                response.Warning(System.Net.HttpStatusCode.NotFound, ex.Message);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
Esempio n. 5
0
        public static bool IsJobExecutingCorrectly(long jobId)
        {
            var _context = new QuartzExecutionDataContext();
            var lastCall = (from job in _context.Jobs
                            join cal in _context.Calls on job.JobID equals cal.JobID
                            where job.JobID == jobId
                            orderby cal.EndedAt descending
                            select cal).FirstOrDefault();

            return(DateTime.Now <= lastCall.NextStart.AddMinutes(2));
        }
Esempio n. 6
0
        public HttpResponseMessage Add([FromUri] long accountID, [FromBody]  ApiHttpRequest <Server> request)
        {
            var response = new ApiHttpResponse <HttpDataSingle <Server> >();
            var server   = request.data;
            var username = Request.Username();

            var _context = new QuartzExecutionDataContext();

            try
            {
                /* only admin and inspector can add clients */
                Action _canProceed = () =>
                {
                    var account = _context.Accounts
                                  .SingleOrDefault(x => x.AccountID == accountID);

                    if (account != null)
                    {
                        if (_context.Servers.FirstOrDefault(x => x.Name == server.Name && x.IP == server.IP) == null)
                        {
                            /*_context.Servers.Add(server);*/
                            account.Servers.Add(server);
                            _context.SaveChanges();
                            response.data.result = _context.Servers.FirstOrDefault(x => x.Name == server.Name && x.IP == server.IP);
                            response.Complete(ResultCode.OK, "Server succefully added");
                        }
                        else
                        {
                            throw new EntityDuplicationException("Already exists server with name: " + server.Name + " and ip: " + server.IP);
                        }
                    }
                    else
                    {
                        throw new EntityNotExistsException("No account exists for id: " + accountID);
                    }
                };
                Action _else = () => response.Complete(ResultCode.OK, "Only admin and inspector can add servers");
                _context.Accounts
                .Where(x => x.Username == username)
                .SingleOrDefault()
                .ProceedIf(x => x.IsAdminOrIspector, _canProceed, _else);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
Esempio n. 7
0
        public HttpResponseMessage JobsList([FromBody] ApiHttpRequest <Dictionary <string, object> > request)
        {
            //var filtersString = HttpUtility.ParseQueryString(Request.RequestUri.Query)["filters"];
            //var filters = !string.IsNullOrEmpty(filtersString) ? filtersString.Deserialize<Dictionary<string, object>>() : new Dictionary<string, object>();
            var filters = request.data;

            var response = new ApiHttpResponse <HttpDataList <dynamic> >();
            var username = Request.Username();
            var _context = new QuartzExecutionDataContext();

            try
            {
                var account = _context.Accounts
                              .FirstOrDefault(x => x.Username == username);
                var jobs = (from ser in _context.Servers
                            join job in _context.Jobs on ser.ServerID equals job.ServerID
                            join accser in _context.AccountServers on ser.ServerID equals accser.ServerID
                            join acc in _context.Accounts on accser.AccountID equals acc.AccountID
                            where (acc.Username == username && account.Roles == RoleType.Client) ||
                            (account.Roles == RoleType.Admin || account.Roles == RoleType.Inspector)
                            select new {
                    JobID = job.JobID,
                    Key = job.Key,
                    Description = job.Description,
                    Executing = job.Executing,
                    ServerID = job.ServerID,
                    StartedAt = job.StartedAt,
                    IsExecutingCorrectly = DateTime.Now <= DbFunctions.AddMinutes(job.Calls.OrderByDescending(x => x.EndedAt).FirstOrDefault().NextStart, 1),
                    LastCall = job.Calls.OrderByDescending(x => x.EndedAt).FirstOrDefault()
                })
                           .Distinct()
                           .Filter(filters)
                           .OrderByDescending(x => x.LastCall.StartedAt);


                //var jobList = jobs?
                //    .OrderByDescending(x => x.LastCall.StartedAt);
                //jobList.ForEach(x => x.Calls.Take(1));
                response.data.result = jobs.ToList <dynamic>();
                response.Complete(ResultCode.OK, "");
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
Esempio n. 8
0
        public HttpResponseMessage UpdateJob([FromBody] ApiHttpRequest <Job> request)
        {
            var response = new ApiHttpResponse <HttpDataSingle <Job> >();
            var job      = request.data;
            var username = Request.Username();
            var _context = new QuartzExecutionDataContext();

            try
            {
                var appID    = Request.Username();
                var appToken = Request.Password();
                var dbServer = _context.Servers
                               .FirstOrDefault(x => x.AppId == appID && x.AppToken == appToken);

                if (dbServer != null)
                {
                    var dbJob = dbServer.Jobs.FirstOrDefault(x => x.Key == job.Key);
                    if (dbJob != null)
                    {
                        dbJob.StartedAt = job.StartedAt;
                        dbJob.Executing = job.Executing;
                        _context.SaveChanges();
                        response.Complete(ResultCode.OK, "");
                    }
                    else
                    {
                        throw new EntityNotExistsException(job.Key);
                    }
                    response.data.result = dbServer.Jobs.FirstOrDefault(x => x.Key == job.Key);
                }
                else
                {
                    throw new EntityNotExistsException("Client with key " + Request.Username() + " not exists");
                }
            }
            catch (EntityNotExistsException ex)
            {
                LogsAppendersManager.Instance.Debug(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message);
                response.Warning(System.Net.HttpStatusCode.NotFound, ex.Message);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
Esempio n. 9
0
        public HttpResponseMessage Delete([FromBody] ApiHttpRequest <Server> request)
        {
            var response = new ApiHttpResponse <HttpDataSingle <bool> >();
            var server   = request.data;
            var username = Request.Username();

            var _context = new QuartzExecutionDataContext();

            try
            {
                /* only admin and inspector can delete clients */
                Action _canProceed = () =>
                {
                    var dbClient = _context.Servers.FirstOrDefault(x => x.Name == server.Name && x.IP == server.IP);
                    if (dbClient != null)
                    {
                        _context.Servers.Remove(dbClient);
                        _context.SaveChanges();
                        response.data.result = true;
                        response.Complete(ResultCode.OK, "Client succefully Deleted");
                    }
                    else
                    {
                        throw new EntityNotExistsException("Not exists Client with Name " + server.Name + " and ip " + server.IP);
                    }
                };
                Action _else = () => response.Complete(ResultCode.OK, "Only admin and inspector can delete clients");
                _context.Accounts
                .Where(x => x.Username == username)
                .SingleOrDefault()
                .ProceedIf(x => x.IsAdminOrIspector, _canProceed, _else);
            }
            catch (EntityNotExistsException ex)
            {
                LogsAppendersManager.Instance.Debug(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message);
                response.Warning(System.Net.HttpStatusCode.NotFound, ex.Message);
            }
            catch (Exception ex1)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex1.Message, ex1);
                response.Fault(ex1);
            }

            return(Request.CreateResponse(response.Code, response));
        }
Esempio n. 10
0
        public HttpResponseMessage GenerateAppToken([FromUri] long serverID)
        {
            var response = new ApiHttpResponse <HttpDataSingle <string> >();
            var username = Request.Username();
            var _context = new QuartzExecutionDataContext();

            try
            {
                /* only admin and inspector can generate app token */
                Action _canProceed = () => {
                    var appIdTemplate = "{0}-{1}-{2}-{3}";
                    var dbClient      = _context.Servers.FirstOrDefault(x => x.ServerID == serverID);
                    if (dbClient == null)
                    {
                        throw new EntityNotExistsException("Not exists Client with id: " + serverID);
                    }
                    var appToken       = string.Format(appIdTemplate, dbClient.Name, dbClient.IP, dbClient.AppId, Guid.NewGuid()).Replace(":", "");
                    var appTokenBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(appToken));
                    dbClient.AppToken = appTokenBase64;
                    _context.SaveChanges();
                    response.data.result = appTokenBase64;
                    response.Complete(ResultCode.OK, "Application Token Generated");
                };

                Action _else = () => response.Complete(ResultCode.OK, "Only admin and inspector can generate app token");

                _context.Accounts
                .Where(x => x.Username == username)
                .SingleOrDefault()
                .ProceedIf(x => x.IsAdminOrIspector, _canProceed, _else);
            }
            catch (EntityNotExistsException ex)
            {
                LogsAppendersManager.Instance.Debug(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message);
                response.Warning(System.Net.HttpStatusCode.NotFound, ex.Message);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
Esempio n. 11
0
        public HttpResponseMessage Update([FromUri] long serverID, [FromBody] ApiHttpRequest <Server> request)
        {
            var response = new ApiHttpResponse <HttpDataSingle <Server> >();
            var server   = request.data;
            var username = Request.Username();

            var _context = new QuartzExecutionDataContext();

            try
            {
                /* only admin and inspector can update clients */
                Action _canProceed = () =>
                {
                    var dbClient = _context.Servers.FirstOrDefault(x => x.ServerID == serverID);
                    if (dbClient != null)
                    {
                        dbClient.Name     = server.Name;
                        dbClient.IP       = server.IP;
                        dbClient.AppId    = string.Empty;
                        dbClient.AppToken = string.Empty;
                        _context.SaveChanges();
                        response.data.result = dbClient;
                        response.Complete(ResultCode.OK, "Server succefully Updated");
                    }
                    else
                    {
                        throw new EntityNotExistsException("Not exists server with Name " + server.Name + " and ip " + server.IP);
                    }
                };
                Action _else = () => response.Complete(ResultCode.OK, "Only admin and inspector update add clients");
                _context.Accounts
                .Where(x => x.Username == username)
                .SingleOrDefault()
                .ProceedIf(x => x.IsAdminOrIspector, _canProceed, _else);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
        public HttpResponseMessage Exists([FromBody] Account account)
        {
            var response = new ApiHttpResponse <HttpDataSingle <Account> >();
            var _context = new QuartzExecutionDataContext();

            try
            {
                var dbAccount = _context.Accounts
                                .Where(x => x.Username == account.Username && x.Password == account.Password)
                                .SingleOrDefault();
                response.data.result = dbAccount;
                response.Complete(ResultCode.OK, string.Empty);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
Esempio n. 13
0
        public HttpResponseMessage Add([FromBody] ApiHttpRequest <Account> request)
        {
            var response = new ApiHttpResponse <HttpDataSingle <bool> >();
            var username = Request.Username();
            var account  = request.data;
            var _context = new QuartzExecutionDataContext();

            try
            {
                Action _canProceed = () =>
                {
                    var dbAccount = _context.Accounts.Where(x => x.Username == account.Username).SingleOrDefault();
                    if (dbAccount == null)
                    {
                        _context.Accounts.Add(account);
                        _context.SaveChanges();
                    }
                    else
                    {
                        throw new EntityDuplicationException("Account alredy exists with username " + account.Username);
                    }
                };
                Action _else = () => response.Complete(ResultCode.OK, "Only admin and inspector can see Accounts");

                _context.Accounts.
                Where(x => x.Username == username)
                .SingleOrDefault()
                .ProceedIf(x => x.IsAdminOrIspector, _canProceed, _else);
            }
            catch (EntityDuplicationException ex)
            {
                LogsAppendersManager.Instance.Debug(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message);
                response.Complete(ResultCode.OK, ex.Message);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
Esempio n. 14
0
        public HttpResponseMessage Create([FromUri] string username, [FromUri] string password, [FromUri] string serverName)
        {
            var response = new ApiHttpResponse <HttpDataSingle <Dictionary <string, string> > >();
            var _context = new QuartzExecutionDataContext();

            try
            {
                //Authentication Check
                var account = _context.Accounts
                              .Where(x => x.Username == username && x.Password == password).SingleOrDefault();
                if (account != null)
                {
                    var _server = _context.Servers
                                  .Where(x => x.Name == serverName)
                                  .SingleOrDefault();

                    if (_server == null)
                    {
                        var server = new Server
                        {
                            Name = serverName,
                            IP   = HttpContext.Current.Request.UserHostAddress
                        };
                        _context.Servers.Add(server);
                        _context.AccountServers.Add(new AccountServer {
                            AccountID = account.AccountID, ServerID = server.ServerID
                        });
                        _context.SaveChanges();


                        var appIdTemplate    = "{0}-{1}-{2}";
                        var appTokenTemplate = "{0}-{1}-{2}-{3}";
                        if (server == null)
                        {
                            throw new EntityNotExistsException("Not found server with name: " + serverName);
                        }

                        var appId          = string.Format(appIdTemplate, server.Name, server.IP, Guid.NewGuid()).Replace(":", "");
                        var appToken       = string.Format(appTokenTemplate, server.Name, server.IP, appId, Guid.NewGuid()).Replace(":", "");
                        var appTokenBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(appToken));

                        server.AppId    = appId;
                        server.AppToken = appTokenBase64;
                        _context.SaveChanges();


                        response.data.result = new Dictionary <string, string>();
                        response.data.result.Add("AppID", server.AppId);
                        response.data.result.Add("AppToken", server.AppToken);
                        response.Complete(ResultCode.OK, "Server Created");

                        try
                        {
                            WebSocketClient.SendMessage(new SocketMessage
                            {
                                Title       = "Nuovo Server " + server.Name + " con ip " + server.IP + " creato da " + username,
                                DataType    = server.GetType(),
                                IsImportant = false,
                                Data        = server
                            });
                        }
                        catch (Exception ex) { LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex); }
                    }
                    else
                    {
                        var server = _context.Servers.FirstOrDefault(x => x.Name == serverName);
                        response.data.result = new Dictionary <string, string>();
                        response.data.result.Add("AppID", server.AppId);
                        response.data.result.Add("AppToken", server.AppToken);

                        throw new EntityDuplicationException("Already exists server with name: " + serverName);
                    }
                }
                else
                {
                    response.NotAuthorized("Bad credentials for: " + username);
                }
            }
            catch (EntityNotExistsException ex)
            {
                LogsAppendersManager.Instance.Debug(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message);
                response.Warning(System.Net.HttpStatusCode.NotFound, ex.Message);
            }
            catch (EntityDuplicationException ex)
            {
                LogsAppendersManager.Instance.Debug(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message);
                response.Complete(ResultCode.KO, ex.Message);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
Esempio n. 15
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            CastleWindsorService.Init();
            var jobsInspector = new Jobs.JobsInspector()
            {
                Name        = "Inspector",
                Group       = "InspectorGroup",
                Description = "Jobs che si occupa di controllare se i job dai clienti stanno eseguendo correttamente",
                Triggers    = new List <TasksEverywhere.Quartz.Context.Jobs.Abstract.ICustomTrigger>
                {
                    new Quartz.Context.Jobs.Concrete.CustomTrigger
                    {
                        Name         = "InspectorTrigger",
                        Group        = "InspectorTriggerGroup",
                        Interval     = 1,
                        IntervalUnit = IntervalUnit.Minute,
                        Life         = 23,
                        LifeUnit     = IntervalUnit.Hour,
                        Period       = TasksEverywhere.Utilities.Enums.PeriodType.Settimanale,
                        StartDate    = DateTime.Parse("12/01/2019 00:00:00"),
                        WeekDays     = new List <DayOfWeek>
                        {
                            DayOfWeek.Monday,
                            DayOfWeek.Tuesday,
                            DayOfWeek.Wednesday,
                            DayOfWeek.Thursday,
                            DayOfWeek.Friday,
                            DayOfWeek.Saturday
                        },
                    }
                }
            };
            var quartzService = CastleWindsorService.Resolve <Quartz.Services.QuartzService>();

            quartzService.Init();
            quartzService.AddJob(jobsInspector);
            quartzService.Start();

            using (var _context = new QuartzExecutionDataContext())
            {
                var account = new Account
                {
                    Username = "******",
                    Password = "******",
                    Roles    = RoleType.AdminAndInspector
                };
                if (_context.Accounts
                    .Where(x => x.Username == account.Username)
                    .FirstOrDefault() == null)
                {
                    _context.Accounts.Add(account);
                }
                _context.SaveChanges();
            }
            try
            {
                WebSocketClient.Init(ConfigurationManager.AppSettings["WebSocketUrl"]);
            }
            catch (Exception e)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), e.Message, e);
            }
        }
Esempio n. 16
0
        public HttpResponseMessage AddJob([FromBody] ApiHttpRequest <Job> request)
        {
            var response = new ApiHttpResponse <HttpDataSingle <long> >();
            var job      = request.data;
            var _context = new QuartzExecutionDataContext();

            try
            {
                var appID    = Request.Username();
                var appToken = Request.Password();
                var dbServer = _context.Servers
                               .FirstOrDefault(x => x.AppId == appID && x.AppToken == appToken);

                if (dbServer != null)
                {
                    if (dbServer.Jobs.FirstOrDefault(x => x.Key.ToLower() == job.Key.ToLower()) == null)
                    {
                        job.Server   = dbServer;
                        job.ServerID = dbServer.ServerID;
                        _context.Jobs.Add(job);
                        _context.SaveChanges();
                        response.Complete(ResultCode.OK, "");

                        try
                        {
                            WebSocketClient.SendMessage(new SocketMessage
                            {
                                Title       = "Nuovo Job " + job.Key + " per server " + job.Server?.Name,
                                DataType    = job.GetType(),
                                IsImportant = false,
                                Data        = job
                            });
                        }
                        catch (Exception ex) { LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex); }
                    }
                    else
                    {
                        throw new EntityDuplicationException("Job alredy exists with key " + job.Key);
                    }
                    response.data.result = dbServer.Jobs.FirstOrDefault(x => x.Key == job.Key).JobID;
                }
                else
                {
                    throw new EntityNotExistsException("Client with key " + Request.Username() + " not exists");
                }
            }
            catch (EntityDuplicationException ex)
            {
                LogsAppendersManager.Instance.Debug(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message);
                response.Complete(ResultCode.OK, ex.Message);
            }
            catch (EntityNotExistsException ex)
            {
                LogsAppendersManager.Instance.Debug(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message);
                response.Warning(System.Net.HttpStatusCode.NotFound, ex.Message);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }
Esempio n. 17
0
        public HttpResponseMessage CallEnd([FromUri] string jobKey, [FromBody] ApiHttpRequest <Call> request)
        {
            var response = new ApiHttpResponse <HttpDataSingle <bool> >();
            var call     = request.data;
            var _context = new QuartzExecutionDataContext();

            try
            {
                var appID    = Request.Username();
                var appToken = Request.Password();
                var dbServer = _context.Servers
                               .FirstOrDefault(x => x.AppId == appID && x.AppToken == appToken);

                if (dbServer != null)
                {
                    var dbCall = (from job in dbServer.Jobs
                                  join cal in _context.Calls on job.JobID equals cal.JobID
                                  where job.Key == jobKey &&
                                  cal.FireInstenceID == call.FireInstenceID
                                  select cal
                                  ).FirstOrDefault();
                    if (dbCall != null)
                    {
                        dbCall.EndedAt       = call.EndedAt;
                        dbCall.Error         = call.Error;
                        dbCall.Job.Executing = false;
                        _context.SaveChanges();
                        response.data.result = true;
                        response.Complete(ResultCode.OK, "");
                        try
                        {
                            WebSocketClient.SendMessage(new SocketMessage
                            {
                                Title       = "Job " + dbCall.Job?.Key + " terminato con instance ID : " + call.FireInstenceID,
                                DataType    = call.GetType(),
                                IsImportant = call.Error != null,
                                Data        = call
                            });
                        }
                        catch (Exception ex) { LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex); }
                    }
                    else
                    {
                        throw new EntityNotExistsException("Call with fire instance id " + call.FireInstenceID + " not exists");
                    }
                }
                else
                {
                    throw new EntityNotExistsException("Client with key " + Request.Username() + " not exists");
                }
            }
            catch (EntityNotExistsException ex)
            {
                LogsAppendersManager.Instance.Debug(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message);
                response.Warning(System.Net.HttpStatusCode.NotFound, ex.Message);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), ex.Message, ex);
                response.Fault(ex);
            }
            return(Request.CreateResponse(response.Code, response));
        }