Skip to content

Commit

Permalink
Add scheduler feature
Browse files Browse the repository at this point in the history
  • Loading branch information
guibranco committed Sep 5, 2020
1 parent c320d3f commit 2c2bfa3
Show file tree
Hide file tree
Showing 17 changed files with 1,200 additions and 58 deletions.
27 changes: 27 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Tests/CrispyWaffle.Tests/bin/Debug/netcoreapp3.1/CrispyWaffle.Tests.dll",
"args": [],
"cwd": "${workspaceFolder}/Tests/CrispyWaffle.Tests",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
42 changes: 42 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Tests/CrispyWaffle.Tests/CrispyWaffle.Tests.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Tests/CrispyWaffle.Tests/CrispyWaffle.Tests.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/Tests/CrispyWaffle.Tests/CrispyWaffle.Tests.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
1 change: 1 addition & 0 deletions .wakatime-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Crispy Waffle
134 changes: 88 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,94 +79,136 @@ Install-Package CrispyWaffle.Log4Net

## Usage

### Console application
### Logging

A simple `console application` with simple logging example:

```cs

static void Main(string[] args)
{
ServiceLocator.Register<IConsoleLogAdapter, StandardConsoleLogAdapter>(LifeStyle.SINGLETON);
ServiceLocator.Register<IExceptionHandler, NullExceptionHandler>(LifeStyle.SINGLETON);
LogConsumer.AddProvider<ConsoleLogProvider>();
LogConsumer.Info("Hello world Crispy Waffle");
LogConsumer.Debug("Current time: {0:hh:mm:ss}", DateTime.Now);
LogConsumer.Warning("Press any key to close the program!");
Console.ReadKey();
ServiceLocator.Register<IConsoleLogAdapter, StandardConsoleLogAdapter>(LifeStyle.SINGLETON);
ServiceLocator.Register<IExceptionHandler, NullExceptionHandler>(LifeStyle.SINGLETON);
LogConsumer.AddProvider<ConsoleLogProvider>();
LogConsumer.Info("Hello world Crispy Waffle");
LogConsumer.Debug("Current time: {0:hh:mm:ss}", DateTime.Now);
LogConsumer.Warning("Press any key to close the program!");
Console.ReadKey();
}
```

### Events

An example using `Events`\`EventsHandlers`

```cs

//The event class.
public class SomeEvent : IEvent
{
public SomeEvent(string data)
{
Id = Guid.NewGuid();
Date = DateTime.Now;
Data = data;
}
public SomeEvent(string data)
{
Id = Guid.NewGuid();
Date = DateTime.Now;
Data = data;
}

public Guid Id { get; }
public Guid Id { get; }

public string Data { get; }
public string Data { get; }

public DateTime Date { get; }
public DateTime Date { get; }
}

//The event handler class. Each event can be handled by N event handlers.
public class SomeEventHandler : IEventHandler<SomeEvent>
{
//constructor of the class, with dependencies...
//constructor of the class, with dependencies...
public void Handle(SomeEvent args)
{
LogConsumer.Warning("Event 'SomeEvent' handled by 'SomeEventHandler'. Event Id: {0}", args.Id);
//do any other processing stuff...
}
public void Handle(SomeEvent args)
{
LogConsumer.Warning("Event 'SomeEvent' handled by 'SomeEventHandler'. Event Id: {0}", args.Id);
//do any other processing stuff...
}

}

public class AnotherSomeEventHandler : IEventHandler<SomeEvent>
{
//constructor of the class, with dependencies...
public void Handle(SomeEvent args)
{
LogConsumer.Warning("Event 'SomeEvent' handled by 'AnotherSomeEventHandler'. Event Id: {0}", args.Id);
//someOtherDependency.DoSomeStuffWithEvent(args.Id, args.Data);
//do any other processing stuff...
}
//constructor of the class, with dependencies...
public void Handle(SomeEvent args)
{
LogConsumer.Warning("Event 'SomeEvent' handled by 'AnotherSomeEventHandler'. Event Id: {0}", args.Id);
//someOtherDependency.DoSomeStuffWithEvent(args.Id, args.Data);
//do any other processing stuff...
}
}

// Program entry point
public static class Program
{
public static void Main(string[] args)
{
//Initialize the dependencies with ServiceLocator.
//Initialize log providers/adapters
//...
var evt = new SomeEvent ("README.md test data");
EventsConsumer.Raise(evt);
}
{
public static void Main(string[] args)
{
//Initialize the dependencies with ServiceLocator.
//Initialize log providers/adapters
//...
var evt = new SomeEvent ("README.md test data");
EventsConsumer.Raise(evt);
}
}
```

### Scheduled jobs (using CRON expressions)

Using `cron` expression to schedule tasks/jobs inside a program.

```cs
public static class Program
{
public static void Main(string[] args)
{
var exampleObj = new SomeClass();
exampleObj.Counter = 10;

var jobManager = new JobManager();
jobManager.AddJob(new JobRunner("* * * * *", () => { exampleObj.Counter++; }));
jobManager.Start();

Thread.Sleep(120 * 1000); //waits 2 minutes
jobManager.Stop(); //stops the manager, so no more execution runs.
if(exampleObj.Counter == 2)
{
LogConsumer.Warning("Example job runned for 2 times!");
}

}

internal class SomeClass
{
public int Counter { get; set; }
}

}

```

---

## Changelog

2020-09-05 - Version 2.2 - [@guibranco](https://github.com/guibranco)

- Add scheduler feature.

2020-09-04 - Version 2.1 - [@guibranco](https://github.com/guibranco)

- Add the Configuration project and package.
Expand Down
17 changes: 6 additions & 11 deletions Src/CrispyWaffle/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,12 @@ public static bool IsValidJson(this string jsonRaw)
[Pure]
public static IEnumerable<string> SplitBy(this string str, int chunkLength)
{
if (!ValidateSplitBy(str, chunkLength))
if (chunkLength < 1)
{
throw new ArgumentException(@"Invalid chuck length", nameof(chunkLength));
}

if (string.IsNullOrWhiteSpace(str))
{
yield return string.Empty;
yield break;
Expand All @@ -474,16 +479,6 @@ public static IEnumerable<string> SplitBy(this string str, int chunkLength)
}
}

private static bool ValidateSplitBy(string str, int chunkLength)
{
if (chunkLength < 1)
{
throw new ArgumentException(@"Invalid chuck length", nameof(chunkLength));
}

return !string.IsNullOrWhiteSpace(str);
}

/// <summary>
/// Strips the tags.
/// </summary>
Expand Down
Loading

0 comments on commit 2c2bfa3

Please sign in to comment.