NetCoreUsefullEndpoints-part 10- adding LongRunningTasks

In the Nuget NetCoreUsefullEndpoints I have already an  endpoint to shutdown  –  see http://msprogrammer.serviciipeweb.ro/2023/02/20/netcoreusefullendpoints-part-9-adding-endpoints-for-shutdown/ .

However, this means that there are no ( or some … )  long running operations that are running … How to identify those ?

For the starting point , I was thinking at something simple, like an IDisposable that knows when the Long Running operation is finished

[HttpGet(Name = “GetWeatherForecast”)]
         public async Task<WeatherForecast[]> Get()
         {
             using var lr = UsefullExtensions.UsefullExtensions.AddLRTS(“weather”+DateTime.UtcNow.Ticks);
             await Task.Delay(5000);
             return Enumerable.Range(1, 5).Select(index => new WeatherForecast
             {
                 Date = DateTime.Now.AddDays(index),
                 TemperatureC = Random.Shared.Next(-20, 55),
                 Summary = Summaries[Random.Shared.Next(Summaries.Length)]
             })
             .ToArray();
         }

The implementation is very simple for adding:

internal static Dictionary<string, LongRunningTask> lrts = new();
public static LongRunningTask AddLRTS(string id, string? name = null)
{
     if(lrts.ContainsKey(id))
     {
         lrts[id].Dispose();
     }
     lrts.Add(id,new LongRunningTask(id, name ?? id));
     return lrts[id];
}

And for removing itself:

public record LongRunningTask(string id, string? name = “”) : IDisposable
{
     public void Dispose()
     {
         UsefullExtensions.lrts.Remove(id);
         GC.SuppressFinalize(this);
     }

}

Also, an endpoint will be registered for the user know what are the operations

var rh = route.MapGet(“api/usefull/LongRunningTasks/”,
             (HttpContext httpContext) =>
             {
                 var data=lrts.Select(it=>new { it.Key, it.Value.name }).ToArray();
                 return data;
             });

        var rhCount = route.MapGet(“api/usefull/LongRunningTasks/Count”,
             (HttpContext httpContext) =>
             {
                 return lrts.LongCount();
             });

And that will be to register and found what are the long running tasks in ASP.NET Core

NetCoreUsefullEndpoints-part 9- adding endpoints for shutdown

In my NuGet NetCoreUsefullEndpoints package  I want to can shutdown the application . Could be hard way, like calling Environment.Exit or the easy way , like calling a cancellation token. Also, what should happen with all the requests that are coming ? ( And I do not want to mention long running tasks – I do not found a useful registration / un-registration for those)

So I come with the following implementation for forced exit

//IEndpointRouteBuilder

var rhForced = route.MapPost(“api/usefull/shutdownForced/{id:int}”,
     (int id) =>
     {
         Environment.Exit(id);
     });

For the shutdown that is requested , I come with 3 modifications:

1.In Program.cs , I put a Middleware for not serving the HTTP

builder.Services.AddSingleton<MiddlewareShutdown>();

app.UseMiddleware<MiddlewareShutdown>();

await app.RunAsync(UsefullExtensions.UsefullExtensions.cts.Token);

2.  Register the routes in order to use the cancellation token

//IEndpointRouteBuilder

var rhSec = route.MapPost(“api/usefull/shutdownAfter/{seconds}”,
             (HttpContext httpContext, int seconds) =>
             {
                 RequestedShutdownAt = DateTime.UtcNow;
                 var h = cts.Token.GetHashCode();
                 cts?.CancelAfter(Math.Abs(seconds)*1000);
                 return h;

            });

3. Use a middleware to return 418 Status Code if the application is shutting down

public class MiddlewareShutdown : IMiddleware
{
     public async Task InvokeAsync(HttpContext context, RequestDelegate next)
     {
         if (UsefullExtensions.RequestedShutdownAt != null)
         {
             context.Response.StatusCode = 418;
             await context.Response.WriteAsync(“Service is stopping at ” + UsefullExtensions.RequestedShutdownAt!.Value.ToString(“s”));
             return;
         }
         await next(context);
         return;
     }
}

And with those you can shutdown gracefully any ASP.NET Core application ( without long running tasks!)

[ADCES]React – first steps(again) and SRE and .NET

Presentation 1: React( first steps) – again
Presenter: Andrei Ignat, http://msprogrammer.serviciipeweb.ro/
Descriere : O sa fie atinse urmatoarele concepte prin exemple practice:

1. React ca librarie, nu ca framework
2. Create-react-app – cu typescript si modificarile care trebuie aduse
3. Componente si Utilizarea de Hooks
4. Comunicarea intre componente de React – de la parinte la copil si intre copii diferiti cu RxJS
4. Utilizarea de librarii: React Router, MUI

Presentation 2: What a SRE wants and how .NET provides
Descriere: We will answer to https://oschvr.com/posts/what-id-like-as-sre/ with examples and tests 😉

Va astept la https://www.meetup.com/bucharest-a-d-c-e-s-meetup/events/291291505/

NetCoreUsefullEndpoints-part 8- adding start date

In my NuGet NetCoreUsefullEndpoints package  I have had already registered the actual date as

var rh = route.MapGet(“api/usefull/dateUTC”, (HttpContext httpContext) =>
            {
                return Results.Ok(DateTime.UtcNow);
            });

Now I want to register also the start date – the date where the application has been started.

1. How to do this  ?

2. What will be the route ?

For 1 can be a static constructor or, better , the singleton in C# :

private static DateTime startDateUTC = DateTime.UtcNow;

For 2 it is more complicated

My solution ( just break compatibility, since I have not a v1 and v2) was the following

var rh = route.MapGet(“api/usefull/date/startUTC”, (HttpContext httpContext) =>
{
     return Results.Ok(startDateUTC);
});

var rhUTC = route.MapGet(“api/usefull/date/nowUTC/”,
     (HttpContext httpContext) =>
     {
         return TypedResults.Ok(DateTime.UtcNow);
     });

rhUTC.AddDefault(corsPolicy, authorization);

So now I have same routing api/usefull/date

Nuget packages and Github repositories

You can see the Github repositories that you have worked  last year by going to https://docs.github.com/en/graphql/overview/explorer and entering

query ContributionGraph {
   user(login: “ignatandrei”) {
     contributionsCollection(
       from: “2022-01-01T00:00:00+00:00”
       to: “2022-12-31T00:00:00+00:00”
     ) {
       commitContributionsByRepository(maxRepositories:100){
         repository{
           nameWithOwner
           url
           updatedAt
         }
       }     
     }
   }
}

Mine are

ignatandrei/TILT
ignatandrei/BlocklyAutomation
ignatandrei/QueryViewer
ignatandrei/RSCG_AMS
ignatandrei/rxDemo
ignatandrei/NetCoreUsefullEndpoints
ignatandrei/Presentations
ignatandrei/NETCoreBlockly
ignatandrei/FunctionsDI
ignatandrei/GeneratorOfHelp
ignatandrei/MicroservicesPortChooser
ignatandrei/AOP_With_Roslyn
ignatandrei/RSCG_TimeBombComment
ignatandrei/RSCG_Examples

And those  are the NuGet packages:

programmerall
GOH
QueryGenerator
RSCG_FunctionsWithDI_Base
RSCG_FunctionsWithDI
AMS_Base
AMSWebAPI
RSCG_AMS
AOPMethodsGenerator
AOPMethodsCommon
RSCG_TimeBombComment
NetCoreBlockly

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.