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); }
private IJob NewJob(IEntitySession session, string name, JobStartInfo startInfo, RetryPolicy retryPolicy) { var job = session.NewEntity <IJob>(); job.Name = name; job.ThreadType = startInfo.ThreadType; job.DeclaringType = startInfo.DeclaringType.Namespace + "." + startInfo.DeclaringType.Name; job.MethodName = startInfo.Method.Name; job.MethodParameterCount = startInfo.Arguments.Length; job.Arguments = SerializeArguments(startInfo.Arguments); job.RetryIntervals = retryPolicy.AsString; return(job); }
// Used for creating 'light' jobs internal JobRunContext(OperationContext context, string jobName, JobStartInfo startInfo, JobRunType runType) { OperationContext = context; JobName = jobName ?? "Unnamed/" + JobRunId; StartInfo = startInfo; StartedOn = context.App.TimeService.UtcNow; JobRunId = Guid.NewGuid(); JobId = Guid.NewGuid(); RunType = runType; _progress = 0; Status = JobRunStatus.Executing; IsPersisted = false; AttemptNumber = 1; }
private JobStartInfo CreateJobStartInfo(IJobRun jobRun, JobRunContext jobContext) { var app = jobContext.App; var startInfo = new JobStartInfo(); var job = jobRun.Job; startInfo.ThreadType = job.ThreadType; startInfo.DeclaringType = ReflectionHelper.GetLoadedType(job.DeclaringType, throwIfNotFound: true); //it will throw if cannot find it var methName = job.MethodName; var argCount = job.MethodParameterCount; startInfo.Method = ReflectionHelper.FindMethod(startInfo.DeclaringType, job.MethodName, job.MethodParameterCount); // if Method is not static, it must be Module instance - this is a convention if (!startInfo.Method.IsStatic) { startInfo.Object = app.GetGlobalObject(startInfo.DeclaringType); //throws if not found. } //arguments var prms = startInfo.Method.GetParameters(); var serArgs = job.Arguments; var arrStrArgs = serArgs.Split(new[] { JsonArgsDelimiter }, StringSplitOptions.None); Util.Check(prms.Length == arrStrArgs.Length, "Serialized arg count ({0}) in job entity " + "does not match the number of target method parameters ({1}); target method: {2}.", arrStrArgs.Length, prms.Length, startInfo.Method.Name); // Deserialize arguments startInfo.Arguments = new object[arrStrArgs.Length]; for (int i = 0; i < arrStrArgs.Length; i++) { var paramType = prms[i].ParameterType; if (paramType == typeof(JobRunContext)) { startInfo.Arguments[i] = jobContext; } else { startInfo.Arguments[i] = Deserialize(paramType, arrStrArgs[i]); } } return(startInfo); }