Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
Feature: NotificationService;
Browse files Browse the repository at this point in the history
Breaking changes in command line arguments;
  • Loading branch information
KonH committed Oct 13, 2017
1 parent 11f6735 commit 54dccb0
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 13 deletions.
27 changes: 27 additions & 0 deletions ConsoleClient/EnvManager.cs
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;
}
}
}
12 changes: 7 additions & 5 deletions ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Server.Services;
using Server.Runtime;
using Microsoft.Extensions.Logging;
Expand All @@ -12,7 +11,9 @@ class Program {
static bool WithConsoleLog = false;

static void Main(string[] args) {
if (args.Length < 2) {
var serverName = EnvManager.FindArgumentValue("server");
var configPathes = EnvManager.FindArgumentValues("config");
if ( string.IsNullOrEmpty(serverName) || (configPathes.Count < 1) ) {
Console.WriteLine("You need to provide serverName and at least one config path!");
Console.WriteLine("Closing...");
return;
Expand All @@ -24,18 +25,19 @@ class Program {
}
loggerFactory.AddFile("log.txt", false);

var serverName = args[0];
var serverArgs = args.Skip(1).ToArray();
var consoleService = new ConsoleService(loggerFactory);

var services = new List<IService> {
consoleService,
new SlackService(loggerFactory),
new StatService("stats.xml" , loggerFactory, true)
};
services.TryAddNotificationService(loggerFactory);

var commandFactory = new CommandFactory(loggerFactory);
var server = new BuildServer(commandFactory, loggerFactory, serverName);
string startUpError = null;
if (!server.TryInitialize(out startUpError, services, serverArgs)) {
if (!server.TryInitialize(out startUpError, services, configPathes)) {
Console.WriteLine(startUpError);
Console.WriteLine("Closing...");
return;
Expand Down
41 changes: 41 additions & 0 deletions ConsoleClient/ServicesExtensions.cs
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.");
}
}
}
2 changes: 1 addition & 1 deletion Server/BuildConfig/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Project {
Keys = keys;
}

public static Project Load(LoggerFactory loggerFactory, string serverName, string[] pathes) {
public static Project Load(LoggerFactory loggerFactory, string serverName, List<string> pathes) {
var logger = loggerFactory.CreateLogger<Project>();
var builder = new ConfigurationBuilder();
foreach (var path in pathes) {
Expand Down
4 changes: 2 additions & 2 deletions Server/Runtime/BuildServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ public class BuildServer {
}
}

public bool TryInitialize(out string errorMessage, List<IService> services, params string[] projectPathes) {
public bool TryInitialize(out string errorMessage, List<IService> services, List<string> projectPathes) {
_logger.LogDebug(
$"TryInitialize: services: {services.Count()}, pathes: {projectPathes.Length}");
$"TryInitialize: services: {services.Count()}, pathes: {projectPathes.Count}");
try {
Project = Project.Load(_loggerFactory, Name, projectPathes);
} catch (Exception e) {
Expand Down
2 changes: 1 addition & 1 deletion Server/Server.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.22.44.1</Version>
<Version>0.23.45.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
Expand Down
10 changes: 8 additions & 2 deletions Server/Services/ConsoleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
using Microsoft.Extensions.Logging;

namespace Server.Services {
public class ConsoleService:IService {
public class ConsoleService : IService, IContextService {

readonly RequestContext _context = new RequestContext("Console");

public RequestContext Context {
get {
return _context;
}
}

LoggerFactory _loggerFactory;

ConsoleServerController _controller;
ConsoleServerView _view;

public ConsoleService(LoggerFactory loggerFactory) {
_loggerFactory = loggerFactory;
}
Expand Down
7 changes: 7 additions & 0 deletions Server/Services/IContextService.cs
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; }
}
}
67 changes: 67 additions & 0 deletions Server/Services/NotificationService.cs
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;
}
}
}
}
12 changes: 10 additions & 2 deletions Server/Services/SlackService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@
using SlackBotNet.Messages;

namespace Server.Services {
public class SlackService:IService {
public class SlackService : IService, IContextService {

public readonly RequestContext Context = new RequestContext("Slack");
readonly RequestContext _context = new RequestContext("Slack");

public RequestContext Context {
get {
return _context;
}
}

public event Action<string> OnMessage;

public SlackServerController Controller { get; private set; }
public SlackServerView View { get; private set; }



LoggerFactory _loggerFactory;
ILogger _logger;

Expand Down

0 comments on commit 54dccb0

Please sign in to comment.