-
Notifications
You must be signed in to change notification settings - Fork 368
/
orchestrator.service.ts
451 lines (395 loc) · 17 KB
/
orchestrator.service.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import { deleteSyncConfig, deleteSyncFilesForConfig } from './config/config.service.js';
import connectionService from '../connection.service.js';
import { deleteScheduleForSync, getSchedule, updateScheduleStatus } from './schedule.service.js';
import { getLatestSyncJob } from './job.service.js';
import {
createSync,
getSyncsByConnectionId,
getSyncsByProviderConfigKey,
getSyncsByProviderConfigAndSyncNames,
getSyncByIdAndName,
getSyncNamesByConnectionId,
softDeleteSync
} from './sync.service.js';
import {
createActivityLogMessageAndEnd,
createActivityLog,
createActivityLogMessage,
updateSuccess as updateSuccessActivityLog
} from '../activity/activity.service.js';
import SyncClient from '../../clients/sync.client.js';
import configService from '../config.service.js';
import type { LogLevel } from '../../models/Activity.js';
import type { Connection } from '../../models/Connection.js';
import type {
Job as SyncJob,
Schedule as SyncSchedule,
SyncDeploymentResult,
IncomingFlowConfig,
Sync,
SyncType,
ReportedSyncJobStatus
} from '../../models/Sync.js';
import { NangoError } from '../../utils/error.js';
import type { Config as ProviderConfig } from '../../models/Provider.js';
import type { ServiceResponse } from '../../models/Generic';
import { SyncStatus, ScheduleStatus, SyncConfigType, SyncCommand, CommandToActivityLog } from '../../models/Sync.js';
interface CreateSyncArgs {
connections: Connection[];
providerConfigKey: string;
environmentId: number;
sync: IncomingFlowConfig;
syncName: string;
}
export class Orchestrator {
public async create(
connections: Connection[],
syncName: string,
providerConfigKey: string,
environmentId: number,
sync: IncomingFlowConfig,
debug = false,
activityLogId?: number
): Promise<boolean> {
try {
const syncConfig = await configService.getProviderConfig(providerConfigKey, environmentId);
if (debug && activityLogId) {
await createActivityLogMessage({
level: 'debug',
environment_id: environmentId,
activity_log_id: activityLogId,
timestamp: Date.now(),
content: `Beginning iteration of starting syncs for ${syncName} with ${connections.length} connections`
});
}
for (const connection of connections) {
const syncExists = await getSyncByIdAndName(connection.id as number, syncName);
if (syncExists) {
continue;
}
const createdSync = await createSync(connection.id as number, syncName);
const syncClient = await SyncClient.getInstance();
await syncClient?.startContinuous(
connection,
createdSync as Sync,
syncConfig as ProviderConfig,
syncName,
{ ...sync, returns: sync.models, input: '' },
debug
);
}
if (debug && activityLogId) {
await createActivityLogMessage({
level: 'debug',
environment_id: environmentId,
activity_log_id: activityLogId,
timestamp: Date.now(),
content: `Finished iteration of starting syncs for ${syncName} with ${connections.length} connections`
});
}
return true;
} catch (e) {
const prettyError = JSON.stringify(e, ['message', 'name'], 2);
await createActivityLogMessage({
level: 'error',
environment_id: environmentId,
activity_log_id: activityLogId as number,
timestamp: Date.now(),
content: `Error starting syncs for ${syncName} with ${connections.length} connections: ${prettyError}`
});
return false;
}
}
public async createSyncs(syncArgs: CreateSyncArgs[], debug = false, activityLogId?: number): Promise<boolean> {
let success = true;
for (const syncToCreate of syncArgs) {
const { connections, providerConfigKey, environmentId, sync, syncName } = syncToCreate;
const result = await this.create(connections, syncName, providerConfigKey, environmentId, sync, debug, activityLogId);
if (!result) {
success = false;
}
}
return success;
}
/**
* Delete
* @desc delete a sync and all the related objects
* 1) sync config files
* 2) sync config
*/
public async deleteConfig(syncConfigId: number, environmentId: number) {
await deleteSyncFilesForConfig(syncConfigId, environmentId);
await deleteSyncConfig(syncConfigId);
}
public async softDeleteSync(syncId: string, environmentId: number) {
await deleteScheduleForSync(syncId, environmentId);
await softDeleteSync(syncId);
}
public async softDeleteSyncsByConnection(connection: Connection) {
const syncs = await getSyncsByConnectionId(connection.id!);
if (!syncs) {
return;
}
for (const sync of syncs) {
await this.softDeleteSync(sync.id!, connection.environment_id);
}
}
public async deleteSyncsByProviderConfig(environmentId: number, providerConfigKey: string) {
const syncs = await getSyncsByProviderConfigKey(environmentId, providerConfigKey);
if (!syncs) {
return;
}
for (const sync of syncs) {
await this.softDeleteSync(sync.id as string, environmentId);
}
}
public async runSyncCommand(
environmentId: number,
providerConfigKey: string,
syncNames: string[],
command: SyncCommand,
connectionId?: string
): Promise<ServiceResponse<boolean>> {
const action = CommandToActivityLog[command];
const provider = await configService.getProviderName(providerConfigKey);
const log = {
level: 'info' as LogLevel,
success: false,
action,
start: Date.now(),
end: Date.now(),
timestamp: Date.now(),
connection_id: connectionId || '',
provider,
provider_config_key: providerConfigKey,
environment_id: environmentId
};
const activityLogId = await createActivityLog(log);
if (!activityLogId) {
return { success: false, error: new NangoError('failed_to_create_activity_log'), response: false };
}
const syncClient = await SyncClient.getInstance();
if (!syncClient) {
return { success: false, error: new NangoError('failed_to_get_sync_client'), response: false };
}
if (connectionId) {
const { success, error, response: connection } = await connectionService.getConnection(connectionId, providerConfigKey, environmentId);
if (!success || !connection) {
return { success: false, error, response: false };
}
let syncs = syncNames;
if (syncs.length === 0) {
syncs = await getSyncNamesByConnectionId(connection.id as number);
}
for (const syncName of syncs) {
const sync = await getSyncByIdAndName(connection.id as number, syncName);
if (!sync) {
throw new Error(`Sync "${syncName}" doesn't exists.`);
}
const schedule = await getSchedule(sync.id as string);
if (!schedule) {
continue;
}
await syncClient.runSyncCommand(
schedule.schedule_id,
sync?.id as string,
command,
activityLogId,
environmentId,
providerConfigKey,
connectionId,
syncName,
connection.id
);
// if they're triggering a sync that shouldn't change the schedule status
if (command !== SyncCommand.RUN) {
await updateScheduleStatus(schedule.schedule_id, command, activityLogId, environmentId);
}
}
} else {
const syncs =
syncNames.length > 0
? await getSyncsByProviderConfigAndSyncNames(environmentId, providerConfigKey, syncNames)
: await getSyncsByProviderConfigKey(environmentId, providerConfigKey);
if (!syncs) {
const error = new NangoError('no_syncs_found');
return { success: false, error, response: false };
}
for (const sync of syncs) {
const schedule = await getSchedule(sync.id as string);
if (!schedule) {
continue;
}
const connection = await connectionService.getConnectionById(sync.nango_connection_id);
if (!connection) {
continue;
}
await syncClient.runSyncCommand(
schedule.schedule_id,
sync.id as string,
command,
activityLogId,
environmentId,
providerConfigKey,
connection.connection_id,
sync.name,
connection.id
);
if (command !== SyncCommand.RUN) {
await updateScheduleStatus(schedule.schedule_id, command, activityLogId, environmentId);
}
}
}
await createActivityLogMessageAndEnd({
level: 'info',
environment_id: environmentId,
activity_log_id: activityLogId,
timestamp: Date.now(),
content: `Sync was updated with command: "${action}" for sync: ${syncNames.join(', ')}`
});
await updateSuccessActivityLog(activityLogId, true);
return { success: true, error: null, response: true };
}
public async getSyncStatus(
environmentId: number,
providerConfigKey: string,
syncNames: string[],
connectionId?: string,
includeJobStatus = false,
optionalConnection?: Connection | null
): Promise<ServiceResponse<ReportedSyncJobStatus[] | void>> {
const syncsWithStatus: ReportedSyncJobStatus[] = [];
let connection = optionalConnection;
if (connectionId && !connection) {
const connectionResult = await connectionService.getConnection(connectionId, providerConfigKey, environmentId);
if (!connectionResult.success || !connectionResult.response) {
return { success: false, error: connectionResult.error, response: null };
}
connection = connectionResult.response;
}
if (connection) {
for (const syncName of syncNames) {
const sync = await getSyncByIdAndName(connection?.id as number, syncName);
if (!sync) {
continue;
}
const { schedule, latestJob, status, nextScheduledSyncAt } = await this.fetchSyncData(sync?.id as string, environmentId);
const reportedStatus = await this.reportedStatus(sync, latestJob, schedule, status, nextScheduledSyncAt, includeJobStatus);
syncsWithStatus.push(reportedStatus);
}
} else {
const syncs =
syncNames.length > 0
? await getSyncsByProviderConfigAndSyncNames(environmentId, providerConfigKey, syncNames)
: await getSyncsByProviderConfigKey(environmentId, providerConfigKey);
if (!syncs) {
return { success: true, error: null, response: syncsWithStatus };
}
for (const sync of syncs) {
const { schedule, latestJob, status, nextScheduledSyncAt } = await this.fetchSyncData(sync?.id as string, environmentId);
const reportedStatus = await this.reportedStatus(sync, latestJob, schedule, status, nextScheduledSyncAt, includeJobStatus);
syncsWithStatus.push(reportedStatus);
}
}
return { success: true, error: null, response: syncsWithStatus };
}
/**
* Classify Sync Status
* @desc categornize the different scenarios of sync status
* 1. If the schedule is paused and the job is not running, then the sync is paused
* 2. If the schedule is paused and the job is not running then the sync is stopped (last return case)
* 3. If the schedule is running but the last job is null then it is an error
* 4. If the job status is stopped then it is an error
* 5. If the job status is running then it is running
* 6. If the job status is success then it is success
*/
public classifySyncStatus(jobStatus: SyncStatus, scheduleStatus: ScheduleStatus): SyncStatus {
if (scheduleStatus === ScheduleStatus.PAUSED && jobStatus !== SyncStatus.RUNNING) {
return SyncStatus.PAUSED;
} else if (scheduleStatus === ScheduleStatus.RUNNING && jobStatus === null) {
return SyncStatus.ERROR;
} else if (jobStatus === SyncStatus.STOPPED) {
return SyncStatus.ERROR;
} else if (jobStatus === SyncStatus.RUNNING) {
return SyncStatus.RUNNING;
} else if (jobStatus === SyncStatus.SUCCESS) {
return SyncStatus.SUCCESS;
}
return SyncStatus.STOPPED;
}
/**
* Trigger If Connections Exist
* @desc for the recently deploy flows, create the sync and trigger it if there are connections
*/
public async triggerIfConnectionsExist(flows: SyncDeploymentResult[], environmentId: number) {
for (const flow of flows) {
if (flow.type === SyncConfigType.ACTION) {
continue;
}
const existingConnections = await connectionService.getConnectionsByEnvironmentAndConfig(environmentId, flow.providerConfigKey);
if (existingConnections.length === 0) {
continue;
}
const { providerConfigKey } = flow;
const name = flow.name || flow.syncName;
await this.create(
existingConnections as Connection[],
name as string,
providerConfigKey,
environmentId,
flow as unknown as IncomingFlowConfig,
false
);
}
}
private async fetchSyncData(syncId: string, environmentId: number) {
const syncClient = await SyncClient.getInstance();
const schedule = await getSchedule(syncId);
const latestJob = await getLatestSyncJob(syncId);
let status = this.classifySyncStatus(latestJob?.status as SyncStatus, schedule?.status as ScheduleStatus);
const syncSchedule = await syncClient?.describeSchedule(schedule?.schedule_id as string);
if (syncSchedule) {
if (syncSchedule?.schedule?.state?.paused && status !== SyncStatus.PAUSED) {
await updateScheduleStatus(schedule?.id as string, SyncCommand.PAUSE, null, environmentId);
if (status !== SyncStatus.RUNNING) {
status = SyncStatus.PAUSED;
}
} else if (!syncSchedule?.schedule?.state?.paused && status === SyncStatus.PAUSED) {
await updateScheduleStatus(schedule?.id as string, SyncCommand.UNPAUSE, null, environmentId);
status = SyncStatus.STOPPED;
}
}
let nextScheduledSyncAt = null;
if (status !== SyncStatus.PAUSED) {
if (syncSchedule && syncSchedule?.info && syncSchedule?.info.futureActionTimes && syncSchedule?.info?.futureActionTimes?.length > 0) {
const futureRun = syncSchedule.info.futureActionTimes[0];
nextScheduledSyncAt = syncClient?.formatFutureRun(futureRun?.seconds?.toNumber() as number);
}
}
return { schedule, latestJob, status, nextScheduledSyncAt };
}
private async reportedStatus(
sync: Sync,
latestJob: SyncJob | null,
schedule: SyncSchedule | null,
status: SyncStatus,
nextScheduledSyncAt?: string | Date | null,
includeJobStatus = false
) {
const reportedStatus: ReportedSyncJobStatus = {
id: sync?.id,
type: latestJob?.type as SyncType,
finishedAt: latestJob?.updated_at,
nextScheduledSyncAt,
name: sync?.name,
status,
frequency: schedule?.frequency,
latestResult: latestJob?.result
} as ReportedSyncJobStatus;
if (includeJobStatus) {
reportedStatus['jobStatus'] = latestJob?.status as SyncStatus;
}
return reportedStatus;
}
}
export default new Orchestrator();