1
0
mirror of https://github.com/bitwarden/directory-connector synced 2025-12-10 05:13:17 +00:00

service sync timer. control service from console

This commit is contained in:
Kyle Spearrin
2017-05-16 12:43:07 -04:00
parent de0acb46ea
commit 10a98e4a12
4 changed files with 200 additions and 4 deletions

View File

@@ -1,4 +1,6 @@
using System;
using Bit.Core.Services;
using Bit.Core.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@@ -6,7 +8,7 @@ using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Service
{
@@ -15,6 +17,7 @@ namespace Service
{
private IContainer _components;
private EventLog _eventLog;
private Timer _timer;
public Service()
{
@@ -44,6 +47,9 @@ namespace Service
_components?.Dispose();
_components = null;
_timer?.Dispose();
_timer = null;
}
base.Dispose(disposing);
@@ -52,11 +58,53 @@ namespace Service
protected override void OnStart(string[] args)
{
_eventLog.WriteEntry("Service started!", EventLogEntryType.Information);
if(SettingsService.Instance.Server == null)
{
_eventLog.WriteEntry("Server not configured.", EventLogEntryType.Error);
return;
}
if(SettingsService.Instance.Sync == null)
{
_eventLog.WriteEntry("Sync not configured.", EventLogEntryType.Error);
return;
}
if(!AuthService.Instance.Authenticated || !AuthService.Instance.OrganizationSet)
{
_eventLog.WriteEntry("Not authenticated with proper organization set.", EventLogEntryType.Error);
return;
}
var timerDelegate = new TimerCallback(Callback);
_timer = new Timer(timerDelegate, null, 1000, 60 * 1000);
}
protected override void OnStop()
{
_eventLog.WriteEntry("Service stopped!", EventLogEntryType.Information);
}
private void Callback(object stateInfo)
{
try
{
var result = Sync.SyncAllAsync(false, true).GetAwaiter().GetResult();
if(result.Success)
{
_eventLog.WriteEntry($"Synced {result.Groups.Count} groups, {result.Users.Count} users.",
EventLogEntryType.SuccessAudit);
}
else
{
_eventLog.WriteEntry($"Sync failed: {result.ErrorMessage}", EventLogEntryType.FailureAudit);
}
}
catch(ApplicationException e)
{
_eventLog.WriteEntry($"Sync exception: {e.Message}", EventLogEntryType.Error);
}
}
}
}