/// <summary> /// Raises the proxy's AllFinished event. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> private void ProxyAllFinished(object sender, EventArgs e) { lock (this) { this.IsLoaded = false; this.proxy = null; this.eventSink = null; this.RaiseEvent(this.AllFinished, e); AppDomain.Unload(this.domain); } }
/// <summary> /// Loads the <see cref="AppDomain"/> to run the <see cref="JobRunner"/> in and starts the runner. /// </summary> private void LoadAndStartAppDomain() { if (!Path.IsPathRooted(this.BasePath)) { this.BasePath = Path.GetFullPath(this.BasePath); } AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationBase = this.BasePath; setup.ShadowCopyFiles = "true"; if (String.IsNullOrEmpty(this.ConfigurationFilePath)) { string webConfigPath = Path.Combine(this.BasePath, "Web.config"); if (File.Exists(webConfigPath)) { this.ConfigurationFilePath = webConfigPath; } } string binPath = Path.Combine(this.BasePath, "bin"); bool isWebApplication = (String.IsNullOrEmpty(this.ConfigurationFilePath) || (!String.IsNullOrEmpty(this.ConfigurationFilePath) && "Web.config".Equals(Path.GetFileName(this.ConfigurationFilePath), StringComparison.OrdinalIgnoreCase))) && Directory.Exists(binPath); if (isWebApplication) { setup.PrivateBinPath = "bin"; } if (!String.IsNullOrEmpty(this.ConfigurationFilePath)) { if (!Path.IsPathRooted(this.ConfigurationFilePath)) { this.ConfigurationFilePath = Path.GetFullPath(this.ConfigurationFilePath); } setup.ConfigurationFile = this.ConfigurationFilePath; } this.domain = AppDomain.CreateDomain("Blue Collar Job Runner", AppDomain.CurrentDomain.Evidence, setup); this.proxy = (JobRunnerProxy)this.domain.CreateInstanceAndUnwrap(typeof(JobRunnerProxy).Assembly.FullName, typeof(JobRunnerProxy).FullName); this.proxy.RunningJobsPersistencePath = this.RunningJobsPersistencePath; this.eventSink = new JobRunnerEventSink(); this.eventSink.AllFinished += new EventHandler(this.ProxyAllFinished); this.eventSink.CancelJob += new EventHandler<JobRecordEventArgs>(this.ProxyCancelJob); this.eventSink.DequeueJob += new EventHandler<JobRecordEventArgs>(this.ProxyDequeueJob); this.eventSink.Error += new EventHandler<JobErrorEventArgs>(this.ProxyError); this.eventSink.ExecuteScheduledJob += new EventHandler<JobRecordEventArgs>(this.ProxyExecuteScheduledJob); this.eventSink.FinishJob += new EventHandler<JobRecordEventArgs>(this.ProxyFinishJob); this.eventSink.RetryEnqueued += new EventHandler<JobRecordEventArgs>(this.ProxyRetryEnqueued); this.eventSink.TimeoutJob += new EventHandler<JobRecordEventArgs>(this.ProxyTimeoutJob); this.proxy.EventSink = this.eventSink; this.watchers.Add(this.CreateWatcher(this.BasePath, FileSystemWatcherMode.Directory, "*.dll")); if (isWebApplication) { this.watchers.Add(this.CreateWatcher(binPath, FileSystemWatcherMode.Directory, "*.dll")); } if (!String.IsNullOrEmpty(this.ConfigurationFilePath)) { this.watchers.Add(this.CreateWatcher(Path.GetDirectoryName(this.ConfigurationFilePath), FileSystemWatcherMode.IndividualFiles, Path.GetFileName(this.ConfigurationFilePath))); } this.proxy.StartRunner(); }