This repository was archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.ts
132 lines (110 loc) · 4.33 KB
/
handler.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
import * as df from "durable-functions";
import { IFunctionContext } from "durable-functions/lib/src/classes";
import { isLeft } from "fp-ts/lib/Either";
import { isSome, none, Option, some } from "fp-ts/lib/Option";
import { readableReport } from "italia-ts-commons/lib/reporters";
import { RetrievedService } from "io-functions-commons/dist/src/models/service";
import {
Input as UpdateVisibleServicesActivityInput,
Result as UpdateVisibleServicesActivityResult
} from "../UpdateVisibleServicesActivity/handler";
import { retrievedServiceToVisibleService } from "../utils/conversions";
import { UpsertServiceEvent } from "../utils/UpsertServiceEvent";
/**
* Using the data of new and old service calculate the action to perform to the visible services
*/
function computeMaybeAction(
newService: RetrievedService,
oldService?: RetrievedService
): Option<UpdateVisibleServicesActivityInput["action"]> {
if (oldService === undefined) {
// A service has been created
return newService.isVisible ? some("UPSERT") : none;
}
// A service has been update
// Visibility not changed
if (oldService.isVisible === newService.isVisible) {
return newService.isVisible ? some("UPSERT") : none;
}
// Visibility changed
// If the old service was NOT visible and the new service IS visible return UPSERT, return DELETE otherwise
return !oldService.isVisible && newService.isVisible
? some("UPSERT")
: some("DELETE");
}
export const handler = function*(
context: IFunctionContext
): IterableIterator<unknown> {
const input = context.df.getInput();
const retryOptions = new df.RetryOptions(5000, 10);
// tslint:disable-next-line: no-object-mutation
retryOptions.backoffCoefficient = 1.5;
// Check if input is valid
const errorOrUpsertServiceEvent = UpsertServiceEvent.decode(input);
if (isLeft(errorOrUpsertServiceEvent)) {
context.log.error(
`UpdateVisibleServicesActivity|Cannot parse input|ERROR=${readableReport(
errorOrUpsertServiceEvent.value
)}`
);
// We will never be able to recover from this, so don't trigger a retry
return [];
}
const upsertServiceEvent = errorOrUpsertServiceEvent.value;
const { newService, oldService } = upsertServiceEvent;
// Update visible services if needed
const maybeAction = computeMaybeAction(newService, oldService);
const visibleService = retrievedServiceToVisibleService(newService);
if (isSome(maybeAction)) {
const action = maybeAction.value;
context.log.verbose(
`UpdateVisibleServicesActivity|Visible services must be updated|SERVICE_ID=${visibleService.serviceId}|ACTION=${action}`
);
const updateVisibleServicesActivityInput = UpdateVisibleServicesActivityInput.encode(
{
action,
visibleService
}
);
try {
const updateVisibleServicesActivityResultJson = yield context.df.callActivityWithRetry(
"UpdateVisibleServicesActivity",
retryOptions,
updateVisibleServicesActivityInput
);
const errorOrUpdateVisibleServicesActivityResult = UpdateVisibleServicesActivityResult.decode(
updateVisibleServicesActivityResultJson
);
if (isLeft(errorOrUpdateVisibleServicesActivityResult)) {
context.log.error(
`UpdateVisibleServicesActivity|Can't decode result|SERVICE_ID=${
visibleService.serviceId
}|ERROR=${readableReport(
errorOrUpdateVisibleServicesActivityResult.value
)}`
);
return [];
}
const updateVisibleServicesActivityResult =
errorOrUpdateVisibleServicesActivityResult.value;
if (updateVisibleServicesActivityResult.kind === "SUCCESS") {
context.log.verbose(
`UpdateVisibleServicesActivity|Update success|SERVICE_ID=${visibleService.serviceId}|ACTION=${action}`
);
} else {
context.log.error(
`UpdateVisibleServicesActivity|Activity failure|SERVICE_ID=${visibleService.serviceId}|ERROR=${updateVisibleServicesActivityResult.reason}`
);
}
} catch (e) {
context.log.error(
`UpdateVisibleServicesActivity|Max retry exceeded|SERVICE_ID=${visibleService.serviceId}|ERROR=${e}`
);
}
} else {
context.log.verbose(
`UpdateVisibleServicesActivity|No need to update visible services|SERVICE_ID=${visibleService.serviceId}`
);
}
return [];
};