Exemplo n.º 1
0
        public DbJob(string name, string description, int interval, string parameters)
            : base(name, description)
        {
            Interval = interval;

            foreach (var parameter in parameters.SplitByChar(';'))
            {
                var kv = parameter.SplitByChar(':');
                if (kv.Length == 3)
                {
                    Parameters.Add(new DbParameter()
                    {
                        Name  = kv[0],
                        Type  = kv[1],
                        Value = kv[2],
                    });
                }
            }

            Implement = (t) =>
            {
                while (true)
                {
                    var startTime = DateTime.Now;

                    ThreadBridging.Retry(3, () =>
                    {
                        try
                        {
                            AppDomainContainer.Instance.Resolve <IJob>(Name).Execute();
                        }
                        catch (Exception e)
                        {
                            LoggerManager.GetLogger().LogEvent("DbJob", LoggerLevel.Error, string.Format("job {0} failed to execute.", Name), e);
                            return(false);
                        }

                        return(true);
                    });

                    LoggerManager.GetLogger().LogEvent("DbJob", LoggerLevel.Info, string.Format("job {0} finish to execute. cost: {1}", Name, (DateTime.Now - startTime).TotalSeconds));

                    if (CheckTransitionalStatus() == TaskObjectStatus.Sleep)
                    {
                        break;
                    }

                    ThreadBridging.Sleep(interval);
                }

                return(true);
            };
        }
Exemplo n.º 2
0
        private void TryChangeStatus(JobStatus fromStatus, JobStatus toStatus)
        {
            var previousStatus = Status;

            ChangeStatus(fromStatus);

            ThreadBridging.Retry(10, () => Status == toStatus);

            if (Status != toStatus)
            {
                // TODO: log here

                if (Status == fromStatus)
                {
                    ChangeStatus(previousStatus);
                }
            }
        }
Exemplo n.º 3
0
        public Deamon(string name, string description, int interval)
            : base(name, description)
        {
            StatusChangedTo += Deamon_StatusChangedTo;

            Implement = (t) =>
            {
                while (true)
                {
                    var startTime = DateTime.Now;

                    ThreadBridging.Retry(3, () =>
                    {
                        try
                        {
                            AppDomainContainer.Instance.Resolve <IDeviceDataSource>(Name).Execute();
                        }
                        catch (Exception e)
                        {
                            LoggerManager.GetLogger().LogEvent("Deamon", LoggerLevel.Error, string.Format("deamon {0} failed to execute.", Name), e);
                            return(false);
                        }

                        return(true);
                    });

                    LoggerManager.GetLogger().LogEvent("Deamon", LoggerLevel.Info, string.Format("deamon {0} finish to execute. cost: {1}", Name, (DateTime.Now - startTime).TotalSeconds));

                    if (CheckTransitionalStatus() == TaskObjectStatus.Sleep)
                    {
                        break;
                    }

                    ThreadBridging.Sleep(interval);
                }

                return(true);
            };
        }