LibAICORE 1.2.15-alpha-g18195b9aa6
Copyright (C) Coredata Inc - All Rights Reserved - The source code of this package is proprietary and confidential, and unauthorized copies of the source code / debug builds / PDB files 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
- streaming, 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
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 - try to keep API calls in appsettings in case they need to be moved to this later, since the JSON system will be the primary way customers can change the software configuration
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();
LibAICORE also has a settings format. Here is the usual block that needs to be added to appsettings:
{
"AICORE": {
"apiUrls": {
"ExecuteCMD": "https://aicoreio.coredata.ca/ExecuteCMD/ExecuteCMD/ExecuteCMD",
"CSVtoDB": "https://aicoreio.coredata.ca/CSVToDB/CSVToDB",
.
.
.
},
"keycloak": {
"realm": "coredata",
"authServerUrl": "https://auth.aicore.ca/",
"sslRequired": "external",
"resource": "aicore",
"verifyTokenAudience": false,
"credentials": {
"secret": "..."
}
},
"user": {
"username": "...",
"password": "..."
},
"tokenMode": "ForwardFromAPICall"
}
}
Note the keycloak and user sections not needed if the token mode is ForwardFromAPICall
, which means if any RunCorejob is called from an API controller, LibAICORE just gets the token from the call to the controller.
If the token mode is set to LoginAsUser
instead, LibAICORE will automatically log in using the credentials under "user" and the keycloak settings under "keycloak" before running the corejob.
No packages depend on LibAICORE.
.NET 6.0
- FluentResults (>= 3.16.0)
- Microsoft.IdentityModel.JsonWebTokens (>= 8.1.2)
- Newtonsoft.Json (>= 13.0.3)
- RestSharp (>= 112.1.0)