MicroBatchFramework – クラウドネイティブ時代のC#バッチフレームワーク



Cy#12MagicOnionC#CLI/Batch

[GitHub  Cysharp/MicroBatchFramework]

.NET CoreWindowsMacLinuxC#C#Cy#C#C#

CLI


C#使MicroBatchFramework
class Program
{
    static async Task Main(string[] args)
    {
        // エントリポイント。この1行でフレームワークのStartup設定は終わりです。
        await BatchHost.CreateDefaultBuilder().RunBatchEngineAsync<MyFirstBatch>(args);
    }
}

// 実際のバッチの定義
public class MyFirstBatch : BatchBase // BatchBaseを継承してもらって
{
    // void(同期)かTask(非同期)の戻り値が設定可能、パラメータはどんな型も自由(JSONからマッピングします)
    public void Hello(string name, int repeat = 3)
    { 
        for (int i = 0; i < repeat; i++)
        {
            this.Context.Logger.LogInformation($"Hello My Batch from {name}");
        }
    }
}

 `SampleApp.exe -name foo -repeat 5` 
// 引数に別名や説明をつけたり
public void Hello(
    [Option("n", "name of send user.")]string name,
    [Option("r", "repeat count.")]int repeat = 3)
{
    // ...omit
}

// サブコマンドの作成や、引数順番でもバインディングなどをサポート
[Command("escape")]
public void UrlEscape([Option(0)]string input)
{
    Console.WriteLine(Uri.EscapeDataString(input));
}



.NET Core WindowsLinuxMacCICircleCICICI
version: 2.1
executors:
  dotnet:
    docker:
      - image: mcr.microsoft.com/dotnet/core/sdk:2.2
    environment:
      DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
      NUGET_XMLDOC_MODE: skip
jobs:
  publish-all:
    executor: dotnet
    steps:
      - checkout
      - run: dotnet publish -c Release --self-contained -r win-x64 -o ./MicroBatchSample/win-x64
      - run: dotnet publish -c Release --self-contained -r linux-x64 -o ./MicroBatchSample/linux-x64
      - run: dotnet publish -c Release --self-contained -r osx-x64 -o ./MicroBatchSample/osx-x64
      - run: apt update && apt install zip -y
      - run: zip -r MicroBatchSample.zip ./MicroBatchSample
      - store_artifacts:
          path: MicroBatchSample.zip
          destination: MicroBatchSample.zip
workflows:
  version: 2
  publish:
    jobs:
      - publish-all

C#Windows使

.NET CoreNPM Global ToolsUlidMicroBatchFramework.NET Core

55695191-6603e600-59f2-11e9-8553-72d03937fd57
dotnet使CLI


C#Parallel
// これを
foreach(var item in foo)
{
// なんかたくさん処理する
}

// こうするだけ
Parallel.ForEach(item =>
{
// なんか色々やる
});

使
class Program
{
    static async Task Main(string[] args)
    {
        // <T>を渡さないと複数検索モードになる
        await BatchHost.CreateDefaultBuilder().RunBatchEngineAsync(args);
    }
}

// こんなクラスや
public class Foo : BatchBase
{
    // こんなメソッドや
    public void Echo(string msg)
    {
        this.Context.Logger.LogInformation(msg);
    }
    // こんなメソッドを
    public void Sum(int x, int y)
    {
        this.Context.Logger.LogInformation($"{x + y}"); 
    }
}

// こんなクラスを
public class Bar : BatchBase
{
    public void Hello2()
    {
        this.Context.Logger.LogInformation("H E L L O");
    }
}

1
SampleApp.exe Foo.Echo -msg "aaaaa"
SampleApp.exe Foo.Sum -x 100 -y 200
SampleApp.exe Bar.Hello2

.NET CoreC#
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS sdk
WORKDIR /workspace
COPY . .
RUN dotnet publish ./MicroBatchFrameworkSample.csproj -c Release -o /app

FROM mcr.microsoft.com/dotnet/core/runtime:2.2
COPY --from=sdk /app .
ENTRYPOINT ["dotnet", "MicroBatchFrameworkSample.dll"]

AWS ECRCircleCI
version: 2.1
orbs:
  aws-ecr: circleci/aws-ecr@3.1.0
workflows:
  build-push:
    jobs:
      # see: https://circleci.com/orbs/registry/orb/circleci/aws-ecr
      - aws-ecr/build_and_push_image:
          repo: "microbatchsample"

C#

AWS Batch使CloudWatchCloudWatch

55616375-a6821a80-57cc-11e9-9d3a-a0691e631f28
LuigiApache Airflow使

Swagger


`RunBatchEngineAsync``RunBatchEngineWebHosting`使
public class Program
{
    public static async Task Main(string[] args)
    {
        await new WebHostBuilder().RunBatchEngineWebHosting("https://localhost:12345");
    }
}

Swagger

55614839-e8a95d00-57c8-11e9-89d5-ab0e7830e401

MicroBatchFramework.NET


.NET Generic HostMicroBatchFramework

.NET Generic HostDI
// appconfig.json
{
  "Foo": 42,
  "Bar": true
}

class Program
{
    static async Task Main(string[] args)
    {
        await BatchHost.CreateDefaultBuilder()
            .ConfigureServices((hostContext, services) =>
            {
                // mapping config json to IOption<MyConfig>
                services.Configure<MyConfig>(hostContext.Configuration);
            })
            .RunBatchEngineAsync<MyFirstBatch>(args);
    }
}

public class MyFirstBatch : BatchBase
{
    IOptions<MyConfig> config;
    ILogger<MyFirstBatch> logger;

    // get configuration from DI.
    public MyFirstBatch(IOptions<MyConfig> config, ILogger<MyFirstBatch> logger)
    {
        this.config = config;
        this.logger = logger;
    }

    public void ShowOption()
    {
        logger.LogInformation(config.Value.Bar);
        logger.LogInformation(config.Value.Foo);
    }
}

.NET Generic HostC#


C#CLI C#CLIC#CircleCIDocker

(FaaS)SDK

AWS BatchKubernetes使

FaaSFaaS MicroBatchFramework

Cy#C#.NET CoreUnity