public void ServiceName()
        {
            var expected = ScheduledTaskEntry.GenerateLogsPartitionKey(this.GetType().ToString());
            var entity = new ScheduledTaskEntry
            {
                ServiceName = expected,
            };

            Assert.AreEqual(expected, entity.ServiceName);
        }
 public void Identifier()
 {
     var entity = new ScheduledTaskEntry
     {
         PartitionKey = ScheduledTaskEntry.GenerateLogsPartitionKey(this.GetType().ToString()),
         ServiceName = this.GetType().ToString(),
     };
     Assert.IsNull(entity.Identifier);
     var data = Guid.NewGuid();
     entity.Identifier = data;
     Assert.AreEqual(data, entity.Identifier);
     entity.Identifier = null;
     Assert.IsNull(entity.Identifier);
 }
        public async Task CheckCompletedFailureOld()
        {
            var records = new List<ScheduledTaskEntry>();
            var record = new ScheduledTaskEntry()
            {
                StartTime = DateTime.UtcNow,
                CompletionTime = DateTime.UtcNow.AddHours(-1),
                Successful = false,
            };
            records.Add(record);

            var table = Substitute.For<ITableStorage>();
            table.QueryByPartition<ScheduledTaskEntry>(Arg.Any<string>()).Returns(Task.FromResult<IEnumerable<ScheduledTaskEntry>>(records));

            var core = new Coordinator(table, new TimeSpan(9000));
            var perform = await core.Check(this.GetType());

            Assert.IsTrue(perform);

            table.Received().QueryByPartition<ScheduledTaskEntry>(Arg.Any<string>());
        }
        /// <summary>
        /// Determine whether a new task needs to be executed
        /// </summary>
        /// <param name="type">Type</param>
        /// <returns>True if need to execute, false if not</returns>
        public virtual async Task<bool> Check(Type type)
        {
            if (null == type)
            {
                throw new ArgumentNullException("type");
            }

            var entry = new ScheduledTaskEntry()
            {
                PartitionKey = ScheduledTaskEntry.GenerateLogsPartitionKey(type.GetType().ToString()),
                ServiceName = type.GetType().ToString(),
            };

            var performTask = true;

            var records = await this.storage.QueryByPartition<ScheduledTaskEntry>(entry.PartitionKey);

            if (records != null && records.Any())
            {
                var latest = records.OrderByDescending(x => x.StartTime).First();

                performTask = (latest.CompletionTime.HasValue) ?
                    DateTime.UtcNow.Subtract(latest.CompletionTime.Value) >= period || !latest.Successful :
                    DateTime.UtcNow.Subtract(latest.StartTime) >= retryInterval;
            }

            return performTask;
        }