Example #1
0
        /// <summary>
        /// Creates a Serialized Job Definition.
        /// Complex Parameters are stored as JSON strings,
        /// And Type Information is stored in a nonversioned format by default.
        /// </summary>
        /// <typeparam name="T">The type of the job to execute.</typeparam>
        /// <param name="jobExpression">The job Expression</param>
        /// <param name="retryParameters">The retry parameters to use, if any.</param>
        /// <param name="executionTime">A time to schedule; use this to schedule jobs in future</param>
        /// <param name="queueName">The Queue to resolve</param>
        /// <param name="typeNameSerializer">To change the default behavior (nonversioned types) specify a different <see cref="ITypeNameSerializer"/> here.</param>
        /// <returns>A wire-safe Serializable job definition.</returns>
        public static SerializableOddJob CreateJobDefinition <T>(Expression <Action <T> > jobExpression,
                                                                 RetryParameters retryParameters = null, DateTimeOffset?executionTime = null, string queueName = "default", ITypeNameSerializer typeNameSerializer = null)
        {
            var job   = JobCreator.Create(jobExpression);
            var mySer = typeNameSerializer ?? new UnversionedTypeSerializer();

            return(new SerializableOddJob()
            {
                JobId = job.JobId,
                MethodName = job.MethodName,
                JobArgs = job.JobArgs.Select((a, i) => new OddJobSerializedParameter()
                {
                    Ordinal = i,
                    Name = a.Name,
                    Value = Newtonsoft.Json.JsonConvert.SerializeObject(a.Value),
                    TypeName = mySer.GetTypeName(a.Value.GetType())
                }).ToArray(),
                TypeExecutedOn = mySer.GetTypeName(job.TypeExecutedOn),
                Status = job.Status,
                MethodGenericTypes = job.MethodGenericTypes.Select(q => mySer.GetTypeName(q)).ToArray(),
                RetryParameters = retryParameters ?? new RetryParameters(),
                ExecutionTime = executionTime,
                QueueName = queueName
            });
        }
Example #2
0
        public Guid AddJob <TJob>(Expression <Action <TJob> > jobExpression, RetryParameters retryParameters = null, DateTimeOffset?executionTime = null, string queueName = "default")
        {
            var jobInfo = JobCreator.Create <TJob>(jobExpression);
            var newJob  = new OddJobWithMetaAndStorageData()
            {
                JobId           = Guid.NewGuid(),
                JobArgs         = jobInfo.JobArgs,
                MethodName      = jobInfo.MethodName,
                RetryParameters = retryParameters,
                TypeExecutedOn  = jobInfo.TypeExecutedOn,
                CreatedOn       = DateTime.Now,
                Status          = JobStates.New
            };

            if (jobStore.ContainsKey(queueName) == false)
            {
                lock (dictionaryLock)
                {
                    jobStore.GetOrAdd(queueName, new List <OddJobWithMetaAndStorageData>());
                }
            }
            lock (jobLock)
            {
                jobStore[queueName].Add(newJob);
            }
            return(newJob.JobId);
        }
Example #3
0
        public void Can_Run_Jobs_With_Static_Method()
        {
            var next  = JobCreator.Create <SampleJobStaticMethod>((j) => SampleJobStaticMethod.DoThing());
            var jobEx = new DefaultJobExecutor(new DefaultContainerFactory());

            jobEx.ExecuteJob(next);
            Xunit.Assert.True(SampleJobStaticMethod.Called);
        }
Example #4
0
        public void Creator_Respects_StaticJob_Handling()
        {
            var next  = JobCreator.Create <StaticClassJob>((j) => SampleJobStaticClass.DoThing());
            var jobEx = new DefaultJobExecutor(new DefaultContainerFactory());

            jobEx.ExecuteJob(next);
            Xunit.Assert.True(SampleJobStaticClass.Called);
        }
Example #5
0
        public void Can_Run_Jobs_With_No_Params()
        {
            var next  = JobCreator.Create <SampleJobNoParam>((j) => j.DoThing());
            var jobEx = new DefaultJobExecutor(new DefaultContainerFactory());

            jobEx.ExecuteJob(next);
            Xunit.Assert.True(SampleJobNoParam.Called);
        }
Example #6
0
        public void Can_Run_Static_Method_On_Static_Class()
        {
            var next  = JobCreator.Create <object>((j) => SampleJobStaticClass.DoThing());
            var jobEx = new DefaultJobExecutor(new DefaultContainerFactory());

            jobEx.ExecuteJob(next);
            Xunit.Assert.True(SampleJobStaticClass.Called);
        }
Example #7
0
        public void Can_Run_Jobs_With_Param_Type_Matching_And_Overloads()
        {
            var myValue = new ClassTest()
            {
                classTestValue = TestConstants.classTestValue
            };
            var next  = JobCreator.Create <SampleJob2>((j) => j.DoThing(TestConstants.derp, TestConstants.herp, myValue));
            var jobEx = new DefaultJobExecutor(new DefaultContainerFactory());

            jobEx.ExecuteJob(next);
        }
Example #8
0
        public void Can_Run_Job_With_Simple_Method_Calls()
        {
            var myValue = new ClassTest()
            {
                classTestValue = TestConstants.classTestValue
            };
            var next  = JobCreator.Create <SampleJob>((j) => j.DoThing(TestConstants.derp, int.Parse(TestConstants.herp.ToString()), myValue));
            var jobEx = new DefaultJobExecutor(new DefaultContainerFactory());

            jobEx.ExecuteJob(next);
        }
Example #9
0
        public void Can_Run_Job_And_pass_parameters()
        {
            var myValue = new ClassTest()
            {
                classTestValue = TestConstants.classTestValue
            };
            var next  = JobCreator.Create <SampleJob>((j) => j.DoThing(TestConstants.derp, TestConstants.herp, myValue));
            var jobEx = new DefaultJobExecutor(new DefaultContainerFactory());

            jobEx.ExecuteJob(next);
        }
Example #10
0
        public void JobWorker_Returns_Failures_for_Failed_Jobs()
        {
            var actor   = Sys.ActorOf(Props.Create(() => new JobWorkerActor(new MockJobFailureExecutor())));
            var jobInfo = JobCreator.Create((MockJob m) => m.MockMethod());
            var jobData = new OddJobWithMetaAndStorageData()
            {
                Status = JobStates.New, JobArgs = jobInfo.JobArgs, JobId = Guid.NewGuid(), MethodName = jobInfo.MethodName, CreatedOn = DateTime.Now, QueueTime = DateTime.Now, TypeExecutedOn = jobInfo.TypeExecutedOn
            };

            actor.Tell(new ExecuteJobRequest(jobData));
            ExpectMsg <JobFailed>();
        }
Example #11
0
        public void JobQueueManager_Can_Mark_Failure()
        {
            var testStore = new InMemoryTestStore();
            var actor     = Sys.ActorOf(Props.Create(() => new JobQueueLayerActor(new InMemoryTestStore())));
            var jobInfo   = JobCreator.Create((MockJob m) => m.MockMethod());
            var myGuid    = testStore.AddJob((MockJob m) => m.MockMethod(), null, null, "test");
            var ourJob    = InMemoryTestStore.jobPeeker["test"].FirstOrDefault(q => q.JobId == myGuid);

            actor.Tell(new MarkJobFailed(myGuid));
            System.Threading.SpinWait.SpinUntil(() => false, TimeSpan.FromSeconds(2));
            Xunit.Assert.Equal(JobStates.Failed, ourJob.Status);
        }
Example #12
0
        public void Can_Run_Job_With_Arity_in_class_With_Arity()
        {
            var myvalue = new ClassTest()
            {
                classTestValue = TestConstants.classTestValue
            };
            var next = JobCreator.Create <SampleJobInGenericClass <string> >(j =>
                                                                             j.DoThing(TestConstants.derp, TestConstants.herp, myvalue));
            var jobEx = new DefaultJobExecutor(new DefaultContainerFactory());

            jobEx.ExecuteJob(next);
        }
Example #13
0
        public void JobQueueManager_Can_Mark_Retry()
        {
            var testStore   = new InMemoryTestStore();
            var actor       = Sys.ActorOf(Props.Create(() => new JobQueueLayerActor(new InMemoryTestStore())));
            var jobInfo     = JobCreator.Create((MockJob m) => m.MockMethod());
            var myGuid      = testStore.AddJob((MockJob m) => m.MockMethod(), new RetryParameters(1, TimeSpan.FromSeconds(20)), null, "test");
            var ourJob      = InMemoryTestStore.jobPeeker["test"].FirstOrDefault(q => q.JobId == myGuid);
            var attemptTime = DateTime.Now;

            actor.Tell(new MarkJobInRetryAndIncrement(myGuid, attemptTime));
            System.Threading.SpinWait.SpinUntil(() => false, TimeSpan.FromSeconds(2));
            Xunit.Assert.Equal(JobStates.Retry, ourJob.Status);
            Xunit.Assert.Equal(1, ourJob.RetryParameters.RetryCount);
            Xunit.Assert.Equal(attemptTime, ourJob.RetryParameters.LastAttempt);
        }
Example #14
0
        public Guid AddJob <TJob>(Expression <Action <TJob> > jobExpression, RetryParameters retryParameters = null, DateTimeOffset?executionTime = null, string queueName = "default")
        {
            var jobData = JobCreator.Create(jobExpression);
            var toSer   = new FileSystemJobMetaData()
            {
                JobId              = Guid.NewGuid(),
                JobArgs            = jobData.JobArgs,
                MethodName         = jobData.MethodName,
                RetryParameters    = retryParameters ?? new RetryParameters(0, TimeSpan.FromSeconds(0), 0, null),
                TypeExecutedOn     = jobData.TypeExecutedOn,
                QueueName          = queueName,
                CreatedOn          = DateTime.Now,
                Status             = JobStates.New,
                MethodGenericTypes = jobData.MethodGenericTypes
            };

            WriteJobToQueue(toSer);
            return(toSer.JobId);
        }