public void Error(IJobExecutionContext jobContext, Exception ex)
 {
     try
     {
         var _context = CastleWindsorService.Resolve <QuartzExecutionContext>();
         var job      = _context.data.Jobs.FirstOrDefault(x => x.Key == jobContext.JobDetail.Key.Name);
         if (job != null)
         {
             var err = new Error
             {
                 Message    = ex.Message,
                 StackTrace = ex.StackTrace
             };
             if (job.Calls.FirstOrDefault(x => x.FireInstenceID == jobContext.FireInstanceId) != null)
             {
                 job.Calls.FirstOrDefault(x => x.FireInstenceID == jobContext.FireInstanceId).Error = err;
             }
             job.Executing = false;
         }
         _context.Commit();
         CastleWindsorService.Release(_context);
     }
     catch (Exception ex1)
     {
         LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), "", ex1);
     }
 }
        public void CallStart(IJobExecutionContext jobContext)
        {
            try
            {
                var _context = CastleWindsorService.Resolve <QuartzExecutionContext>();
                var call     = new Call
                {
                    FireInstenceID = jobContext.FireInstanceId,
                    StartedAt      = jobContext.FireTimeUtc.DateTime,
                    NextStart      = jobContext.NextFireTimeUtc.HasValue ? jobContext.NextFireTimeUtc.Value.DateTime : DateTime.Parse("01/01/1753"),
                    EndedAt        = DateTime.Parse("01/01/1753")
                };

                var job = _context.data.Jobs.FirstOrDefault(x => x.Key == jobContext.JobDetail.Key.Name);
                if (job == null)
                {
                    job = new Job {
                        Key = jobContext.JobDetail.Key.Name, Calls = new List <Call>()
                    };
                    _context.data.Jobs.Add(job);
                }

                job.Calls.Add(call);
                job.Executing = true;
                job.StartedAt = jobContext.FireTimeUtc.DateTime;
                _context.Commit();
                CastleWindsorService.Release(_context);
            }
            catch (Exception ex)
            {
                LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), "", ex);
            }
        }
 public void CallEnd(IJobExecutionContext jobContext)
 {
     try
     {
         var _context = CastleWindsorService.Resolve <QuartzExecutionContext>();
         var job      = _context.data.Jobs.FirstOrDefault(x => x.Key == jobContext.JobDetail.Key.Name);
         if (job != null)
         {
             if (job.Calls.FirstOrDefault(x => x.FireInstenceID == jobContext.FireInstanceId) != null)
             {
                 job.Calls.FirstOrDefault(x => x.FireInstenceID == jobContext.FireInstanceId).EndedAt = DateTime.Now;
             }
             job.Executing = false;
         }
         _context.Commit();
         CastleWindsorService.Release(_context);
     }
     catch (Exception ex)
     {
         LogsAppendersManager.Instance.Error(this.GetType(), MethodBase.GetCurrentMethod(), "", ex);
     }
 }
        public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            //Check if ip is in configured list, unauthorize if is not
            if (AuthorizedIps != null && AuthorizedIps.Count > 0 && AuthorizedIps.FirstOrDefault(x => actionContext.Request.RequestUri.AbsoluteUri.Contains(x)) == null)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            //check if has autorization header, unathorize if has not
            if (actionContext.Request.Headers.Authorization != null)
            {
                // Gets header parameters
                string authenticationString = actionContext.Request.Headers.Authorization.Parameter;
                string originalString       = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationString));

                // Gets username and password
                var username = actionContext.Request.Username();
                var password = actionContext.Request.Password();
                //var appId = actionContext.Request.AppID();
                //var appToken = actionContext.Request.AppToken();
                string ip           = HttpContext.Current.Request.UserHostAddress;
                bool   isAuthorized = false;

                //Check if is authorized via DB
                if (dbAuth)
                {
                    //DbContext initilization
                    var _context = CastleWindsorService.Resolve <QuartzExecutionDataService>();

                    //Validate username password and appid apptoken or admin
                    isAuthorized = _context.data.Accounts
                                   .Where(x => x.Username == username && x.Password == password)
                                   .SingleOrDefault() != null;

                    if (!isAuthorized)
                    {
                        isAuthorized = _context.data.Servers
                                       .Where(x => x.AppId == username && x.AppToken == password)
                                       .SingleOrDefault() != null;
                    }

                    CastleWindsorService.Release(_context);
                }
                if (!isAuthorized)
                {
                    var configUsername = ConfigurationManager.AppSettings["AuthorizedUsername"];
                    var configPassword = ConfigurationManager.AppSettings["AuthorizedPassword"];

                    if (!username.ToLower().Equals(configUsername) && !password.Equals(configPassword))
                    {
                        // returns unauthorized error
                        isAuthorized = false;
                    }
                    else
                    {
                        isAuthorized = true;
                    }
                }
                if (!isAuthorized)
                {
                    LogsAppendersManager.Instance.Debug(this.GetType(), MethodBase.GetCurrentMethod(), "Not Authotirzed username: " + username);
                    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
                }
            }
            else
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            base.OnAuthorization(actionContext);
        }