This repository has been archived by the owner on Mar 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking changes in command line arguments;
- Loading branch information
Showing
10 changed files
with
171 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace ConsoleClient { | ||
static class EnvManager { | ||
const string ArgStart = "-"; | ||
const string ArgDelimiter = "="; | ||
|
||
public static string FindArgumentValue(string argName) { | ||
return FindArgumentValues(argName).FirstOrDefault(); | ||
} | ||
|
||
public static List<string> FindArgumentValues(string argName) { | ||
var args = Environment.GetCommandLineArgs(); | ||
var argItems = args.Where(a => a.StartsWith(ArgStart + argName + ArgDelimiter)); | ||
var argValues = new List<string>(); | ||
foreach ( var argItem in argItems ) { | ||
var parts = argItem.Split(ArgDelimiter); | ||
if ( parts.Length >= 2 ) { | ||
argValues.Add(parts[1]); | ||
} | ||
} | ||
return argValues; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Logging; | ||
using Server.Services; | ||
|
||
namespace ConsoleClient { | ||
static class ServicesExtensions { | ||
static bool TryParseTimeSpan(string str, string ending, Func<int, TimeSpan> generator, out TimeSpan span) { | ||
if ( str.EndsWith(ending) ) { | ||
var valueStr = str.Substring(0, str.Length - ending.Length); | ||
if ( int.TryParse(valueStr, out int value) ) { | ||
span = generator(value); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
static bool TryParseTimeSpan(string str, out TimeSpan span) { | ||
return | ||
TryParseTimeSpan(str, "h", t => TimeSpan.FromHours(t), out span) || | ||
TryParseTimeSpan(str, "m", t => TimeSpan.FromMinutes(t), out span) || | ||
TryParseTimeSpan(str, "s", t => TimeSpan.FromSeconds(t), out span); | ||
} | ||
|
||
public static void TryAddNotificationService(this List<IService> services, ILoggerFactory loggerFactory) { | ||
var logger = loggerFactory.CreateLogger("ServicesExtensions"); | ||
var notifyValue = EnvManager.FindArgumentValue("notify"); | ||
if ( notifyValue != null ) { | ||
logger.LogDebug($"TryAddNotificationService: Found notify arg value: '{notifyValue}'"); | ||
if ( TryParseTimeSpan(notifyValue, out TimeSpan span) ) { | ||
logger.LogDebug($"TryAddNotificationService: Parsed TimeSpan: '{span}'"); | ||
var service = new NotificationService(loggerFactory, span); | ||
services.Add(service); | ||
return; | ||
} | ||
} | ||
logger.LogDebug("TryAddNotificationService: Notify arg isn't found."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using Server.Runtime; | ||
|
||
namespace Server.Services { | ||
interface IContextService { | ||
RequestContext Context { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.Extensions.Logging; | ||
using Server.BuildConfig; | ||
using Server.Runtime; | ||
|
||
namespace Server.Services { | ||
public class NotificationService : IService { | ||
ILogger _logger; | ||
|
||
BuildServer _server; | ||
BuildProcess _process; | ||
|
||
TimeSpan _minInterval; | ||
DateTime _lastMessageTime; | ||
|
||
public NotificationService(ILoggerFactory loggerFactory, TimeSpan minInterval) { | ||
_logger = loggerFactory.CreateLogger<NotificationService>(); | ||
_minInterval = minInterval; | ||
} | ||
|
||
public bool TryInit(BuildServer server, Project project) { | ||
_server = server; | ||
_server.OnInitBuild += OnInitBuild; | ||
return true; | ||
} | ||
|
||
private void OnInitBuild(RequestContext _, BuildProcess process) { | ||
_logger.LogDebug("OnInitBuild"); | ||
_process = process; | ||
process.TaskStarted += OnTaskStarted; | ||
process.BuildDone += OnBuildDone; | ||
_lastMessageTime = DateTime.Now; | ||
} | ||
|
||
private void OnTaskStarted(BuildTask task) { | ||
if ( _process != null ) { | ||
_logger.LogDebug($"OnTaskDone: {_process.Name}: {task.Node.Name}"); | ||
var now = DateTime.Now; | ||
if ( now > _lastMessageTime.Add(_minInterval) ) { | ||
CallStatus(); | ||
_lastMessageTime = now; | ||
} | ||
} | ||
} | ||
|
||
void CallStatus() { | ||
var viewServices =_server.Services.FindAll(service => service is IContextService).Select(s => s as IContextService); | ||
_logger.LogDebug($"CallStatus: ViewServices: {viewServices.Count()}"); | ||
foreach ( var service in viewServices ) { | ||
_logger.LogDebug($"CallStatus: service: {service.GetType().Name}"); | ||
if ( (service != null) && (service.Context != null) ) { | ||
_logger.LogDebug($"CallStatus: It is IContextService with Context: '{service.Context.Name}'"); | ||
_server.RequestStatus(service.Context); | ||
} | ||
} | ||
} | ||
|
||
private void OnBuildDone() { | ||
if ( _process != null ) { | ||
_logger.LogDebug($"OnBuildDone: {_process.Name}: {_process.IsSuccess}"); | ||
_process.BuildDone -= OnBuildDone; | ||
_process.TaskStarted -= OnTaskStarted; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters