Skip to content

Commit 10207d4

Browse files
committed
Add sleepfordays sample
1 parent 30db7db commit 10207d4

File tree

8 files changed

+325
-0
lines changed

8 files changed

+325
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Sleep for days
2+
3+
This sample demonstrates how to use Temporal to run a workflow that periodically sleeps for a number of days.
4+
5+
## Run the sample
6+
7+
1. Start the Worker:
8+
9+
```bash
10+
./gradlew -q execute -PmainClass=io.temporal.samples.sleepfordays.Worker
11+
```
12+
13+
2. Start the Starter
14+
15+
```bash
16+
./gradlew -q execute -PmainClass=io.temporal.samples.sleepfordays.Starter
17+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.sleepfordays;
21+
22+
import io.temporal.activity.ActivityInterface;
23+
24+
@ActivityInterface
25+
public interface SendEmailActivity {
26+
void sendEmail(String email);
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.sleepfordays;
21+
22+
public class SendEmailActivityImpl implements SendEmailActivity {
23+
@Override
24+
public void sendEmail(String email) {
25+
System.out.println(email);
26+
}
27+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.sleepfordays;
21+
22+
import io.temporal.activity.ActivityOptions;
23+
import io.temporal.workflow.Promise;
24+
import io.temporal.workflow.Workflow;
25+
import java.time.Duration;
26+
27+
public class SleepForDaysImpl implements SleepForDaysWorkflow {
28+
29+
private final SendEmailActivity activity;
30+
private boolean complete = false;
31+
32+
public SleepForDaysImpl() {
33+
this.activity =
34+
Workflow.newActivityStub(
35+
SendEmailActivity.class,
36+
ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(10)).build());
37+
}
38+
39+
@Override
40+
public String sleepForDays() {
41+
while (!this.complete) {
42+
activity.sendEmail(String.format("Sleeping for 30 days"));
43+
Promise<Void> timer = Workflow.newTimer(Duration.ofDays(30));
44+
Workflow.await(() -> timer.isCompleted() || this.complete);
45+
}
46+
47+
return "done!";
48+
}
49+
50+
@Override
51+
public void complete() {
52+
this.complete = true;
53+
}
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.sleepfordays;
21+
22+
import io.temporal.workflow.SignalMethod;
23+
import io.temporal.workflow.WorkflowInterface;
24+
import io.temporal.workflow.WorkflowMethod;
25+
26+
@WorkflowInterface
27+
public interface SleepForDaysWorkflow {
28+
@WorkflowMethod
29+
String sleepForDays();
30+
31+
@SignalMethod
32+
void complete();
33+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.sleepfordays;
21+
22+
import io.temporal.client.WorkflowClient;
23+
import io.temporal.client.WorkflowOptions;
24+
import io.temporal.client.WorkflowStub;
25+
26+
public class Starter {
27+
28+
public static final String TASK_QUEUE = "SleepForDaysTaskQueue";
29+
30+
public static void main(String[] args) {
31+
// Start a workflow execution.
32+
SleepForDaysWorkflow workflow =
33+
Worker.client.newWorkflowStub(
34+
SleepForDaysWorkflow.class,
35+
WorkflowOptions.newBuilder().setTaskQueue(TASK_QUEUE).build());
36+
37+
// Start the workflow.
38+
WorkflowClient.start(workflow::sleepForDays);
39+
40+
WorkflowStub stub = WorkflowStub.fromTyped(workflow);
41+
42+
// Wait for workflow to complete. This will wait indefinitely until a 'complete' signal is sent.
43+
stub.getResult(String.class);
44+
System.exit(0);
45+
}
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.sleepfordays;
21+
22+
import io.temporal.client.WorkflowClient;
23+
import io.temporal.serviceclient.WorkflowServiceStubs;
24+
import io.temporal.worker.WorkerFactory;
25+
26+
public class Worker {
27+
public static final String TASK_QUEUE = "SleepForDaysTaskQueue";
28+
public static final WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
29+
public static final WorkflowClient client = WorkflowClient.newInstance(service);
30+
public static final WorkerFactory factory = WorkerFactory.newInstance(client);
31+
32+
public static void main(String[] args) {
33+
io.temporal.worker.Worker worker = factory.newWorker(TASK_QUEUE);
34+
worker.registerWorkflowImplementationTypes(SleepForDaysImpl.class);
35+
worker.registerActivitiesImplementations(new SendEmailActivityImpl());
36+
37+
factory.start();
38+
System.out.println("Worker started for task queue: " + TASK_QUEUE);
39+
}
40+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.sleepfordays;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.mockito.ArgumentMatchers.anyString;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.times;
26+
import static org.mockito.Mockito.verify;
27+
28+
import io.temporal.client.WorkflowClient;
29+
import io.temporal.client.WorkflowOptions;
30+
import io.temporal.testing.TestWorkflowRule;
31+
import java.time.Duration;
32+
import org.junit.Rule;
33+
import org.junit.Test;
34+
35+
public class SleepForDaysTest {
36+
37+
@Rule
38+
public TestWorkflowRule testWorkflowRule =
39+
TestWorkflowRule.newBuilder()
40+
.setWorkflowTypes(SleepForDaysImpl.class)
41+
.setDoNotStart(true)
42+
.build();
43+
44+
@Test(timeout = 8000)
45+
public void testSleepForDays() {
46+
// Mock activity
47+
SendEmailActivity activities = mock(SendEmailActivity.class);
48+
testWorkflowRule.getWorker().registerActivitiesImplementations(activities);
49+
// Start environment
50+
testWorkflowRule.getTestEnvironment().start();
51+
52+
// Create a workflow
53+
WorkflowOptions workflowOptions =
54+
WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.getTaskQueue()).build();
55+
SleepForDaysWorkflow workflow =
56+
testWorkflowRule
57+
.getWorkflowClient()
58+
.newWorkflowStub(SleepForDaysWorkflow.class, workflowOptions);
59+
60+
// Start workflow
61+
WorkflowClient.start(workflow::sleepForDays);
62+
63+
long startTime = testWorkflowRule.getTestEnvironment().currentTimeMillis();
64+
// Time-skip 5 minutes.
65+
testWorkflowRule.getTestEnvironment().sleep(Duration.ofMinutes(5));
66+
// Check that the activity has been called, we're now waiting for the sleep to finish.
67+
verify(activities, times(1)).sendEmail(anyString());
68+
// Time-skip 3 days.
69+
testWorkflowRule.getTestEnvironment().sleep(Duration.ofDays(90));
70+
// Expect 3 more activity calls.
71+
verify(activities, times(4)).sendEmail(anyString());
72+
// Send the signal to complete the workflow.
73+
workflow.complete();
74+
// Expect no more activity calls to have been made - workflow is complete.
75+
verify(activities, times(4)).sendEmail(anyString());
76+
;
77+
// Expect more than 90 days to have passed.
78+
long endTime = testWorkflowRule.getTestEnvironment().currentTimeMillis();
79+
assertEquals(true, endTime - startTime > Duration.ofDays(90).toMillis());
80+
}
81+
}

0 commit comments

Comments
 (0)