Ejemplo n.º 1
0
        public static void UseSilkierQuartz(this IAppBuilder app, SilkierQuartzOptions options, Action <Services> configure = null)
        {
            options = options ?? throw new ArgumentNullException(nameof(options));

            app.UseFileServer(options);

            var services = Services.Create(options);

            configure?.Invoke(services);

            app.Use((owin, next) =>
            {
                owin.Set(Services.ContextKey, services);
                return(next());
            });

            HttpConfiguration config = new HttpConfiguration();

            config.Routes.MapHttpRoute(
                name: nameof(SilkierQuartz),
                routeTemplate: "{controller}/{action}",
                defaults: new { controller = "Scheduler", action = "Index" }
                );

            config.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter(new MultipartFormatterSettings()
            {
                ValidateNonNullableMissedProperty = false, CultureInfo = CultureInfo.InvariantCulture
            }));

            config.Services.Replace(typeof(IHostBufferPolicySelector), new BufferPolicySelector());
            config.Services.Replace(typeof(IExceptionHandler), new ExceptionHandler((IExceptionHandler)config.Services.GetService(typeof(IExceptionHandler)), services.ViewEngine.ErrorPage, true));

            app.UseWebApi(config);
        }
Ejemplo n.º 2
0
        private static void UseFileServer(this IAppBuilder app, SilkierQuartzOptions options)
        {
            IFileSystem fs;

            if (string.IsNullOrEmpty(options.ContentRootDirectory))
            {
                fs = new FixedEmbeddedResourceFileSystem(Assembly.GetExecutingAssembly(), nameof(SilkierQuartz) + ".Content");
            }
            else
            {
                fs = new PhysicalFileSystem(options.ContentRootDirectory);
            }

            var fsOoptions = new FileServerOptions()
            {
                RequestPath        = new PathString("/Content"),
                EnableDefaultFiles = false,
                FileSystem         = fs
            };

            var mimeMap = ((FileExtensionContentTypeProvider)fsOoptions.StaticFileOptions.ContentTypeProvider).Mappings;

            if (!mimeMap.ContainsKey(".woff2"))
            {
                mimeMap.Add(".woff2", "application/font-woff2");
            }

            app.UseFileServer(fsOoptions);
        }
Ejemplo n.º 3
0
        public static ViewEngineFileSystem Create(SilkierQuartzOptions options)
        {
            ViewEngineFileSystem fs;

            if (string.IsNullOrEmpty(options.ViewsRootDirectory))
            {
                fs = new EmbeddedFileSystem();
            }
            else
            {
                fs = new DiskFileSystem(options.ViewsRootDirectory);
            }

            return(fs);
        }
Ejemplo n.º 4
0
        private static void UseFileServer(this IApplicationBuilder app, SilkierQuartzOptions options)
        {
            IFileProvider fs;

            if (string.IsNullOrEmpty(options.ContentRootDirectory))
            {
                fs = new ManifestEmbeddedFileProvider(Assembly.GetExecutingAssembly(), "Content");
            }
            else
            {
                fs = new PhysicalFileProvider(options.ContentRootDirectory);
            }

            var fsOptions = new FileServerOptions()
            {
                RequestPath             = new PathString($"{options.VirtualPathRoot}/Content"),
                EnableDefaultFiles      = false,
                EnableDirectoryBrowsing = false,
                FileProvider            = fs
            };

            app.UseFileServer(fsOptions);
        }
Ejemplo n.º 5
0
        public static Services Create(SilkierQuartzOptions options)
        {
            var handlebarsConfiguration = new HandlebarsConfiguration()
            {
                FileSystem = ViewFileSystemFactory.Create(options),
                ThrowOnUnresolvedBindingExpression = true,
            };

            var services = new Services()
            {
                Options    = options,
                Scheduler  = options.Scheduler,
                Handlebars = HandlebarsDotNet.Handlebars.Create(handlebarsConfiguration),
            };

            HandlebarsHelpers.Register(services);

            services.ViewEngine   = new ViewEngine(services);
            services.TypeHandlers = new TypeHandlerService(services);
            services.Cache        = new Cache(services);

            return(services);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Use SilkierQuartz and automatically discover IJob subclasses with SilkierQuartzAttribute
        /// </summary>
        /// <param name="app"></param>
        /// <param name="options"></param>
        /// <param name="configure"></param>
        public static IApplicationBuilder UseSilkierQuartz(this IApplicationBuilder app, SilkierQuartzOptions options, Action <Services> configure = null)
        {
            options = options ?? throw new ArgumentNullException(nameof(options));

            app.UseFileServer(options);
            if (options.Scheduler == null)
            {
                try
                {
                    options.Scheduler = app.ApplicationServices.GetRequiredService <ISchedulerFactory>()?.GetScheduler().Result;
                }
                catch (Exception)
                {
                    options.Scheduler = null;
                }
                if (options.Scheduler == null)
                {
                    options.Scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
                }
            }
            var services = Services.Create(options);

            configure?.Invoke(services);

            app.Use(async(context, next) =>
            {
                context.Items[typeof(Services)] = services;
                await next.Invoke();
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(nameof(SilkierQuartz), $"{options.VirtualPathRoot}/{{controller=Scheduler}}/{{action=Index}}");

                SilkierQuartzAuthenticateConfig.VirtualPathRoot          = options.VirtualPathRoot;
                SilkierQuartzAuthenticateConfig.VirtualPathRootUrlEncode = options.VirtualPathRoot.Replace("/", "%2F");
                SilkierQuartzAuthenticateConfig.UserName     = options.AccountName;
                SilkierQuartzAuthenticateConfig.UserPassword = options.AccountPassword;
                SilkierQuartzAuthenticateConfig.IsPersist    = options.IsAuthenticationPersist;
                endpoints.MapControllerRoute($"{nameof(SilkierQuartz)}Authenticate",
                                             $"{options.VirtualPathRoot}/{{controller=Authenticate}}/{{action=Login}}");
            });

            var types = GetSilkierQuartzJobs();

            types.ForEach(t =>
            {
                var so = t.GetCustomAttribute <SilkierQuartzAttribute>();
                app.UseQuartzJob(t, () =>
                {
                    var tb = TriggerBuilder.Create();
                    tb.WithSimpleSchedule(x =>
                    {
                        x.WithInterval(so.WithInterval);
                        if (so.RepeatCount > 0)
                        {
                            x.WithRepeatCount(so.RepeatCount);
                        }
                        else
                        {
                            x.RepeatForever();
                        }
                    });
                    if (so.StartAt == DateTimeOffset.MinValue)
                    {
                        tb.StartNow();
                    }
                    else
                    {
                        tb.StartAt(so.StartAt);
                    }
                    var tk = new TriggerKey(!string.IsNullOrEmpty(so.TriggerName) ? so.TriggerName : $"{t.Name}'s Trigger");
                    if (!string.IsNullOrEmpty(so.TriggerGroup))
                    {
                        so.TriggerGroup = so.TriggerGroup;
                    }
                    tb.WithIdentity(tk);
                    tb.WithDescription(so.TriggerDescription ?? $"{t.Name}'s Trigger,full name is {t.FullName}");
                    if (so.Priority > 0)
                    {
                        tb.WithPriority(so.Priority);
                    }
                    return(tb);
                });
            });


            return(app);
        }
Ejemplo n.º 7
0
 public static IApplicationBuilder UseQuartzmin(this IApplicationBuilder app, SilkierQuartzOptions options, Action <Services> configure = null)
 => app.UseSilkierQuartz(options, configure);