/
example.dart
259 lines (238 loc) · 7.74 KB
/
example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2022 Kato Shinya. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided the conditions.
import 'dart:async';
import 'dart:io';
import 'package:batch/batch.dart';
void main(List<String> args) => runWorkflow(
args: args,
argsConfigBuilder: (parser) {
// The well-known "args" library can be used as standard.
// See more information about "args" at https://pub.dev/packages/args.
parser
..addOption('userName', abbr: 'u')
..addOption('appName', abbr: 'a')
..addFlag('release', abbr: 'r', defaultsTo: false);
},
onLoadArgs: (args) {
// This callback is useful when you want to create a singleton instance using command line arguments
// and manage it as SharedParameters in batch application. If this callback is not defined,
// all command line arguments are added as SharedParameters automatically.
return {'userName': args['userName']};
},
jobs: [
///! You can add scheduled jobs here.
_SayHelloWorldJob(),
_TestRetryAndCallbackJob(),
_TestBranchJob(),
_ParallelJob(),
_ShutdownJob()
],
sharedParameters: {
//! You can add any initial values and it's shared in this batch application.
'token': 'xxxxxxxx',
'tokens': ['xxxxxxxx', 'yyyyyyyy'],
},
logConfig: LogConfiguration(
//! You can customize log configuration.
level: LogLevel.trace,
output: MultiLogOutput([
ConsoleLogOutput(),
FileLogOutput(file: File('./test.txt')),
]),
color: LogColor(
info: ConsoleColor.cyan3,
),
printLog: true,
),
);
class _SayHelloWorldJob implements ScheduledJobBuilder {
@override
ScheduledJob build() => ScheduledJob(
name: 'Say Hello World Job',
schedule: CronParser('*/1 * * * *'), //! Execute every minute.
steps: [
//! You can add steps.
//! Please add in the order of execution.
Step(name: 'Say Hello Step', task: SayHelloTask()),
Step(name: 'Say World Step', task: SayWorldTask())
],
jobParameters: {
//! You can add any initial values and it's shared in this job.
'token': 'xxxxxxxx',
'tokens': ['xxxxxxxx', 'yyyyyyyy'],
},
);
}
class _TestRetryAndCallbackJob implements ScheduledJobBuilder {
@override
ScheduledJob build() => ScheduledJob(
name: 'Test Retry Job',
schedule: CronParser('*/1 * * * *'), //! Execute every 1 minutes.
//! You can set any preconditions to run this job.
steps: [
Step(
name: 'Test Retry and Callbacks Step',
precondition: (context) => true,
task: RetryTask(),
//! You can use convenient callbacks to each cycle.
onStarted: (context) => log.info('\n------ Started ------'),
onSucceeded: (context) => log.info('\n------ Succeeded ------'),
onError: (context, error, stackTrace) =>
log.error('Something wrong!', error, stackTrace),
onCompleted: (context) => log.info('\n------ Completed ------'),
retryConfig: RetryConfiguration(
maxAttempt: 3,
backOff: Duration(seconds: 1),
retryableExceptions: [FormatException()],
onRecover: (context) {
//! Do something when all retry attempts are failed.
},
),
),
Step(
name: 'Test Skip Exception Step',
task: SkipExceptionTask(),
skipConfig: SkipConfiguration(skippableExceptions: [Exception()]),
)
],
precondition: (context) => true,
);
}
class _TestBranchJob implements ScheduledJobBuilder {
@override
ScheduledJob build() => ScheduledJob(
name: 'Test Branch Job',
schedule: CronParser('*/2 * * * *'), //! Execute every 2 minutes.
steps: [
Step(
name: 'Switch Branch Step',
task: TestSwitchBranchTask(),
branchesOnSucceeded: [
Step(name: 'Say Hello Task (Branch)', task: SayHelloTask())
],
branchesOnFailed: [
Step(name: 'Should Not Be Executed Step', task: DummyTask())
],
branchesOnCompleted: [
Step(name: 'Say World Task (Branch)', task: SayWorldTask())
],
)
],
);
}
class _ParallelJob implements ScheduledJobBuilder {
@override
ScheduledJob build() => ScheduledJob(
name: 'Parallel Job',
schedule: CronParser('*/3 * * * *'), //! Execute every 5 minutes.
steps: [
ParallelStep(
name: 'Parallel Step',
precondition: (context) => true,
tasks: [
DoHeavyTask(),
DoHeavyTask(),
DoHeavyTask(),
DoHeavyTask(),
],
),
],
);
}
class _ShutdownJob implements ScheduledJobBuilder {
@override
ScheduledJob build() => ScheduledJob(
name: 'Shutdown Job',
schedule: CronParser('*/5 * * * *'), //! Execute every 5 minutes.
steps: [
Step.ofShutdown(),
],
);
}
class TestTask extends Task<TestTask> {
@override
void execute(ExecutionContext context) {
// This parameter is shared just in this job.
context.jobParameters['key'] = 'job_parameter';
// You can use shared parameters in any places.
log.info(context.sharedParameters['key1']);
log.info(context.sharedParameters['key2']);
log.trace('Trace');
log.info('Info');
log.debug('Debug');
log.warn('Warn');
log.error('Error');
log.fatal('Fatal');
}
}
class SayHelloTask extends Task<SayHelloTask> {
@override
void execute(ExecutionContext context) {
log.debug('Hello,');
}
}
class SayWorldTask extends Task<SayWorldTask> {
@override
void execute(ExecutionContext context) {
log.info('World!');
}
}
class RetryTask extends Task<RetryTask> {
/// The count for retry test
static int count = 0;
@override
void execute(ExecutionContext context) {
if (count < 3) {
count++;
throw FormatException();
} else {
count = 0;
}
}
}
class SkipExceptionTask extends Task<SkipExceptionTask> {
@override
void execute(ExecutionContext context) {
throw Exception();
}
}
class TestSwitchBranchTask extends Task<TestSwitchBranchTask> {
@override
void execute(ExecutionContext context) {
// Switch branch on this step.
context.stepExecution!.switchBranchToSucceeded();
}
}
class DummyTask extends Task<DummyTask> {
@override
void execute(ExecutionContext context) {
log.fatal('This should not be executed!.');
}
}
class DoHeavyTask extends ParallelTask<DoHeavyTask> {
@override
FutureOr<void> execute(ExecutionContext context) {
int i = 0;
while (i < 10000000000) {
i++;
}
//! The logging functions provided by frameworks such as "log.info" and "info" cannot be
//! used in parallel processing. Instead, use the "sendMessage" method corresponding
//! to the log level as below.
//!
//! Messages sent by the following "sendMessage" method are logged out at once after
//! parallel processing is completed.
super.sendMessageAsTrace('Trace');
super.sendMessageAsDebug('Debug');
super.sendMessageAsInfo('Info');
try {
throw Exception('Error output is also possible.');
} catch (error, stackTrace) {
// The error and stackTrace are optional.
super.sendMessageAsWarn('Warn');
super.sendMessageAsError('Error', error, stackTrace);
super.sendMessageAsFatal('Fatal', error, stackTrace);
}
}
}