LibAICORE 1.1.39-alpha-g0ada0164a0
Copyright (C) Coredata Inc - All Rights Reserved - The source code of this package is proprietary and confidential, and unauthorized copies of the source code are strictly prohibited
A corejob is a task that is:
- asynchronous, may take seconds, minutes, or hours
 - transient, variables not in db/files/etc are deleted on finish
 - non-importing, files may be GB or TB so files are edited one row at a time (do not import everything into RAM!!)
 - authenticated, users must be checked for permissions before every action - this is done with keycloak (auth.aicore.ca)
 - private + sandboxed, users should have absolutely no access to any information (files, DB, corejobs) from other companies (customers) - any files outside of /AICORE/Customers/[company] should be inaccessible
 - developer maintained, a developer at coredata is expected to modify corejobs according to logic requirements
 
In an API controller, corejobs can be called either with POST, which has a chance of timing out and only returns log messages at the end of execution in an ActionResult - or with SSE, which returns each log message asynchronously with server-sent events as it is made, and will not time out as long as the API client/web browser has keepalive enabled (?). This is why logs are async.
This library was made for ASP.NET Core 6 using reflection and dependency injection - please read up on both if you want to edit the library
To call corejobs made with the AICORE low-code JSON system, a function ExecuteCMD has been provided
Full example:
public class TestController : ControllerBase
{
    private readonly CorejobRegistry _corejobs;
    public TestController(CorejobRegistry corejobs)
    {
        _corejobs = corejobs;
    }
	
    [HttpPost]
    [Route("test_registry")]
    [Produces("application/json")]
    public async Task Test()
    {
        await _corejobs.RunCorejobPost("test_registry", null);
    }
	
    [HttpPost]
    [Route("test_registry_sse")]
    public async Task TestSSE()
    {
        await _corejobs.RunCorejobSSE("test_registry", null);
    }
}
public class CorejobRegistryTest : CorejobRegistryBase
{
    // Has access to all services a controller does:
    private readonly ExampleDBContext _db;
    public CorejobRegistryTest(IHttpContextAccessor httpContextAccessor, ILogger<CorejobRegistryTest> logger, IConfiguration configuration, ExampleDBContext db) : base(httpContextAccessor, logger, configuration)
    {
        _db = db;
    }
    [Corejob("test_registry")]
    [Customer("coredata")]
    public async Task<IResultBase> TestRegistry()
    {
        string? username = GetUserName();
        var user = await _db.Users.Where(u => u.username == username).FirstOrDefaultAsync();
        string customer_folder = await GetCustomerRoot();
        await LogInformation("Current user: " + username + " (" + user.email + ")    Customer folder: " + customer_folder);
        // Low-code JSON corejob
        await ExecuteCMD("bf9fb9b6-8c42-495f-a6e6-2da9ed4a8909", new() {
            ["date"] = DateTime.Now.ToString()
        });
        string? customer = GetCustomerName();
        return Result.Ok().WithSuccess("Successfully ran test_registry for customer " + customer);
        // Any exceptions will automatically be caught and replaced with a Result.Fail() with the exception message; no try/catch needed
    }
}
Remember to add this line in Program.cs:
builder.Services.AddCorejobs();
No packages depend on LibAICORE.
.NET 6.0
- FluentResults (>= 3.15.2)
 - Keycloak.AuthServices.Authentication (>= 1.6.0)
 - Keycloak.AuthServices.Authorization (>= 1.6.0)
 - Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
 - Newtonsoft.Json (>= 13.0.3)
 - RestSharp (>= 110.2.0)