Skip to content

Commit 8262211

Browse files
authored
Merge pull request #19 from Cysharp/feature/ReturnValueAsExitCode
Treat the return value as the exit code.
2 parents e8bd374 + a856b44 commit 8262211

File tree

4 files changed

+151
-2
lines changed

4 files changed

+151
-2
lines changed

ReadMe.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,30 @@ You can call like here.
216216
> SampleApp.exe -array [10,20,30] -person {"Age":10,"Name":"foo"}
217217
```
218218

219+
Exit Code
220+
---
221+
If the batch method returns `int` or `Task<int>` value, BatchEngine will set the return value to the exit code.
222+
223+
```csharp
224+
public class ExampleBatch : BatchBase
225+
{
226+
[Command(nameof(ExitCodeWithTask))]
227+
public async Task<int> ExitCodeWithTask()
228+
{
229+
return 54321;
230+
}
231+
232+
[Command(nameof(ExitCode))]
233+
public int ExitCode()
234+
{
235+
return 12345;
236+
}
237+
}
238+
```
239+
240+
> **NOTE**: If the method throws an unhandled exception, BatchEngine always set `1` to the exit code.
241+
242+
219243
Daemon
220244
---
221245

src/MicroBatchFramework/BatchEngine.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,17 @@ async Task RunCore(BatchContext ctx, Type type, MethodInfo methodInfo, string[]
149149
try
150150
{
151151
var result = methodInfo.Invoke(instance, invokeArgs);
152-
if (result is Task t)
152+
switch (result)
153153
{
154-
await t;
154+
case int exitCode:
155+
Environment.ExitCode = exitCode;
156+
break;
157+
case Task<int> taskWithExitCode:
158+
Environment.ExitCode = await taskWithExitCode;
159+
break;
160+
case Task task:
161+
await task;
162+
break;
155163
}
156164
}
157165
catch (Exception ex)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using Xunit;
2+
3+
[assembly: CollectionBehavior(DisableTestParallelization = true)]
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using FluentAssertions;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
using Xunit.Abstractions;
8+
9+
namespace MicroBatchFramework.Tests
10+
{
11+
public class ExitCodeTest
12+
{
13+
public class ExitCodeTestBatch : BatchBase
14+
{
15+
[Command(nameof(NoExitCode))]
16+
public void NoExitCode()
17+
{
18+
}
19+
20+
[Command(nameof(NoExitCodeException))]
21+
public void NoExitCodeException()
22+
{
23+
throw new Exception();
24+
}
25+
26+
[Command(nameof(NoExitCodeWithTask))]
27+
public Task NoExitCodeWithTask()
28+
{
29+
return Task.CompletedTask;
30+
}
31+
32+
[Command(nameof(ExitCode))]
33+
public int ExitCode()
34+
{
35+
return 12345;
36+
}
37+
38+
[Command(nameof(ExitCodeException))]
39+
public int ExitCodeException()
40+
{
41+
throw new Exception();
42+
}
43+
44+
[Command(nameof(ExitCodeWithTask))]
45+
public Task<int> ExitCodeWithTask()
46+
{
47+
return Task.FromResult(54321);
48+
}
49+
50+
[Command(nameof(ExitCodeWithTaskException))]
51+
public Task<int> ExitCodeWithTaskException()
52+
{
53+
throw new Exception();
54+
}
55+
}
56+
57+
[Fact]
58+
public async Task NoExitCode()
59+
{
60+
Environment.ExitCode = 0;
61+
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(NoExitCode) });
62+
Assert.Equal(0, Environment.ExitCode);
63+
}
64+
65+
[Fact]
66+
public async Task NoExitCodeWithTask()
67+
{
68+
Environment.ExitCode = 0;
69+
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(NoExitCodeWithTask) });
70+
Assert.Equal(0, Environment.ExitCode);
71+
}
72+
73+
[Fact]
74+
public async Task NoExitCodeException()
75+
{
76+
Environment.ExitCode = 0;
77+
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(NoExitCodeException) });
78+
Assert.Equal(1, Environment.ExitCode);
79+
}
80+
81+
[Fact]
82+
public async Task ExitCode()
83+
{
84+
Environment.ExitCode = 0;
85+
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(ExitCode) });
86+
Assert.Equal(12345, Environment.ExitCode);
87+
}
88+
89+
[Fact]
90+
public async Task ExitCodeException()
91+
{
92+
Environment.ExitCode = 0;
93+
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(ExitCodeException) });
94+
Assert.Equal(1, Environment.ExitCode);
95+
}
96+
97+
[Fact]
98+
public async Task ExitCodeWithTask()
99+
{
100+
Environment.ExitCode = 0;
101+
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(ExitCodeWithTask) });
102+
Assert.Equal(54321, Environment.ExitCode);
103+
}
104+
105+
[Fact]
106+
public async Task ExitCodeWithTaskException()
107+
{
108+
Environment.ExitCode = 0;
109+
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(ExitCodeWithTaskException) });
110+
Assert.Equal(1, Environment.ExitCode);
111+
}
112+
113+
}
114+
}

0 commit comments

Comments
 (0)