batch 0.2.0 copy "batch: ^0.2.0" to clipboard
batch: ^0.2.0 copied to clipboard

outdated

A lightweight and powerful batch library written in Dart. You can easily develop a job schedule and batch program in Dart with this library.

example/example.dart

// Copyright (c) 2022, Kato Shinya. All rights reserved.
// Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:batch/batch.dart';

void main() {
  final job1 = Job(name: 'Job1', cron: '*/1 * * * *')
    ..nextStep(
      Step(name: 'Step1')
        ..nextTask(TestTask())
        ..nextTask(SayHelloTask())
        ..nextTask(SayWorldTask()),
    )
    ..nextStep(
      Step(name: 'Step2')
        ..nextTask(TestTask())
        ..nextTask(SayHelloTask())
        ..nextTask(SayWorldTask()),
    );

  final job2 = Job(name: 'Job2', cron: '*/3 * * * *')
    ..nextStep(
      Step(name: 'Step1')
        ..nextTask(SayHelloTask())
        ..nextTask(SayWorldTask()),
    );

  BatchApplication(
    logConfig: LogConfiguration(
      level: LogLevel.trace,
      filter: DevelopmentLogFilter(),
      output: ConsoleLogOutput(),
    ),
  )
    // You can add any parameters that is shared in this batch application.
    ..addSharedParameter(key: 'key1', value: 'value1')
    ..addSharedParameter(key: 'key2', value: {'any': 'object'})
    ..addJob(job1)
    ..addJob(job2)
    ..run();
}

class TestTask extends Task<TestTask> {
  static int count = 0;

  @override
  Future<RepeatStatus> execute(ExecutionContext context) async {
    if (count == 5) {
      trace('Finish.');
      return RepeatStatus.finished;
    }

    // This parameter is shared just in tasks in this step.
    context.parameters['key_$count'] = 'value$count';
    // You can use shared parameters in any places.
    info(context.sharedParameters['key1']);

    count++;

    info('Continue.');
    return RepeatStatus.continuable;
  }
}

class SayHelloTask extends Task<SayHelloTask> {
  @override
  Future<RepeatStatus> execute(ExecutionContext context) async {
    debug('Hello,');
    return RepeatStatus.finished;
  }
}

class SayWorldTask extends Task<SayWorldTask> {
  @override
  Future<RepeatStatus> execute(ExecutionContext context) async {
    info('World!');
    return RepeatStatus.finished;
  }
}
28
likes
0
pub points
53%
popularity

Publisher

verified publishershinyakato.dev

A lightweight and powerful batch library written in Dart. You can easily develop a job schedule and batch program in Dart with this library.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

cron

More

Packages that depend on batch