Page cover image

Hello Perigee!

Resources

Want to watch the intro to Perigee instead?
Completed Project Files

Our first application

Go ahead and create a new .NET 5/6+ console application. Open up Program.cs, and head to the first step to get started!

Installation and Project Setup

Step 1) The application starting point

Let's start with the basics. First let's create a new application in Program.cs. Depending on the version of .NET you started with, you'll see two different things when you open Program.cs

Net 6++

The .NET 6 ++ version looks like this:

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

//YOUR CODE HERE

If this is the case, delete Line 2 - Console.WriteLine("Hello, World!"); and start there.

Net 5 and lower

The .NET 5 and below versions looks like this:

class Program
{

    static void Main(string[] args)
    {
        //YOUR CODE HERE
    }
}

If this is the case, start coding on Line 6.

The application code

Now that we know where to start, here's a simple Perigee application.

//A fully managed perigee application! 
PerigeeApplication.ApplicationNoInit("FirstApp", 
(taskConfig) => { 

});

Let's look what what we have here in the constructor:

  • Line 2 - "FirstApp" - The name of the application, this can be whatever you like!

  • Line 3 - taskConfig block - This block is where we add any and all thread managed tasks.

Make sure you have everything included in your using statements by clicking the 💡 icon or press ctrl+. to find missing namespaces

using Microsoft.Extensions.Logging;
using Perigee;

Step 2) Adding thread managed code

This wouldn't be a hello tutorial without writing "Hello, World!", so let's add a CRON method to log Hello Perigee every 15 seconds!

The .AddCron method adds a new thread to the system. Each thread is managed by Perigee and is independent of every other thread. This is an important aspect of Perigee Application Design as it allows for individual thread management. Threads do not affect each other, and Perigee's internal thread management system has mechanisms in place to automatically restart downed threads.

//A fully managed perigee application! 
using Microsoft.Extensions.Logging;
using Perigee;

PerigeeApplication.ApplicationNoInit("FirstApp", (taskConfig) => {

    taskConfig.AddCRON("HelloWorld", "*/15 * * * * *", (ct, log) => {
        log.LogInformation("Hello Perigee from {appName}", taskConfig.AppName);
    });

});
  • Line 4 - We use fluent syntax to .AddCRON() - This method takes a name, CRON string, and a callback(cancelToken, ILogger).

  • Line 5 - We use the built in logger passed to us to log information in a templated format, passing in the name of the application to the logger.

Running the application produces a log new line every 15 seconds!

To close this application using graceful shutdown, press CTRL-C, it will start the safe shutdown procedure allowing the application to properly stop all running tasks before just exiting out.

Hello Perigee - Extended

My oh my, we logged something! 👍Exciting right? Let's take our first demo application a step further and watch for CSV files, read them in, and report the row and data counts.

Simply replace .AddCron() with the directory watcher instead, full code below:

PerigeeApplication.ApplicationNoInit("FirstApp", (taskConfig) => {

    taskConfig.AddDirectoryWatch("CSV", "C:\\Watch", "*.csv", SearchOption.TopDirectoryOnly, (ct, l, path) => {

        //Read the CSV
        var CSVData = CSVReader.ToDataTable(path, out var rRes);

        //Reprt on it
        l.LogInformation("Read CSV {file}[{encoding}]. Columns/Rows: {col}/{row}; Delimiter: {delChar}; Jagged? {jagged}",
            Path.GetFileName(path), rRes.FileEncoding.EncodingName, rRes.ColumnCount,
            CSVData.Rows.Count, rRes.FinalDelimiter, rRes.RowShifts.Count > 0 ? "YES" : "NO");

        //Remove it - OR let the failed folder policy kick in
        //File.Delete(path);

    }, policy: ThreadRegistry.DirectoryWatchFailurePolicy.MoveToFailedFolder);

});

Here's a sample.csv:

Name, Age, Email, Phone
John Doe, 30, [email protected], 123-456-7890
Jane Smith, 25, [email protected], 987-654-3210
Bob Johnson, 40, [email protected], 555-555-5555
Mary Lee, 28, [email protected], 111-222-3333
Hash Brown, 35, [email protected], 444-444-4444

Voila! You're now watching for CSV files, reading them in, and reporting on the number of rows and columns.

For an intro to Perigee we accomplished quite a lot.

  • We learned about threads - how they're added and independent of each other.

  • We learned about logging information to the available sinks.

  • CRON strings and how to use a CRON thread.

  • How to configure Perigee.

  • What "Graceful Shutdown" looks like.

  • We even got to see the CSV reader in action.

Let's hop over to the next section and continue!

Last updated