private void CreateAndPerform <T>(T argumentValue, bool checkJsonOnly = false)
        {
            var type       = typeof(JobArgumentFacts);
            var methodInfo = type.GetMethod("Method", new[] { typeof(T) });

            var serializationMethods = new List <Tuple <string, Func <string> > >();

#if !NETCOREAPP1_0
            if (!checkJsonOnly)
            {
                var converter = TypeDescriptor.GetConverter(typeof(T));
                serializationMethods.Add(new Tuple <string, Func <string> >(
                                             "TypeDescriptor",
                                             () => converter.ConvertToInvariantString(argumentValue)));
            }
#endif

            serializationMethods.Add(new Tuple <string, Func <string> >(
                                         "JSON",
                                         () => JsonConvert.SerializeObject(argumentValue)));

            foreach (var method in serializationMethods)
            {
                var data = new InvocationData(
                    methodInfo?.DeclaringType?.AssemblyQualifiedName,
                    methodInfo?.Name,
                    JobHelper.ToJson(methodInfo?.GetParameters().Select(x => x.ParameterType).ToArray()),
                    JobHelper.ToJson(new[] { method.Item2() }));

                var job = data.DeserializeJob();

                Assert.Equal(argumentValue, job.Args[0]);
            }
        }
Ejemplo n.º 2
0
        private static Job DeserializeJob(string invocationData, string arguments, out InvocationData data)
        {
            data           = SerializationHelper.Deserialize <InvocationData>(invocationData, SerializationOption.User);
            data.Arguments = arguments;

            try
            {
                return(data.DeserializeJob());
            }
            catch (JobLoadException)
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
    public override JobData?GetJobData(string?jobId)
    {
        if (jobId == null)
        {
            throw new ArgumentNullException(nameof(jobId));
        }
        if (Guid.TryParse(jobId, out Guid _) == false)
        {
            return(null);
        }

        try
        {
            Documents.Job data = Storage.Container.ReadItemWithRetries <Documents.Job>(jobId, PartitionKeys.Job);

            InvocationData invocationData = data.InvocationData;
            invocationData.Arguments = data.Arguments;

            Job?job = null;
            JobLoadException?loadException = null;

            try
            {
                job = invocationData.DeserializeJob();
            }
            catch (JobLoadException ex)
            {
                loadException = ex;
            }

            return(new JobData
            {
                Job = job,
                State = data.StateName,
                CreatedAt = data.CreatedOn,
                LoadException = loadException
            });
        }
        catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
        {
            /* ignored */
        }
        catch (AggregateException ex) when(ex.InnerException is CosmosException {
            StatusCode: HttpStatusCode.NotFound
        })
Ejemplo n.º 4
0
        public void Deserialize_CanHandleArgumentWithExplicitTypeName_WhenUsingTypeNameHandlingAuto()
        {
            JobHelper.SetSerializerSettings(new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto
            });

            var data = new InvocationData(
                "Hangfire.Core.Tests.Storage.InvocationDataFacts, Hangfire.Core.Tests",
                "GenericMethod",
                "[\"System.Object, mscorlib\"]",
                "[\"{\\\"$type\\\":\\\"Hangfire.Core.Tests.Storage.InvocationDataFacts+SomeClass, Hangfire.Core.Tests\\\"}\"]");

            var job = data.DeserializeJob();

            Assert.Equal("GenericMethod", job.Method.Name);
            Assert.Equal(new object[] { typeof(object) }, job.Method.GetParameters().Select(x => x.ParameterType).ToArray());
            Assert.IsType <SomeClass>(job.Args[0]);
        }
        public override JobData GetJobData(string jobId)
        {
            if (jobId == null)
            {
                throw new ArgumentNullException(nameof(jobId));
            }

            Uri uri = UriFactory.CreateDocumentUri(Storage.Options.DatabaseName, Storage.Options.CollectionName, jobId);
            Task <DocumentResponse <Documents.Job> > task = Storage.Client.ReadDocumentWithRetriesAsync <Documents.Job>(uri, new RequestOptions {
                PartitionKey = new PartitionKey((int)DocumentTypes.Job)
            });

            task.Wait();

            if (task.Result.Document != null)
            {
                Documents.Job  data           = task.Result;
                InvocationData invocationData = data.InvocationData;
                invocationData.Arguments = data.Arguments;

                Common.Job       job           = null;
                JobLoadException loadException = null;

                try
                {
                    job = invocationData.DeserializeJob();
                }
                catch (JobLoadException ex)
                {
                    loadException = ex;
                }

                return(new JobData
                {
                    Job = job,
                    State = data.StateName,
                    CreatedAt = data.CreatedOn,
                    LoadException = loadException
                });
            }

            return(null);
        }
Ejemplo n.º 6
0
        public override JobData GetJobData([NotNull] string jobId)
        {
            if (jobId == null)
            {
                throw new ArgumentNullException(nameof(jobId));
            }

            //var storedData = Redis.HashGetAll(_storage.GetRedisKey($"job:{jobId}"));
            var storedData = RedisClient.HGetAll(_storage.GetRedisKey($"job:{jobId}"));

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

            string type           = storedData.FirstOrDefault(x => x.Key == "Type").Value;
            string method         = storedData.FirstOrDefault(x => x.Key == "Method").Value;
            string parameterTypes = storedData.FirstOrDefault(x => x.Key == "ParameterTypes").Value;
            string arguments      = storedData.FirstOrDefault(x => x.Key == "Arguments").Value;
            string createdAt      = storedData.FirstOrDefault(x => x.Key == "CreatedAt").Value;

            Job job = null;
            JobLoadException loadException = null;

            var invocationData = new InvocationData(type, method, parameterTypes, arguments);

            try
            {
                job = invocationData.DeserializeJob();
            }
            catch (JobLoadException ex)
            {
                loadException = ex;
            }

            return(new JobData
            {
                Job = job,
                State = storedData.FirstOrDefault(x => x.Key == "State").Value,
                CreatedAt = JobHelper.DeserializeNullableDateTime(createdAt) ?? DateTime.MinValue,
                LoadException = loadException
            });
        }
Ejemplo n.º 7
0
        public Job TryGetJob(out JobLoadException exception)
        {
            exception = null;

            if (Job != null)
            {
                return(new Job(Job.Type, Job.Method, Job.Args.ToArray()));
            }

            try
            {
                return(InvocationData.DeserializeJob());
            }
            catch (JobLoadException ex)
            {
                exception = ex;
                return(null);
            }
        }
Ejemplo n.º 8
0
        private static Job DeserializeJob(string invocationData, string arguments, out InvocationData data, out Exception exception)
        {
            data = InvocationData.DeserializePayload(invocationData);

            if (!String.IsNullOrEmpty(arguments))
            {
                data.Arguments = arguments;
            }

            try
            {
                exception = null;
                return(data.DeserializeJob());
            }
            catch (JobLoadException ex)
            {
                exception = ex;
                return(null);
            }
        }
Ejemplo n.º 9
0
        public override JobData GetJobData(string jobId)
        {
            if (jobId == null)
            {
                throw new ArgumentNullException(nameof(jobId));
            }

            Task <ItemResponse <Documents.Job> > task = Storage.Container.ReadItemWithRetriesAsync <Documents.Job>(jobId, new PartitionKey((int)DocumentTypes.Job));

            task.Wait();

            if (task.Result.Resource != null)
            {
                Documents.Job  data           = task.Result;
                InvocationData invocationData = data.InvocationData;
                invocationData.Arguments = data.Arguments;

                Common.Job       job           = null;
                JobLoadException loadException = null;

                try
                {
                    job = invocationData.DeserializeJob();
                }
                catch (JobLoadException ex)
                {
                    loadException = ex;
                }

                return(new JobData
                {
                    Job = job,
                    State = data.StateName,
                    CreatedAt = data.CreatedOn,
                    LoadException = loadException
                });
            }

            return(null);
        }