Exemplo n.º 1
0
        public void ScheduleJobOnlyOnce <T>(string uniqueName, T jobInfo, DateTime schedule, Recurrence r = null,
                                            string jobRoute = null) where T : class
        {
            Debug.Assert(null != jobInfo);

            bool exists;

            using (var ds = DocumentStoreLocator.ResolveOrRoot(CommonConfiguration.CoreDatabaseRoute))
            {
                exists = ds.Query <ScheduledItem>().Any(i => i.UniqueName == uniqueName);
            }

            if (!exists)
            {
                var log = ClassLogger.Create(typeof(JobDocumentsScheduler));


                log.InfoFormat("Scheduling job: {0} at time {1}, recurring: {2}, route = {3}", jobInfo, schedule,
                               r == null ? string.Empty : r.ToString(), jobRoute);

                using (var ds = DocumentStoreLocator.ResolveOrRoot(CommonConfiguration.CoreDatabaseRoute))
                {
                    ds.Store(new ScheduledItem(uniqueName, JsonConvert.SerializeObject(jobInfo, _settings),
                                               typeof(T), jobRoute, schedule, r));
                    ds.SaveChanges();
                }
            }
        }
Exemplo n.º 2
0
        public StochasticRecurringWorker()
        {
            _log   = ClassLogger.Create(GetType());
            _dblog = DebugOnlyLogger.Create(_log);

            var rand = GoodSeedRandom.Create();

            _baseTime = DateTime.UtcNow + TimeSpan.FromMinutes(rand.Next(_delay));
        }
Exemplo n.º 3
0
        protected SleepyWorkerEntryPoint()
        {
            MinThreadSleep     = 100;
            MaxThreadSleep     = 2200;
            CurrentThreadSleep = MinThreadSleep;
            _log   = ClassLogger.Create(typeof(SleepyWorkerEntryPoint));
            _dblog = DebugOnlyLogger.Create(_log);

            _log.InfoFormat("Creating new {0}", GetType());
        }
Exemplo n.º 4
0
        //the way a code is encrypted may be changed by class inheritors
        //but adding the _magic is unique using the | separator and _magic
        protected static string GenerateCode(string plainText)
        {
            var log   = ClassLogger.Create(typeof(AuthorizationCode));
            var dblog = DebugOnlyLogger.Create(log);

            var cryptText = PWCrypto.Encrypt(plainText, _pwd);

            dblog.InfoFormat(GeneratedCodeMsgFormat, plainText);

            return(cryptText);
        }
Exemplo n.º 5
0
 public Recurrence()
 {
     DaysOfMonth  = null;
     DaysOfWeek   = null;
     MonthsLater  = 0;
     DaysLater    = 0;
     HoursLater   = 0;
     MinutesLater = 0;
     AtFixedTime  = DateTime.MinValue;
     _log         = ClassLogger.Create(GetType());
     _dblog       = DebugOnlyLogger.Create(_log);
 }
Exemplo n.º 6
0
        public static T DecryptDeserialize(string t, string pass)
        {
            Contract.Requires(!string.IsNullOrEmpty(t));
            Contract.Requires(!string.IsNullOrWhiteSpace(pass));

            var log = ClassLogger.Create(typeof(EncryptedObject <T>));

            string p = PWCrypto.Decrypt(t, pass);

            log.InfoFormat("Decrypting object {0}", p);
            T token = JsonConvert.DeserializeObject <T>(p);

            return(token);
        }
Exemplo n.º 7
0
        public static string EncryptSerialize(T token, string pass)
        {
            Contract.Requires(null != token);
            Contract.Requires(!string.IsNullOrWhiteSpace(pass));

            var log = ClassLogger.Create(typeof(EncryptedObject <T>));


            string s = JsonConvert.SerializeObject(token);

            log.InfoFormat("Securing object {0}", s);
            string c = PWCrypto.Encrypt(s, pass);

            return(c);
        }
Exemplo n.º 8
0
        public InMemoryCachedData()
        {
            _log   = ClassLogger.Create(GetType());
            _dblog = DebugOnlyLogger.Create(_log);

            var config = Catalog.Factory.Resolve <IConfig>(SpecialFactoryContexts.Routed);

            ExpireItems       = config.Get(CachedDataLocalConfig.OptionalGroomExpiredData, false);
            RenewOnCacheHit   = config.Get(CachedDataLocalConfig.OptionalCacheHitRenewsExpiration, false);
            DisposeOfData     = config.Get(CachedDataLocalConfig.OptionalDisposeData, false);
            DefaultExpireTime =
                TimeSpan.FromSeconds(config.Get(CachedDataLocalConfig.OptionalDefaultExpirationTimeSeconds, 3600));
            MaximumCacheItemsCount = config.Get(CachedDataLocalConfig.OptionalMaximumCacheSize, 0);
            NotifyExpiredWithData  = config.Get(CachedDataLocalConfig.OptionalNotifyExpiredWithData, true);

            _nextGroomSchedule = DateTime.UtcNow + _groomScheduleTimeOut;
        }
Exemplo n.º 9
0
        public void ScheduleJob <T>(T jobInfo, DateTime schedule, Recurrence r = null, string jobRoute = "")
            where T : class
        {
            Debug.Assert(null != jobInfo);

            var log = ClassLogger.Create(typeof(JobDocumentsScheduler));

            log.InfoFormat("Scheduling job: {0} at time {1}, recurring: {2}, route = {3}", jobInfo, schedule,
                           r == null ? string.Empty : r.ToString(), jobRoute);


            log.InfoFormat("Scheduling job: {0} at time {1}, recurring: {2}, route = {3}", jobInfo, schedule,
                           r == null ? string.Empty : r.ToString(), jobRoute);

            using (var ds = DocumentStoreLocator.ResolveOrRoot(CommonConfiguration.CoreDatabaseRoute))
            {
                ds.Store(new ScheduledItem(_notUnique, JsonConvert.SerializeObject(jobInfo, _settings), typeof(T),
                                           jobRoute, schedule, r));
                ds.SaveChanges();
            }
        }
Exemplo n.º 10
0
        //validates an auth code
        public static bool IsGoodAuthCode(string authCode)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(authCode));

            var log   = ClassLogger.Create(typeof(AuthorizationCode));
            var dblog = DebugOnlyLogger.Create(log);

            try
            {
                var plain = DecryptCode(authCode);

                dblog.InfoFormat(DecryptedAuthCodeFormat, plain);

                var parts = plain.Split(Separator);
                if (parts.Length != 2)
                {
                    log.ErrorFormat(AuthCodeFailedFormat, plain);
                    return(false);
                }

                var goodGuid = GuidEncoder.Decode(parts[0]);
                if (Guid.Empty != goodGuid && parts[1].Contains(_magic))
                {
                    dblog.InfoFormat(AuthCodeAuthorizedFormat, plain);
                    return(true);
                }

                //when not in right format
                log.ErrorFormat(AuthCodeFailedFormat, plain);
                return(false);
            }
            catch (Exception exception)
            {
                log.Error("IsGoodAuthCode", exception);
                log.ErrorFormat(AuthCodeFailedFormat, authCode);
                return(false);
            }
        }
Exemplo n.º 11
0
        private void Email(object msg, CancellationToken ct, IMessageAcknowledge ack)
        {
            var log = ClassLogger.Create(typeof(SendEmailWorker));

            var command = (SendEmail)msg;

            Debug.Assert(!string.IsNullOrEmpty(command.Sender));
            Debug.Assert(!string.IsNullOrEmpty(command.Subject));
            Debug.Assert(null != command.Content);
            Debug.Assert(command.Recipients.EmptyIfNull().Any());

            if (!command.Recipients.Any())
            {
                return;
            }

            if (ct.IsCancellationRequested)
            {
                return;
            }

            var cred = new NetworkCredential {
                UserName = _account, Password = _password
            };
            var email = new SmtpClient
            {
                Credentials = cred, UseDefaultCredentials = true, EnableSsl = _useSSL, Port = _port, Host = _server
            };

            var message = new MailMessage
            {
                From       = new MailAddress(command.Sender),
                Subject    = command.Subject,
                Body       = command.Content,
                IsBodyHtml = command.HtmlFormat
            };

            foreach (var r in command.Recipients)
            {
                message.To.Add(new MailAddress(r));
            }

            if (!string.IsNullOrEmpty(_replyAddress))
            {
                message.ReplyToList.Add(_replyAddress);
            }

            var sent       = false;
            var retryCount = 0;

            while (!sent)
            {
                if (ct.IsCancellationRequested)
                {
                    return;
                }

                try
                {
                    if (ct.IsCancellationRequested)
                    {
                        return;
                    }

                    email.Send(message);
                    log.InfoFormat(
                        "Sent email about '{0}' to {1} recipients", command.Subject, command.Recipients.Count());
                    sent = true;
                }
                catch (SmtpFailedRecipientsException)
                {
                    sent = true;
                }
                catch (SmtpFailedRecipientException)
                {
                    sent = true;
                }
                catch (Exception ex)
                {
                    retryCount++;
                    if (retryCount > 10)
                    {
                        const string es = "Could not send email.";
                        log.Error(es);
                        log.TraceException(ex);
                        var on = Catalog.Factory.Resolve <IApplicationAlert>();
                        on.RaiseAlert(ApplicationAlertKind.Services, es, ex);
                        ack.MessageRejected();
                        break;
                    }

                    if (ct.IsCancellationRequested)
                    {
                        return;
                    }

                    ct.WaitHandle.WaitOne(3000);
                }
            }

            if (sent)
            {
                ack.MessageAcknowledged();
            }
        }