1 | import * as wmill from 'windmill-client'; |
2 |
|
3 | import { ApifyClient } from 'apify-client@^2.19.0'; |
4 |
|
5 | type ApifyApiKey = { |
6 | api_key: string; |
7 | }; |
8 |
|
9 | type Apify = { |
10 | token: string; |
11 | }; |
12 |
|
13 | type EventType = 'SUCCEEDED' | 'FAILED' | 'TIMED-OUT' | 'ABORTED'; |
14 |
|
15 | export type DynSelect_taskId = string; |
16 | export async function taskId(api_key?: ApifyApiKey, oauth_token?: Apify) { |
17 | if (!api_key?.api_key && !oauth_token?.token) { |
18 | return [{ value: '', label: 'Missing Apify API key or OAuth token' }]; |
19 | } |
20 |
|
21 | try { |
22 | const client = createClient(api_key, oauth_token); |
23 |
|
24 | const data = await client.tasks().list(); |
25 | const items = data?.items ?? []; |
26 |
|
27 | return items.map((task: any) => ({ |
28 | value: task.id, |
29 | label: task.title || task.name || task.id, |
30 | })); |
31 | } catch (error: any) { |
32 | return [ |
33 | { value: '', label: `Failed to load tasks: ${error.message || error}` }, |
34 | ]; |
35 | } |
36 | } |
37 |
|
38 | const createClient = (api_key?: ApifyApiKey, oauth_token?: Apify): ApifyClient => { |
39 | const token = oauth_token?.token ?? api_key?.api_key; |
40 | if (!token) { |
41 | throw new Error('Missing Apify API key or OAuth token'); |
42 | } |
43 |
|
44 | return new ApifyClient({ |
45 | token: token, |
46 | requestInterceptors: [ |
47 | (request) => { |
48 | if (!request.headers) { |
49 | request.headers = {}; |
50 | } |
51 | request.headers['x-apify-integration-platform'] = 'windmill'; |
52 | return request; |
53 | }, |
54 | ], |
55 | }); |
56 | }; |
57 |
|
58 | function areEventTypesEqual(a?: EventType[], b?: EventType[]): boolean { |
59 | if (!a || !b) return false; |
60 | return JSON.stringify([...a].sort()) === JSON.stringify([...b].sort()); |
61 | } |
62 |
|
63 | type TriggerState = { |
64 | lastRunId?: string; |
65 | taskId?: string; |
66 | eventTypes?: EventType[]; |
67 | }; |
68 |
|
69 | |
70 | |
71 | export async function main( |
72 | taskId: DynSelect_taskId, |
73 | eventTypes: EventType[], |
74 | runsLimit: number, |
75 | api_key?: ApifyApiKey, |
76 | oauth_token?: Apify, |
77 | ) { |
78 | |
79 | if (!eventTypes.length) { |
80 | return { error: 'At least one of the Event Types has to be picked.' }; |
81 | } |
82 |
|
83 | |
84 | const validatedRunsLimit = Number.isFinite(Number(runsLimit)) |
85 | ? Math.min(1000, Math.floor(Number(runsLimit))) |
86 | : NaN; |
87 |
|
88 | if (!validatedRunsLimit || validatedRunsLimit < 1) { |
89 | return { error: 'runsLimit must be a positive integer.' }; |
90 | } |
91 |
|
92 | try { |
93 | const client = createClient(api_key, oauth_token); |
94 |
|
95 | |
96 | const { items } = await client.task(taskId).runs().list({ |
97 | limit: validatedRunsLimit, |
98 | offset: 0, |
99 | desc: true, |
100 | status: eventTypes, |
101 | }); |
102 |
|
103 | |
104 | const runs: any[] = items ?? []; |
105 |
|
106 | |
107 | const state = (await wmill.getState()) as TriggerState | undefined; |
108 | const newestRunId: string | undefined = runs[0]?.id; |
109 |
|
110 | |
111 | const taskChanged = state?.taskId !== taskId; |
112 | const eventTypesChanged = !areEventTypesEqual(state?.eventTypes, eventTypes); |
113 |
|
114 | |
115 | |
116 | |
117 | |
118 | if (!state || taskChanged || eventTypesChanged) { |
119 | await wmill.setState({ |
120 | lastRunId: newestRunId, |
121 | taskId: taskId, |
122 | eventTypes, |
123 | }); |
124 | return []; |
125 | } |
126 |
|
127 | |
128 | if (runs.length === 0) { |
129 | return []; |
130 | } |
131 |
|
132 | |
133 | |
134 | const lastRunId = state.lastRunId; |
135 | const foundIdx = lastRunId ? runs.findIndex((run) => run?.id === lastRunId) : -1; |
136 |
|
137 | |
138 | const newerRuns = foundIdx >= 0 ? runs.splice(0, foundIdx) : runs; |
139 |
|
140 | |
141 | await wmill.setState({ |
142 | lastRunId: newestRunId, |
143 | taskId: taskId, |
144 | eventTypes, |
145 | }); |
146 |
|
147 | |
148 | return newerRuns; |
149 | } catch (error: any) { |
150 | return { |
151 | error: `Failed to process task trigger. Reason: ${ |
152 | error?.message || error |
153 | }`, |
154 | }; |
155 | } |
156 | } |