Ejemplo n.º 1
0
        public static IGlobalConfiguration UseRecurringJobAdmin(this IGlobalConfiguration config, bool includeReferences = false, [NotNull] params Assembly[] assemblies)
        {
            if (assemblies == null)
            {
                throw new ArgumentNullException(nameof(assemblies));
            }

            StorageAssemblySingleton.GetInstance().SetCurrentAssembly(includeReferences, assemblies);
            PeriodicJobBuilder.GetAllJobs();
            CreateManagmentJob();
            return(config);
        }
Ejemplo n.º 2
0
        public static IGlobalConfiguration UseRecurringJobAdmin(this IGlobalConfiguration config, [NotNull] params string[] assemblies)
        {
            if (assemblies == null)
            {
                throw new ArgumentNullException(nameof(assemblies));
            }

            StorageAssemblySingleton.GetInstance().SetCurrentAssembly(assemblies: assemblies.Select(x => Type.GetType(x).Assembly).ToArray());
            PeriodicJobBuilder.GetAllJobs();
            CreateManagmentJob();
            return(config);
        }
Ejemplo n.º 3
0
        internal static void GetAllJobs()
        {
            Metadata = new List <RecurringJobAttribute>();

            foreach (var assembly in StorageAssemblySingleton.GetInstance().currentAssembly)
            {
                foreach (var type in assembly.GetTypes())
                {
                    foreach (var method in type.GetTypeInfo().DeclaredMethods)
                    {
                        if (!method.IsDefined(typeof(RecurringJobAttribute), false))
                        {
                            continue;
                        }

                        var attribute = method.GetCustomAttribute <RecurringJobAttribute>(false);

                        if (attribute == null)
                        {
                            continue;
                        }

                        if (method.GetCustomAttributes(true).OfType <RecurringJobAttribute>().Any())
                        {
                            var attr = method.GetCustomAttribute <RecurringJobAttribute>();
                            Metadata.Add(attr);
                        }

                        var _registry = new RecurringJobRegistry();

                        _registry.Register(
                            attribute.RecurringJobId,
                            method,
                            attribute.Cron,
                            string.IsNullOrEmpty(attribute.TimeZone) ? TimeZoneInfo.Utc : TimeZoneInfo.FindSystemTimeZoneById(attribute.TimeZone),
                            attribute.Queue ?? EnqueuedState.DefaultQueue);
                    }
                }
            }
        }
        public void AddOrUpdate(PeriodicJob periodicJob)
        {
            var job      = periodicJob;
            var timeZone = TimeZoneInfo.Utc;

            //验证Cron
            CronExpression.Parse(periodicJob.Cron);

            //验证时区
            if (!string.IsNullOrEmpty(job.TimeZoneId))
            {
                timeZone = TimeZoneInfo.FindSystemTimeZoneById(job.TimeZoneId);
            }

            //验证ClassName
            var classType = StorageAssemblySingleton.GetInstance().GetClassType(job.ClassFullName);

            if (classType == null)
            {
                throw new Exception($"Class Name :{job.ClassFullName} 不存在");
            }

            //验证MethodName
            var methodInfo = StorageAssemblySingleton.GetInstance().GetMethodInfo(job.ClassFullName, job.MethodName);

            if (methodInfo == null)
            {
                throw new Exception($"Method Name :{job.MethodName} 不存在");
            }

            AddOrUpdate(
                job.Id,
                classType,
                methodInfo,
                job.Cron,
                timeZone,
                job.Queue ?? EnqueuedState.DefaultQueue);
        }
        public async Task Dispatch([NotNull] DashboardContext context)
        {
            var response = new Response()
            {
                Status = true
            };

            var job = new PeriodicJob();

            job.Id         = context.Request.GetQuery("Id");
            job.Cron       = context.Request.GetQuery("Cron");
            job.Class      = context.Request.GetQuery("Class");
            job.Method     = context.Request.GetQuery("Method");
            job.Queue      = context.Request.GetQuery("Queue");
            job.TimeZoneId = context.Request.GetQuery("TimeZoneId");

            var timeZone = TimeZoneInfo.Utc;

            if (!Utility.IsValidSchedule(job.Cron))
            {
                response.Status  = false;
                response.Message = "Invalid CRON";

                await context.Response.WriteAsync(JsonConvert.SerializeObject(response));

                return;
            }

            try
            {
                if (!string.IsNullOrEmpty(job.TimeZoneId))
                {
                    timeZone = TimeZoneInfo.FindSystemTimeZoneById(job.TimeZoneId);
                }
            }
            catch (Exception ex)
            {
                response.Status  = false;
                response.Message = ex.Message;

                await context.Response.WriteAsync(JsonConvert.SerializeObject(response));

                return;
            }


            if (!StorageAssemblySingleton.GetInstance().IsValidType(job.Class))
            {
                response.Status  = false;
                response.Message = "The Class not found";

                await context.Response.WriteAsync(JsonConvert.SerializeObject(response));

                return;
            }

            if (!StorageAssemblySingleton.GetInstance().IsValidMethod(job.Class, job.Method))
            {
                response.Status  = false;
                response.Message = "The Method not found";

                await context.Response.WriteAsync(JsonConvert.SerializeObject(response));

                return;
            }


            var methodInfo = StorageAssemblySingleton.GetInstance().currentAssembly
                             .Where(x => x?.GetType(job.Class)?.GetMethod(job.Method) != null)
                             .FirstOrDefault()
                             .GetType(job.Class)
                             .GetMethod(job.Method);

            _recurringJobRegistry.Register(
                job.Id,
                methodInfo,
                job.Cron,
                timeZone,
                job.Queue ?? EnqueuedState.DefaultQueue);


            context.Response.StatusCode = (int)HttpStatusCode.OK;

            await context.Response.WriteAsync(JsonConvert.SerializeObject(response));
        }