private void ServiceThreadMain() { try { string currentUser = ScalarPlatform.Instance.GetCurrentUser(); EventMetadata metadata = new EventMetadata(); metadata.Add("Version", ProcessHelper.GetCurrentProcessVersion()); metadata.Add(nameof(currentUser), currentUser); this.tracer.RelatedEvent(EventLevel.Informational, $"ScalarService_{nameof(this.ServiceThreadMain)}", metadata); if (int.TryParse(currentUser, out int sessionId)) { try { this.maintenanceTaskScheduler = new MaintenanceTaskScheduler( this.tracer, new PhysicalFileSystem(), new MacScalarVerbRunner(this.tracer), this.repoRegistry); // On Mac, there is no separate session Id. currentUser is used as sessionId this.maintenanceTaskScheduler.RegisterUser(new UserAndSession(currentUser, sessionId)); this.maintenanceTaskScheduler.ScheduleRecurringTasks(); } catch (Exception e) { this.tracer.RelatedError(CreateEventMetadata(e), "Failed to start maintenance scheduler"); } } else { EventMetadata errorMetadata = CreateEventMetadata(); errorMetadata.Add(nameof(currentUser), currentUser); this.tracer.RelatedError( errorMetadata, $"{nameof(this.ServiceThreadMain)}: Failed to parse current user as int."); } this.serviceStopped.WaitOne(); this.serviceStopped.Dispose(); this.serviceStopped = null; if (this.maintenanceTaskScheduler != null) { this.maintenanceTaskScheduler.Dispose(); this.maintenanceTaskScheduler = null; } } catch (Exception e) { this.LogExceptionAndExit(e, nameof(this.ServiceThreadMain)); } }
public void StopRunning() { if (this.serviceStopped == null) { return; } try { if (this.productUpgradeTimer != null) { this.productUpgradeTimer.Stop(); } if (this.tracer != null) { this.tracer.RelatedInfo("Stopping"); } if (this.serviceStopped != null) { this.serviceStopped.Set(); } if (this.serviceThread != null) { this.serviceThread.Join(); this.serviceThread = null; if (this.maintenanceTaskScheduler != null) { this.maintenanceTaskScheduler.Dispose(); this.maintenanceTaskScheduler = null; } if (this.serviceStopped != null) { this.serviceStopped.Dispose(); this.serviceStopped = null; } } } catch (Exception e) { this.LogExceptionAndExit(e, nameof(this.StopRunning)); } }
public void Run() { try { EventMetadata metadata = new EventMetadata(); metadata.Add("Version", ProcessHelper.GetCurrentProcessVersion()); this.tracer.RelatedEvent(EventLevel.Informational, $"ScalarService_{nameof(this.Run)}", metadata); PhysicalFileSystem fileSystem = new PhysicalFileSystem(); this.repoRegistry = new ScalarRepoRegistry( this.tracer, fileSystem, this.repoRegistryLocation); this.maintenanceTaskScheduler = new MaintenanceTaskScheduler(this.tracer, fileSystem, new WindowsScalarVerbRunner(this.tracer), this.repoRegistry); this.AssignCurrentLoggedOnUser(); this.maintenanceTaskScheduler.ScheduleRecurringTasks(); this.requestHandler = new RequestHandler(this.tracer, EtwArea); string pipeName = ScalarPlatform.Instance.GetScalarServiceNamedPipeName(this.serviceName); this.tracer.RelatedInfo("Starting pipe server with name: " + pipeName); using (NamedPipeServer pipeServer = NamedPipeServer.StartNewServer( pipeName, this.tracer, this.requestHandler.HandleRequest)) { this.productUpgradeTimer.Start(); this.serviceStopped.WaitOne(); } } catch (Exception e) { this.LogExceptionAndExit(e, nameof(this.Run)); } }