/
activity.ts
225 lines (201 loc) · 6.37 KB
/
activity.ts
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
import { Construct } from 'constructs';
import { StatesMetrics } from './stepfunctions-canned-metrics.generated';
import { CfnActivity } from './stepfunctions.generated';
import * as cloudwatch from '../../aws-cloudwatch';
import * as iam from '../../aws-iam';
import { ArnFormat, IResource, Lazy, Names, Resource, Stack } from '../../core';
/**
* Properties for defining a new Step Functions Activity
*/
export interface ActivityProps {
/**
* The name for this activity.
*
* @default - If not supplied, a name is generated
*/
readonly activityName?: string;
}
/**
* Define a new Step Functions Activity
*/
export class Activity extends Resource implements IActivity {
/**
* Construct an Activity from an existing Activity ARN
*/
public static fromActivityArn(scope: Construct, id: string, activityArn: string): IActivity {
class Imported extends Resource implements IActivity {
public get activityArn() { return activityArn; }
public get activityName() {
return Stack.of(this).splitArn(activityArn, ArnFormat.COLON_RESOURCE_NAME).resourceName || '';
}
}
return new Imported(scope, id);
}
/**
* Construct an Activity from an existing Activity Name
*/
public static fromActivityName(scope: Construct, id: string, activityName: string): IActivity {
return Activity.fromActivityArn(scope, id, Stack.of(scope).formatArn({
service: 'states',
resource: 'activity',
resourceName: activityName,
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
}));
}
/**
* @attribute
*/
public readonly activityArn: string;
/**
* @attribute
*/
public readonly activityName: string;
constructor(scope: Construct, id: string, props: ActivityProps = {}) {
super(scope, id, {
physicalName: props.activityName ||
Lazy.string({ produce: () => this.generateName() }),
});
const resource = new CfnActivity(this, 'Resource', {
name: this.physicalName!, // not null because of above call to `super`
});
this.activityArn = this.getResourceArnAttribute(resource.ref, {
service: 'states',
resource: 'activity',
resourceName: this.physicalName,
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
});
this.activityName = this.getResourceNameAttribute(resource.attrName);
}
/**
* Grant the given identity permissions on this Activity
*
* @param identity The principal
* @param actions The list of desired actions
*/
public grant(identity: iam.IGrantable, ...actions: string[]) {
return iam.Grant.addToPrincipal({
grantee: identity,
actions,
resourceArns: [this.activityArn],
});
}
/**
* Return the given named metric for this Activity
*
* @default sum over 5 minutes
*/
public metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
namespace: 'AWS/States',
metricName,
dimensions: { ActivityArn: this.activityArn },
statistic: 'sum',
...props,
}).attachTo(this);
}
/**
* The interval, in milliseconds, between the time the activity starts and the time it closes.
*
* @default average over 5 minutes
*/
public metricRunTime(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(StatesMetrics.activityRunTimeAverage, props);
}
/**
* The interval, in milliseconds, for which the activity stays in the schedule state.
*
* @default average over 5 minutes
*/
public metricScheduleTime(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(StatesMetrics.activityScheduleTimeAverage, props);
}
/**
* The interval, in milliseconds, between the time the activity is scheduled and the time it closes.
*
* @default average over 5 minutes
*/
public metricTime(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(StatesMetrics.activityTimeAverage, props);
}
/**
* Metric for the number of times this activity is scheduled
*
* @default sum over 5 minutes
*/
public metricScheduled(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(StatesMetrics.activitiesScheduledSum, props);
}
/**
* Metric for the number of times this activity times out
*
* @default sum over 5 minutes
*/
public metricTimedOut(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(StatesMetrics.activitiesTimedOutSum, props);
}
/**
* Metric for the number of times this activity is started
*
* @default sum over 5 minutes
*/
public metricStarted(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(StatesMetrics.activitiesStartedSum, props);
}
/**
* Metric for the number of times this activity succeeds
*
* @default sum over 5 minutes
*/
public metricSucceeded(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(StatesMetrics.activitiesSucceededSum, props);
}
/**
* Metric for the number of times this activity fails
*
* @default sum over 5 minutes
*/
public metricFailed(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(StatesMetrics.activitiesFailedSum, props);
}
/**
* Metric for the number of times the heartbeat times out for this activity
*
* @default sum over 5 minutes
*/
public metricHeartbeatTimedOut(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(StatesMetrics.activitiesHeartbeatTimedOutSum, props);
}
private generateName(): string {
const name = Names.uniqueId(this);
if (name.length > 80) {
return name.substring(0, 40) + name.substring(name.length - 40);
}
return name;
}
private cannedMetric(
fn: (dims: { ActivityArn: string }) => cloudwatch.MetricProps,
props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
...fn({ ActivityArn: this.activityArn }),
...props,
}).attachTo(this);
}
}
/**
* Represents a Step Functions Activity
* https://docs.aws.amazon.com/step-functions/latest/dg/concepts-activities.html
*/
export interface IActivity extends IResource {
/**
* The ARN of the activity
*
* @attribute
*/
readonly activityArn: string;
/**
* The name of the activity
*
* @attribute
*/
readonly activityName: string;
}