Example #1
0
        public long ScheduledCount([NotNull] string tagName)
        {
            if (tagName == null)
            {
                throw new ArgumentNullException(nameof(tagName));
            }

            return(UseConnection(redis => redis.SortedSetLength(GetRedisKey(RedisTagsKeyInfo.GetScheduledKey(tagName)))));
        }
Example #2
0
        public JobList <ScheduledJobDto> ScheduledJobs(string tagName, int from, int count)
        {
            return(UseConnection(redis =>
            {
                var scheduledJobs = redis
                                    .SortedSetRangeByRankWithScores(GetRedisKey(RedisTagsKeyInfo.GetScheduledKey(tagName)), from, from + count - 1)
                                    .ToList();

                if (scheduledJobs.Count == 0)
                {
                    return new JobList <ScheduledJobDto>(new List <KeyValuePair <string, ScheduledJobDto> >());
                }

                var jobs = new ConcurrentDictionary <string, List <string> >();
                var states = new ConcurrentDictionary <string, List <string> >();;

                var pipeline = redis.CreateBatch();
                var tasks = new Task[scheduledJobs.Count * 2];
                int i = 0;
                foreach (var scheduledJob in scheduledJobs)
                {
                    var jobId = scheduledJob.Element;
                    tasks[i] = pipeline.HashGetAsync(
                        GetRedisKey($"job:{jobId}"),
                        new RedisValue[] { "Type", "Method", "ParameterTypes", "Arguments" })
                               .ContinueWith(x => jobs.TryAdd(jobId, x.Result.ToStringArray().ToList()));
                    i++;
                    tasks[i] = pipeline.HashGetAsync(
                        GetRedisKey($"job:{jobId}:state"),
                        new RedisValue[] { "State", "ScheduledAt" })
                               .ContinueWith(x => states.TryAdd(jobId, x.Result.ToStringArray().ToList()));
                    i++;
                }

                pipeline.Execute();
                Task.WaitAll(tasks);

                return new JobList <ScheduledJobDto>(scheduledJobs
                                                     .Select(job => new KeyValuePair <string, ScheduledJobDto>(
                                                                 job.Element,
                                                                 new ScheduledJobDto
                {
                    EnqueueAt = JobHelper.FromTimestamp((long)job.Score),
                    Job = TryToGetJob(jobs[job.Element][0], jobs[job.Element][1], jobs[job.Element][2], jobs[job.Element][3]),
                    ScheduledAt =
                        states[job.Element].Count > 1
                                    ? JobHelper.DeserializeNullableDateTime(states[job.Element][1])
                                    : null,
                    InScheduledState =
                        ScheduledState.StateName.Equals(states[job.Element][0], StringComparison.OrdinalIgnoreCase)
                }))
                                                     .ToList());
            }));
        }
Example #3
0
        public TagsStatisticVM GetStatistics([NotNull] string tagName)
        {
            tagName = tagName.ToLower();
            return(UseConnection(redis =>
            {
                var stats = new TagsStatisticVM {
                };

                var pipeline = redis.CreateBatch();
                var tasks = new Task[10];

                tasks[0] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetEnqueuedKey(tagName)))
                           .ContinueWith(x => stats.Enqueued = x.Result);

                tasks[1] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetScheduledKey(tagName)))
                           .ContinueWith(x => stats.Scheduled = x.Result);

                tasks[2] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetProcessingKey(tagName)))
                           .ContinueWith(x => stats.Processing = x.Result);

                tasks[3] = pipeline.StringGetAsync(GetRedisKey(RedisTagsKeyInfo.GetStatsSucceededKey(tagName)))
                           .ContinueWith(x => stats.Succeeded = long.Parse(x.Result.HasValue ? (string)x.Result : "0"));

                tasks[4] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetFailedKey(tagName)))
                           .ContinueWith(x => stats.Failed = x.Result);

                tasks[5] = pipeline.StringGetAsync(GetRedisKey(RedisTagsKeyInfo.GetStatsDeletedKey(tagName)))
                           .ContinueWith(x => stats.Deleted = long.Parse(x.Result.HasValue ? (string)x.Result : "0"));

                tasks[6] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetAwaitingKey(tagName)))
                           .ContinueWith(x => stats.Awaiting = x.Result);

                tasks[7] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetRetryKey(tagName)))
                           .ContinueWith(x => stats.Retries = x.Result);

                tasks[8] = pipeline.SetLengthAsync(GetRedisKey("servers"))
                           .ContinueWith(x => stats.Servers = x.Result);

                tasks[9] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetRecurringJobKey(tagName)))
                           .ContinueWith(x => stats.Recurring = x.Result);


                pipeline.Execute();
                Task.WaitAll(tasks);

                return stats;
            }));
        }
Example #4
0
        public List <TagsStatisticVM> GetStatisticsSummary(string[] tags)
        {
            return(UseConnection(redis =>
            {
                var result = new List <TagsStatisticVM> {
                };

                var pipeline = redis.CreateBatch();
                var tasks = new Task[tags.Length * 11];

                var index = 0;
                foreach (var tagName in tags)
                {
                    var stats = new TagsStatisticVM
                    {
                        TagCode = tagName
                    };
                    tasks[index++] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetEnqueuedKey(tagName)))
                                     .ContinueWith(x => stats.Enqueued = x.Result);

                    tasks[index++] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetScheduledKey(tagName)))
                                     .ContinueWith(x => stats.Scheduled = x.Result);

                    tasks[index++] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetProcessingKey(tagName)))
                                     .ContinueWith(x => stats.Processing = x.Result);

                    tasks[index++] = pipeline.StringGetAsync(GetRedisKey(RedisTagsKeyInfo.GetStatsSucceededKey(tagName)))
                                     .ContinueWith(x => stats.Succeeded = long.Parse(x.Result.HasValue ? (string)x.Result : "0"));

                    tasks[index++] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetFailedKey(tagName)))
                                     .ContinueWith(x => stats.Failed = x.Result);

                    tasks[index++] = pipeline.StringGetAsync(GetRedisKey(RedisTagsKeyInfo.GetStatsDeletedKey(tagName)))
                                     .ContinueWith(x => stats.Deleted = long.Parse(x.Result.HasValue ? (string)x.Result : "0"));

                    tasks[index++] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetAwaitingKey(tagName)))
                                     .ContinueWith(x => stats.Awaiting = x.Result);

                    tasks[index++] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetRetryKey(tagName)))
                                     .ContinueWith(x => stats.Retries = x.Result);

                    tasks[index++] = pipeline.StringGetAsync(GetRedisKey(RedisTagsKeyInfo.GetStatsSucceededDateKey(tagName, DateTime.Today)))
                                     .ContinueWith(x => stats.Today = long.Parse(x.Result.HasValue ? (string)x.Result : "0"));

                    tasks[index++] = pipeline.StringGetAsync(GetRedisKey(RedisTagsKeyInfo.GetStatsSucceededHourKey(tagName, DateTime.Now.AddHours(-1))))
                                     .ContinueWith(x => stats.LastHour = long.Parse(x.Result.HasValue ? (string)x.Result : "0"));

                    tasks[index++] = pipeline.SortedSetLengthAsync(GetRedisKey(RedisTagsKeyInfo.GetRecurringJobKey(tagName)))
                                     .ContinueWith(x =>
                    {
                        stats.Recurring = x.Result;
                    });

                    result.Add(stats);
                }


                pipeline.Execute();
                Task.WaitAll(tasks);

                return result;
            }));
        }