Пример #1
0
    /// <summary>
    /// Replaces internal FCN with a custom file change monitor for RockWeb
    /// </summary>
    public static void RockWebFileChangeMonitor()
    {
        StopFileMonitoring();
        HttpInternals.StopFileMonitoring();
        DirectoryInfo rockWebPath = new DirectoryInfo(HttpRuntime.AppDomainAppPath);

        FileSystemWatcher rockWebFsw = new FileSystemWatcher(rockWebPath.FullName);

        rockWebFsw.NotifyFilter          = NotifyFilters.LastWrite;
        rockWebFsw.IncludeSubdirectories = true;
        rockWebFsw.Changed            += fsw_Changed;
        rockWebFsw.EnableRaisingEvents = true;

        // also restart if any .cs files are modified in the solution
        var solutionPath = Path.Combine(rockWebPath.Parent.FullName);
        FileSystemWatcher sourceFileFsw = new FileSystemWatcher(solutionPath, "*.cs");

        sourceFileFsw.NotifyFilter          = NotifyFilters.LastWrite;
        sourceFileFsw.IncludeSubdirectories = true;
        sourceFileFsw.Changed            += sourceFileFsw_Changed;
        sourceFileFsw.EnableRaisingEvents = true;
    }
Пример #2
0
        /// <summary>
        /// Handles the Start event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Application_Start(object sender, EventArgs e)
        {
            if (System.Web.Hosting.HostingEnvironment.IsDevelopmentEnvironment)
            {
                HttpInternals.RockWebFileChangeMonitor();
            }

            // Check if database should be auto-migrated for the core and plugins
            bool autoMigrate = true;

            if (!Boolean.TryParse(ConfigurationManager.AppSettings["AutoMigrateDatabase"], out autoMigrate))
            {
                autoMigrate = true;
            }

            if (autoMigrate)
            {
                try
                {
                    Database.SetInitializer(new MigrateDatabaseToLatestVersion <Rock.Data.RockContext, Rock.Migrations.Configuration>());

                    // explictly check if the database exists, and force create it if doesn't exist
                    Rock.Data.RockContext rockContext = new Rock.Data.RockContext();
                    if (!rockContext.Database.Exists())
                    {
                        rockContext.Database.Initialize(true);
                    }
                    else
                    {
                        var migrator = new System.Data.Entity.Migrations.DbMigrator(new Rock.Migrations.Configuration());
                        migrator.Update();
                    }

                    // Migrate any plugins that have pending migrations
                    List <Type> configurationTypeList = Rock.Reflection.FindTypes(typeof(System.Data.Entity.Migrations.DbMigrationsConfiguration)).Select(a => a.Value).ToList();

                    foreach (var configType in configurationTypeList)
                    {
                        if (configType != typeof(Rock.Migrations.Configuration))
                        {
                            var config = Activator.CreateInstance(configType) as System.Data.Entity.Migrations.DbMigrationsConfiguration;
                            System.Data.Entity.Migrations.DbMigrator pluginMigrator = Activator.CreateInstance(typeof(System.Data.Entity.Migrations.DbMigrator), config) as System.Data.Entity.Migrations.DbMigrator;
                            pluginMigrator.Update();
                        }
                    }
                }
                catch (Exception ex)
                {
                    // if migrations fail, log error and attempt to continue
                    LogError(ex, -1, string.Empty, HttpContext.Current);
                }
            }
            else
            {
                // default Initializer is CreateDatabaseIfNotExists, but we don't want that to happen if automigrate is false, so set it to NULL so that nothing happens
                Database.SetInitializer <Rock.Data.RockContext>(null);
            }

            // Preload the commonly used objects
            LoadCacheObjects();

            // setup and launch the jobs infrastructure if running under IIS
            bool runJobsInContext = Convert.ToBoolean(ConfigurationManager.AppSettings["RunJobsInIISContext"]);

            if (runJobsInContext)
            {
                ISchedulerFactory sf;

                // create scheduler
                sf    = new StdSchedulerFactory();
                sched = sf.GetScheduler();

                // get list of active jobs
                ServiceJobService jobService = new ServiceJobService();
                foreach (ServiceJob job in jobService.GetActiveJobs().ToList())
                {
                    try
                    {
                        IJobDetail jobDetail  = jobService.BuildQuartzJob(job);
                        ITrigger   jobTrigger = jobService.BuildQuartzTrigger(job);

                        sched.ScheduleJob(jobDetail, jobTrigger);
                    }
                    catch (Exception ex)
                    {
                        // create a friendly error message
                        string message = string.Format("Error loading the job: {0}.  Ensure that the correct version of the job's assembly ({1}.dll) in the websites App_Code directory. \n\n\n\n{2}", job.Name, job.Assembly, ex.Message);
                        job.LastStatusMessage = message;
                        job.LastStatus        = "Error Loading Job";

                        jobService.Save(job, null);
                    }
                }

                // set up the listener to report back from jobs as they complete
                sched.ListenerManager.AddJobListener(new RockJobListener(), EverythingMatcher <JobKey> .AllJobs());

                // start the scheduler
                sched.Start();
            }

            // add call back to keep IIS process awake at night and to provide a timer for the queued transactions
            AddCallBack();

            RegisterFilters(GlobalConfiguration.Configuration.Filters);

            RegisterRoutes(RouteTable.Routes);

            Rock.Security.Authorization.Load();

            AddEventHandlers();

            new EntityTypeService().RegisterEntityTypes(Server.MapPath("~"));
            new FieldTypeService().RegisterFieldTypes(Server.MapPath("~"));

            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }