private JobStartInfo CreateJobStartInfo(LambdaExpression expression, JobThreadType threadType) { var info = new JobStartInfo(); var callExpr = expression.Body as MethodCallExpression; Util.Check(callExpr != null, "Invalid Job definition expression - must be a method call: {0}", expression); var method = callExpr.Method; if (!method.IsStatic) { // it is instance method; check that it is defined on one of global objects - module, service, or custom global object var obj = this.App.GetGlobalObject(method.DeclaringType); Util.Check(obj != null, "Job method {0}.{1} is an instance method, but is defined on object that is not EntityModule, service or any registered global object.", method.DeclaringType, method.Name); } info.Method = method; info.ReturnsTask = info.Method.ReturnType.IsTypeOrSubType(typeof(Task)); info.ThreadType = threadType; info.DeclaringType = method.DeclaringType; info.Arguments = new object[callExpr.Arguments.Count]; for (int i = 0; i < callExpr.Arguments.Count; i++) { var argExpr = callExpr.Arguments[i]; if (argExpr.Type == typeof(JobRunContext)) { continue; } info.Arguments[i] = ExpressionHelper.Evaluate(argExpr); } return(info); }
/// <summary>Creates a job entity. The job can be scheduled later to run at certain time(s).</summary> /// <param name="session">Entity session.</param> /// <param name="jobName">Name or code that identifies the job.</param> /// <param name="jobMethod">The expression representing a call to the worker method. Must be a sync or async method returning Task. </param> /// <param name="threadType">Thread type.</param> /// <param name="retryPolicy">Optional, retry policy.</param> /// <returns>The created job entity.</returns> /// <remarks>The job implementation method must be a call to a static or instance void method. /// See remarks for ExecuteWithRetries methods for more about implementation method. /// </remarks> public static IJob CreateJob(this IEntitySession session, string jobName, Expression <Action <JobRunContext> > jobMethod, JobThreadType threadType = JobThreadType.Background, RetryPolicy retryPolicy = null) { var service = session.Context.App.GetService <IJobExecutionService>(); var job = service.CreateJob(session, jobName, jobMethod, threadType, retryPolicy); return(job); }
public IJob CreateJob(IEntitySession session, string name, LambdaExpression lambda, JobThreadType threadType = JobThreadType.Pool, RetryPolicy retryPolicy = null) { retryPolicy = retryPolicy ?? _settings.DefaultRetryPolicy; var startInfo = CreateJobStartInfo(lambda, threadType); var job = NewJob(session, name, startInfo, retryPolicy); return(job); }
/// <summary>Creates a job and a job run to start at a given UTC time.</summary> /// <param name="session">Entity session.</param> /// <param name="jobName">Job name; free-form string to identify the job in the database.</param> /// <param name="jobMethod">Method to execute. Must be a call to a static or entity module method.</param> /// <param name="runOnUtc">UTC date-time to run job at.</param> /// <param name="dataId">Optional, a Guid value to pass to executing job in JobRunContext.</param> /// <param name="data">Optional, a string value to pass to executing job in JobRunContext.</param> /// <param name="threadType">Thread type.</param> /// <param name="retryPolicy">Optional, retry policy.</param> /// <returns>Job run entity.</returns> /// <remarks>See remarks for method ExecuteWithRetriesNoWait for more information about job method.</remarks> public static IJobRun ScheduleJobRunOn(this IEntitySession session, string jobName, Expression <Action <JobRunContext> > jobMethod, DateTime runOnUtc, Guid?dataId = null, string data = null, JobThreadType threadType = JobThreadType.Background, RetryPolicy retryPolicy = null) { var service = session.Context.App.GetService <IJobExecutionService>(); var job = service.CreateJob(session, jobName, jobMethod, threadType, retryPolicy); var jobRun = service.ScheduleJobRunOn(job, runOnUtc, dataId, data); return(jobRun); }
/// <summary>Creates a job and starts it after session.SaveChanges() is called.</summary> /// <param name="session">Entity session.</param> /// <param name="jobName">Job name; free-form string to identify the job in the database.</param> /// <param name="jobMethod">Method to execute. Must be a call to a static or entity module method.</param> /// <param name="dataId">Optional, a Guid value to pass to executing job in JobRunContext.</param> /// <param name="data">Optional, a string value to pass to executing job in JobRunContext.</param> /// <param name="threadType">Thread type.</param> /// <param name="retryPolicy">Optional, retry policy.</param> /// <returns>Job run entity.</returns> /// <remarks>See remarks for method ExecuteWithRetriesNoWait for more information about job method.</remarks> public static IJobRun ScheduleJobRunOnSaveChanges(this IEntitySession session, string jobName, Expression <Action <JobRunContext> > jobMethod, Guid?dataId = null, string data = null, JobThreadType threadType = JobThreadType.Background, RetryPolicy retryPolicy = null) { var job = CreateJob(session, jobName, jobMethod, threadType, retryPolicy); var jobRun = job.ScheduleJobRunOnSaveChanges(dataId, data); return(jobRun); }