Exemple #1
0
        /// <summary>
        ///     The mock monitor api with exception.
        /// </summary>
        private void MockMonitorApiWithException()
        {
            var sh = new StateHistoryDto
            {
                Data =
                    new Dictionary <string, string>
                {
                    {
                        "ExceptionMessage",
                        "string message"
                    },
                    {
                        "ExceptionDetails",
                        "string details"
                    }
                },
                CreatedAt = DateTime.Now,
                Reason    = "A reason",
                StateName = "BadState"
            };

            var jd = new JobDetailsDto {
                CreatedAt = DateTime.Now, History = new List <StateHistoryDto> {
                    sh
                }
            };

            this.StateHistoryWithException = jd;
            this.MonitorApiMock.Setup(m => m.JobDetails(It.IsAny <string>())).Returns(jd);
        }
        public override void AddJobState(string jobId, IHangfireState state)
        {
            QueueCommand(() =>
            {
                var job = _data.Get <JobDto>(jobId);
                if (job == null)
                {
                    return;
                }

                DateTime createdAt = DateTime.UtcNow;
                Dictionary <string, string> serializedStateData = state.SerializeData();

                var stateData = new StateDto
                {
                    Id        = AutoIncrementIdGenerator.GenerateId(typeof(StateDto)),
                    JobId     = jobId,
                    Name      = state.Name,
                    Reason    = state.Reason,
                    CreatedAt = createdAt,
                    Data      = SerializationHelper.Serialize(serializedStateData, null, SerializationOption.User)
                };

                var stateHistory = new StateHistoryDto
                {
                    StateName = state.Name,
                    CreatedAt = createdAt,
                    Reason    = state.Reason,
                    Data      = serializedStateData
                };

                job.History.Add(stateHistory);
            });
        }
Exemple #3
0
        /// <summary>
        /// Returns the job state for the given job.
        /// </summary>
        private JobState GetJobState(StateHistoryDto jobDetails)
        {
            var stateName = jobDetails?.StateName;

            if (stateName == EnqueuedState.StateName)
            {
                return(JobState.NotStarted);
            }
            else if (stateName == ProcessingState.StateName)
            {
                return(JobState.InProgress);
            }
            else
            {
                return(JobState.Unknown);
            }
        }
Exemple #4
0
        /// <summary>
        ///     Returns the job state for the given job.
        /// </summary>
        private static JobState GetJobState(StateHistoryDto jobDetails)
        {
            var stateName = jobDetails?.StateName;

            if (stateName == EnqueuedState.StateName)
            {
                return(JobState.NotStarted);
            }

            if (stateName == AwaitingState.StateName)
            {
                return(JobState.Awaiting);
            }

            if (stateName == SucceededState.StateName)
            {
                return(JobState.Succeeded);
            }

            if (stateName == FailedState.StateName)
            {
                return(JobState.Failed);
            }

            if (stateName == ScheduledState.StateName)
            {
                return(JobState.Scheduled);
            }

            if (stateName == DeletedState.StateName)
            {
                return(JobState.Deleted);
            }

            return(stateName == ProcessingState.StateName ? JobState.InProgress : JobState.Unknown);
        }
Exemple #5
0
        public JobDetailsDto JobDetails([NotNull] string jobId)
        {
            if (jobId == null)
            {
                throw new ArgumentNullException(nameof(jobId));
            }

            return(UseConnection(redis =>
            {
                var job = redis
                          .HashGetAll(_storage.GetRedisKey($"job:{jobId}"))
                          .ToStringDictionary();

                if (job.Count == 0)
                {
                    return null;
                }

                var hiddenProperties = new[] { "Type", "Method", "ParameterTypes", "Arguments", "State", "CreatedAt", "Fetched" };

                var history = redis
                              .ListRange(_storage.GetRedisKey($"job:{jobId}:history"))
                              .ToStringArray()
                              .Select(SerializationHelper.Deserialize <Dictionary <string, string> >)
                              .ToList();

                // history is in wrong order, fix this
                history.Reverse();

                var stateHistory = new List <StateHistoryDto>(history.Count);
                foreach (var entry in history)
                {
                    var stateData = new Dictionary <string, string>(entry, StringComparer.OrdinalIgnoreCase);
                    var dto = new StateHistoryDto
                    {
                        StateName = stateData["State"],
                        Reason = stateData.ContainsKey("Reason") ? stateData["Reason"] : null,
                        CreatedAt = JobHelper.DeserializeDateTime(stateData["CreatedAt"]),
                    };

                    // Each history item contains all of the information,
                    // but other code should not know this. We'll remove
                    // unwanted keys.
                    stateData.Remove("State");
                    stateData.Remove("Reason");
                    stateData.Remove("CreatedAt");

                    dto.Data = stateData;
                    stateHistory.Add(dto);
                }

                // For compatibility
                if (!job.ContainsKey("Method"))
                {
                    job.Add("Method", null);
                }
                if (!job.ContainsKey("ParameterTypes"))
                {
                    job.Add("ParameterTypes", null);
                }

                return new JobDetailsDto
                {
                    Job = TryToGetJob(job["Type"], job["Method"], job["ParameterTypes"], job["Arguments"]),
                    CreatedAt =
                        job.ContainsKey("CreatedAt")
                            ? JobHelper.DeserializeDateTime(job["CreatedAt"])
                            : (DateTime?)null,
                    Properties =
                        job.Where(x => !hiddenProperties.Contains(x.Key)).ToDictionary(x => x.Key, x => x.Value),
                    History = stateHistory
                };
            }));
        }
Exemple #6
0
        public JobDetailsDto JobDetails(string jobId)
        {
            return(UseConnection(redis =>
            {
                var job = redis.HashGetAll(String.Format(RedisStorage.Prefix + "job:{0}", jobId)).ToStringDictionary();
                if (job.Count == 0)
                {
                    return null;
                }

                var hiddenProperties = new[] { "Type", "Method", "ParameterTypes", "Arguments", "State", "CreatedAt" };

                var historyList = redis.ListRange(String.Format(RedisStorage.Prefix + "job:{0}:history", jobId))
                                  .Select(x => (string)x).ToList();

                var history = historyList
                              .Select(JobHelper.FromJson <Dictionary <string, string> >)
                              .ToList();

                var stateHistory = new List <StateHistoryDto>(history.Count);
                foreach (var entry in history)
                {
                    var stateData = new Dictionary <string, string>(entry, StringComparer.OrdinalIgnoreCase);
                    var dto = new StateHistoryDto
                    {
                        StateName = stateData["State"],
                        Reason = stateData.ContainsKey("Reason") ? stateData["Reason"] : null,
                        CreatedAt = JobHelper.DeserializeDateTime(stateData["CreatedAt"]),
                    };

                    // Each history item contains all of the information,
                    // but other code should not know this. We'll remove
                    // unwanted keys.
                    stateData.Remove("State");
                    stateData.Remove("Reason");
                    stateData.Remove("CreatedAt");

                    dto.Data = stateData;
                    stateHistory.Add(dto);
                }

                // For compatibility
                if (!job.ContainsKey("Method"))
                {
                    job.Add("Method", null);
                }
                if (!job.ContainsKey("ParameterTypes"))
                {
                    job.Add("ParameterTypes", null);
                }

                return new JobDetailsDto
                {
                    Job = TryToGetJob(job["Type"], job["Method"], job["ParameterTypes"], job["Arguments"]),
                    CreatedAt =
                        job.ContainsKey("CreatedAt")
                            ? JobHelper.DeserializeDateTime(job["CreatedAt"])
                            : (DateTime?)null,
                    Properties =
                        job.Where(x => !hiddenProperties.Contains(x.Key)).ToDictionary(x => x.Key, x => x.Value),
                    History = stateHistory
                };
            }));
        }
 public void QueueHangfire_GetJobState(StateHistoryDto jobDetails, JobState jobStateExpected)
 {
     InvokeGetJobState(jobDetails).Should().Be(jobStateExpected);
 }
        private JobState InvokeGetJobState(StateHistoryDto jobDetails)
        {
            object[] parameters = { jobDetails };

            return((JobState)_methodInfoGetJobState.Invoke(null, parameters));
        }
Exemple #9
0
        public JobDetailsDto JobDetails(string jobId)
        {
            var job = _redis.GetAllEntriesFromHash(String.Format("hangfire:job:{0}", jobId));

            if (job.Count == 0)
            {
                return(null);
            }

            var hiddenProperties = new[]
            { "Type", "Method", "ParameterTypes", "Arguments", "State", "CreatedAt" };

            var historyList = _redis.GetAllItemsFromList(
                String.Format("hangfire:job:{0}:history", jobId));

            var history = historyList
                          .Select(JobHelper.FromJson <Dictionary <string, string> >)
                          .ToList();

            var stateHistory = new List <StateHistoryDto>(history.Count);

            foreach (var entry in history)
            {
                var dto = new StateHistoryDto
                {
                    StateName = entry["State"],
                    Reason    = entry.ContainsKey("Reason") ? entry["Reason"] : null,
                    CreatedAt = JobHelper.FromStringTimestamp(entry["CreatedAt"]),
                };

                // Each history item contains all of the information,
                // but other code should not know this. We'll remove
                // unwanted keys.
                var stateData = new Dictionary <string, string>(entry);
                stateData.Remove("State");
                stateData.Remove("Reason");
                stateData.Remove("CreatedAt");

                dto.Data = stateData;
                stateHistory.Add(dto);
            }

            // For compatibility
            if (!job.ContainsKey("Method"))
            {
                job.Add("Method", null);
            }
            if (!job.ContainsKey("ParameterTypes"))
            {
                job.Add("ParameterTypes", null);
            }

            return(new JobDetailsDto
            {
                Job = TryToGetJob(job["Type"], job["Method"], job["ParameterTypes"], job["Arguments"]),
                CreatedAt =
                    job.ContainsKey("CreatedAt") ? JobHelper.FromStringTimestamp(job["CreatedAt"]) : (DateTime?)null,
                Properties = job.Where(x => !hiddenProperties.Contains(x.Key)).ToDictionary(x => x.Key, x => x.Value),
                History = stateHistory
            });
        }