Esempio n. 1
0
        /// <summary>
        /// Create <see cref="RecurringJob"/> with the provider for specified interface or class.
        /// </summary>
        /// <param name="typesProvider">Specified interface or class</param>
        public void Build(Func <IEnumerable <Type> > typesProvider)
        {
            if (typesProvider == null)
            {
                throw new ArgumentNullException(nameof(typesProvider));
            }

            foreach (var type in typesProvider())
            {
                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 (string.IsNullOrWhiteSpace(attribute.RecurringJobId))
                    {
                        attribute.RecurringJobId = method.GetRecurringJobId();
                    }

                    if (!attribute.Enabled)
                    {
                        RecurringJob.RemoveIfExists(attribute.RecurringJobId);
                        continue;
                    }
                    _registry.Register(
                        attribute.RecurringJobId,
                        method,
                        attribute.Cron,
                        string.IsNullOrEmpty(attribute.TimeZone) ? TimeZoneInfo.Utc : TimeZoneInfo.FindSystemTimeZoneById(attribute.TimeZone),
                        attribute.Queue ?? EnqueuedState.DefaultQueue);
                }
            }
        }
Esempio n. 2
0
        public void Build(Func <IEnumerable <RecurringJobInfo> > recurringJobInfoProvider)
        {
            if (recurringJobInfoProvider == null)
            {
                throw new ArgumentNullException(nameof(recurringJobInfoProvider));
            }

            foreach (RecurringJobInfo recurringJobInfo in recurringJobInfoProvider())
            {
                if (string.IsNullOrWhiteSpace(recurringJobInfo.RecurringJobId))
                {
                    throw new Exception($"The property of {nameof(recurringJobInfo.RecurringJobId)} is null, empty, or consists only of white-space.");
                }
                if (!recurringJobInfo.Enable)
                {
                    RecurringJob.RemoveIfExists(recurringJobInfo.RecurringJobId);
                    continue;
                }
                _registry.Register(recurringJobInfo);
            }
        }
Esempio n. 3
0
        public async Task Dispatch([NotNull] DashboardContext context)
        {
            if (!"POST".Equals(context.Request.Method, StringComparison.OrdinalIgnoreCase))
            {
                context.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
                return;
            }

            var options = await GetRecurringJobInfoAsync(context);

            if (options == null ||
                string.IsNullOrWhiteSpace(options.Type) ||
                string.IsNullOrWhiteSpace(options.Cron) ||
                string.IsNullOrWhiteSpace(options.JobId) ||
                string.IsNullOrWhiteSpace(options.MethodName) ||
                string.IsNullOrWhiteSpace(options.Queue) ||
                string.IsNullOrWhiteSpace(options.TimeZone))
            {
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                await context.Response.WriteAsync("parameters is not null or empty");

                return;
            }



            Type jobType = Type.GetType(options.Type, false, true);

            if (jobType == null)
            {
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                await context.Response.WriteAsync($"{options.Type} error");

                return;
            }
            var method = jobType.GetTypeInfo().GetDeclaredMethod(options.MethodName);

            if (method == null)
            {
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                await context.Response.WriteAsync($"{options.MethodName} error");

                return;
            }
            TimeZoneInfo timeZoneInfo = null;

            try
            {
                timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(options.TimeZone);
            }
            catch (TimeZoneNotFoundException)
            {
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                await context.Response.WriteAsync($"{options.TimeZone} error");

                return;
            }

            _recurringJobRegistry.Register(options.JobId, method, options.Cron, timeZoneInfo, options.Queue);
            await context.Response.WriteAsync("添加成功");
        }