Пример #1
0
        public void Process(JobContext context)
        {
            JobDataService jobService = new JobDataService(JobManager.Settings);
            Job            job        = new Job();

            job.Id        = context.JobId;
            job.StartedOn = DateTime.UtcNow;
            job.Status    = JobStatus.Running;

            try
            {
                jobService.UpdateJob(job);
                Pool.Add(context);

                var      assemblies = AppDomain.CurrentDomain.GetAssemblies();
                Assembly assembly   = assemblies.FirstOrDefault(a => a.GetName().Name == context.Type.Assembly);

                if (assembly == null)
                {
                    //log error
                    throw new Exception("Assembly can not be found!");
                }

                Type type = assembly.GetType(context.Type.CompleteClassName);
                if (type == null)
                {
                    throw new Exception($"Type with name '{context.Type.CompleteClassName}' does not exist in assembly {assembly.FullName}");
                }

                var method = type.GetMethod(context.Type.MethodName);
                if (method == null)
                {
                    throw new Exception($"Method with name '{context.Type.MethodName}' does not exist in assembly {assembly.FullName}");
                }

                //execute job method
                method.Invoke(new DynamicObjectCreater(type).CreateInstance(), new object[] { context });

                job.FinishedOn = DateTime.UtcNow;
                job.Status     = JobStatus.Finished;
                jobService.UpdateJob(job);
            }
            catch (Exception ex)
            {
                job.FinishedOn   = DateTime.UtcNow;
                job.Status       = JobStatus.Failed;
                job.ErrorMessage = ex.Message;
                jobService.UpdateJob(job);
            }
            finally
            {
                Pool.Remove(context);
            }
        }
Пример #2
0
        private JobManager(JobManagerSettings settings)
        {
            Settings   = settings;
            JobService = new JobDataService(Settings);
            //Register default job types
            LoadDefaultTypes();

            //Get all jobs with status running and set them to status abort.
            var runningJobs = JobService.GetRunningJobs();

            foreach (var job in runningJobs)
            {
                job.Status     = JobStatus.Aborted;
                job.AbortedBy  = Guid.Empty;                //by system
                job.FinishedOn = DateTime.UtcNow;
                JobService.UpdateJob(job);
            }
        }
Пример #3
0
        public void Process(JobContext context)
        {
            JobDataService jobService = new JobDataService(JobManager.Settings);
            Job            job        = new Job();

            job.Id        = context.JobId;
            job.StartedOn = DateTime.UtcNow;
            job.Status    = JobStatus.Running;

            try
            {
                jobService.UpdateJob(job);

                lock (lockObj)
                {
                    Pool.Add(context);
                }

                var      assemblies = AppDomain.CurrentDomain.GetAssemblies();
                Assembly assembly   = assemblies.FirstOrDefault(a => a.GetName().Name == context.Type.Assembly);

                if (assembly == null)
                {
                    //log error
                    throw new Exception("Assembly can not be found!");
                }

                Type type = assembly.GetType(context.Type.CompleteClassName);
                if (type == null)
                {
                    throw new Exception($"Type with name '{context.Type.CompleteClassName}' does not exist in assembly {assembly.FullName}");
                }

                var method = type.GetMethod(context.Type.MethodName);
                if (method == null)
                {
                    throw new Exception($"Method with name '{context.Type.MethodName}' does not exist in assembly {assembly.FullName}");
                }

                using (var secCtx = SecurityContext.OpenSystemScope())
                {
                    try
                    {
                        DbContext.CreateContext(Settings.ConnectionString);
                        //execute job method
                        method.Invoke(new DynamicObjectCreater(type).CreateInstance(), new object[] { context });
                    }
                    catch (TargetInvocationException ex)
                    {
                        throw ex.InnerException;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        DbContext.CloseContext();
                    }
                }

                job.FinishedOn = DateTime.UtcNow;
                job.Status     = JobStatus.Finished;
                jobService.UpdateJob(job);
            }
            catch (Exception ex)
            {
                using (var secCtx = SecurityContext.OpenSystemScope())
                {
                    try
                    {
                        DbContext.CreateContext(Settings.ConnectionString);

                        Log log = new Log();
                        log.Create(LogType.Error, $"JobPool.Process.{context.Type.Name}", ex);

                        job.FinishedOn   = DateTime.UtcNow;
                        job.Status       = JobStatus.Failed;
                        job.ErrorMessage = ex.Message;
                        jobService.UpdateJob(job);
                    }
                    finally
                    {
                        DbContext.CloseContext();
                    }
                }
            }
            finally
            {
                lock (lockObj)
                {
                    Pool.Remove(context);
                }
            }
        }
Пример #4
0
 private ScheduleManager(JobManagerSettings settings)
 {
     Settings   = settings;
     JobService = new JobDataService(Settings);
 }