Skip to content

Commit

Permalink
apps(tasks): add tasks to demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Oct 18, 2023
1 parent b2baabb commit 0f16657
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/Blazor/MudBlazorUnited/MudBlazorUnited.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ProjectReference Include="..\..\..\src\Sitko.Core.Storage.Metadata.Postgres\Sitko.Core.Storage.Metadata.Postgres.csproj" />
<ProjectReference Include="..\..\..\src\Sitko.Core.Storage.Remote.Server\Sitko.Core.Storage.Remote.Server.csproj" />
<ProjectReference Include="..\..\..\src\Sitko.Core.Storage.Remote\Sitko.Core.Storage.Remote.csproj" />
<ProjectReference Include="..\..\..\src\Sitko.Core.Tasks.Kafka\Sitko.Core.Tasks.Kafka.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 11 additions & 1 deletion apps/Blazor/MudBlazorUnited/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using FluentValidation;
using MudBlazorUnited.Components;
using MudBlazorUnited.Data;
using MudBlazorUnited.Tasks;
using MudBlazorUnited.Tasks.Demo;
using Sitko.Core.App.Localization;
using Sitko.Core.App.Web;
using Sitko.Core.Blazor.MudBlazor.Server;
Expand All @@ -11,6 +13,7 @@
using Sitko.Core.Storage.FileSystem;
using Sitko.Core.Storage.Metadata.Postgres;
using Sitko.Core.Storage.Remote;
using Sitko.Core.Tasks.Kafka;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -24,7 +27,14 @@
.AddFileSystemStorage<TestBlazorStorageOptions>()
.AddRemoteStorage<TestRemoteStorageOptions>()
.AddPostgresStorageMetadata<TestBlazorStorageOptions>()
.AddJsonLocalization();
.AddJsonLocalization()
.AddKafkaTasks<MudBlazorBaseTask, MudBlazorTasksDbContext>(options =>
{
options
.AddTask<LoggingTask, LoggingTaskConfig, LoggingTaskResult>("* * * * *")
.AddExecutorsFromAssemblyOf<MudBlazorBaseTask>();
}, true,
options => options.AutoApplyMigrations = true);

builder.Services.AddValidatorsFromAssemblyContaining<Program>();
builder.Services.Configure<MudLayoutOptions>(builder.Configuration.GetSection("MudLayout"));
Expand Down
14 changes: 14 additions & 0 deletions apps/Blazor/MudBlazorUnited/Tasks/Demo/LoggingTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Sitko.Core.Tasks;
using Sitko.Core.Tasks.Data.Entities;

namespace MudBlazorUnited.Tasks.Demo;

[Task("logging")]
public record LoggingTask : MudBlazorBaseTask<LoggingTaskConfig, LoggingTaskResult>;

public record LoggingTaskResult : BaseTaskResult;

public record LoggingTaskConfig : BaseTaskConfig
{
public Guid Id { get; set; }
}
21 changes: 21 additions & 0 deletions apps/Blazor/MudBlazorUnited/Tasks/Demo/LoggingTaskExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Elastic.Apm.Api;
using Sitko.Core.Repository;
using Sitko.Core.Tasks.Execution;

namespace MudBlazorUnited.Tasks.Demo;

[TaskExecutor("Loggers", 10)]
public class LoggingTaskExecutor : BaseTaskExecutor<LoggingTask, LoggingTaskConfig, LoggingTaskResult>
{
public LoggingTaskExecutor(ILogger<LoggingTaskExecutor> logger, IServiceScopeFactory serviceScopeFactory,
IRepository<LoggingTask, Guid> repository, ITracer? tracer = null) : base(logger, serviceScopeFactory,
repository, tracer)
{
}

protected override Task<LoggingTaskResult> ExecuteAsync(LoggingTask task, CancellationToken cancellationToken)
{
Logger.LogInformation("Run Logging Task: {Id}", task.Config.Id);
return Task.FromResult(new LoggingTaskResult());
}
}
12 changes: 12 additions & 0 deletions apps/Blazor/MudBlazorUnited/Tasks/Demo/LoggingTaskFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Sitko.Core.Tasks.Scheduling;

namespace MudBlazorUnited.Tasks.Demo;

public class LoggingTaskFactory : IBaseTaskFactory<LoggingTask>
{
public Task<LoggingTask[]> GetTasksAsync(CancellationToken cancellationToken)
{
var tasks = new[] { new LoggingTask { Config = new() { Id = Guid.NewGuid() }, Result = new() } };
return Task.FromResult(tasks);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using MudBlazorUnited.Tasks.Demo;

#nullable disable

namespace MudBlazorUnited.Tasks.Migrations
{
/// <inheritdoc />
public partial class Tasks : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Tasks",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Config = table.Column<LoggingTaskConfig>(type: "jsonb", nullable: true),
Result = table.Column<LoggingTaskResult>(type: "jsonb", nullable: true),
DateAdded = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
DateUpdated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
TaskStatus = table.Column<int>(type: "integer", nullable: false),
ExecuteDateStart = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
ExecuteDateEnd = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
Type = table.Column<string>(type: "character varying(21)", maxLength: 21, nullable: false),
ParentId = table.Column<Guid>(type: "uuid", nullable: true),
UserId = table.Column<string>(type: "text", nullable: true),
LastActivityDate = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Tasks", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Tasks");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MudBlazorUnited.Tasks;
using MudBlazorUnited.Tasks.Demo;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace MudBlazorUnited.Tasks.Migrations
{
[DbContext(typeof(MudBlazorTasksDbContext))]
partial class MudBlazorTasksDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.0-rc.2.23480.1")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("MudBlazorUnited.Tasks.MudBlazorBaseTask", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");

b.Property<DateTimeOffset>("DateAdded")
.HasColumnType("timestamp with time zone");

b.Property<DateTimeOffset>("DateUpdated")
.HasColumnType("timestamp with time zone");

b.Property<DateTimeOffset?>("ExecuteDateEnd")
.HasColumnType("timestamp with time zone");

b.Property<DateTimeOffset?>("ExecuteDateStart")
.HasColumnType("timestamp with time zone");

b.Property<DateTimeOffset?>("LastActivityDate")
.HasColumnType("timestamp with time zone");

b.Property<Guid?>("ParentId")
.HasColumnType("uuid");

b.Property<int>("TaskStatus")
.HasColumnType("integer");

b.Property<string>("Type")
.IsRequired()
.HasMaxLength(21)
.HasColumnType("character varying(21)");

b.Property<string>("UserId")
.HasColumnType("text");

b.HasKey("Id");

b.ToTable("Tasks", (string)null);

b.HasDiscriminator<string>("Type").HasValue("MudBlazorBaseTask");

b.UseTphMappingStrategy();
});

modelBuilder.Entity("MudBlazorUnited.Tasks.Demo.LoggingTask", b =>
{
b.HasBaseType("MudBlazorUnited.Tasks.MudBlazorBaseTask");

b.Property<LoggingTaskConfig>("Config")
.IsRequired()
.HasColumnType("jsonb")
.HasColumnName("Config");

b.Property<LoggingTaskResult>("Result")
.HasColumnType("jsonb")
.HasColumnName("Result");

b.ToTable("Tasks", (string)null);

b.HasDiscriminator().HasValue("LoggingTask");
});
#pragma warning restore 612, 618
}
}
}
12 changes: 12 additions & 0 deletions apps/Blazor/MudBlazorUnited/Tasks/MudBlazorBaseTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Sitko.Core.Tasks.Data.Entities;

namespace MudBlazorUnited.Tasks;

public abstract record MudBlazorBaseTask : BaseTask;

public abstract record MudBlazorBaseTask<TConfig, TResult> : MudBlazorBaseTask, IBaseTask<TConfig, TResult>
where TConfig : BaseTaskConfig, new() where TResult : BaseTaskResult
{
public TConfig Config { get; set; } = default!;
public TResult? Result { get; set; }
}
6 changes: 6 additions & 0 deletions apps/Blazor/MudBlazorUnited/Tasks/MudBlazorTasksDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Sitko.Core.Tasks.Data;

namespace MudBlazorUnited.Tasks;

public class MudBlazorTasksDbContext(DbContextOptions<MudBlazorTasksDbContext> options) : TasksDbContext<MudBlazorBaseTask>(options);
12 changes: 11 additions & 1 deletion apps/Blazor/MudBlazorUnited/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
{
"DetailedErrors": true,
"Serilog":{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Sitko.Core.Apps.Blazor.Forms": "Debug",
"Sitko.Core.Repository": "Debug"
}
}
},
"Kafka": {
"Brokers": [
"127.0.0.1:9092"
],
"Tasks": {
"MudBlazorBaseTask": {
"TasksTopic": "MudBlazorTasks"
}
}
}
}
20 changes: 19 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,30 @@ services:
volumes:
- "mc:/root/.mc"
vault:
image: "vault"
image: hashicorp/vault
environment:
- VAULT_DEV_ROOT_TOKEN_ID=root
cap_add:
- IPC_LOCK
ports:
- "8200:8200"
kafka:
image: docker.io/bitnami/kafka:3.5
ports:
- "9092:9092"
volumes:
- "kafka_data:/bitnami"
environment:
# KRaft settings
- KAFKA_CFG_NODE_ID=0
- KAFKA_CFG_PROCESS_ROLES=controller,broker
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=0@kafka:9093
# Listeners
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
- KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
- KAFKA_CFG_INTER_BROKER_LISTENER_NAME=PLAINTEXT
volumes:
mc:
kafka_data:

0 comments on commit 0f16657

Please sign in to comment.