phanikumv commented on code in PR #76539:
URL: https://github.com/apache/airflow/pull/76539#discussion_r4610981067
##########
ts-sdk/src/sdk/dag.ts:
##########
@@ -1255,6 +1367,178 @@ export class Dag {
return Object.freeze(group);
}
+ #placeLoop(group: TaskGroupRef, args: readonly unknown[]): LoopRef {
+ const { groupId } = group;
+ const label = `Loop "${groupId}" of Dag "${this.dagId}"`;
+ if (this.#finalized) {
+ throw new Error(
+ `${label} was declared after the Dag was read; declare every loop
while the module is ` +
+ "loading",
+ );
+ }
+ if (this.#loops.has(groupId)) {
+ throw new Error(`${label} was already declared; call loop() once per
task group`);
+ }
+ const [first] = args;
+ const until = typeof first === "function" ? (first as
UntilCallable<never>) : undefined;
+ const expected = until === undefined ? 1 : 3;
+ // TypeScript already says so, but from plain JavaScript a dropped
argument would leave the
+ // gate without the inputs or the bound it was meant to have.
+ if (args.length !== expected) {
+ throw new Error(
+ until === undefined
+ ? `${label} takes either loop({ maxIterations }) or loop(until,
inputs, ` +
+ `{ maxIterations }), not ${args.length} arguments of which the
first is ` +
+ describeValue(first)
+ : `${label} takes loop(until, inputs, { maxIterations }), not
${args.length} arguments`,
+ );
+ }
+ const options: unknown = args[expected - 1];
+ if (!isPlainRecord(options)) {
+ throw new Error(`The options of ${label} must be an object holding
maxIterations`);
+ }
+ const { maxIterations, ...gateSpec } = options;
+ if (
+ typeof maxIterations !== "number" ||
+ !Number.isSafeInteger(maxIterations) ||
+ maxIterations <= 0
+ ) {
+ throw new Error(
+ `maxIterations of ${label} must be a positive integer, not
${describeValue(maxIterations)}`,
+ );
+ }
+ // The gate is named after its handler, as a task is, and Python's
fallback stands in for a
+ // handler that has no name Airflow accepts, or for no handler at all.
+ const named = readFunctionName(until);
+ const gateId =
+ typeof gateSpec["taskId"] === "string"
+ ? undefined
+ : named !== undefined && LOOP_GATE_NAME.test(named)
+ ? named
+ : DEFAULT_LOOP_GATE_ID;
+ const handler = until ?? (async () => undefined);
Review Comment:
```suggestion
if (gateId !== undefined) {
const qualified = this.#childId(groupId, gateId);
if (this.#tasks.has(qualified) || this.#groups.has(qualified)) {
throw new Error(
`${label} would name its gate "${qualified}", which is already
taken; ` +
"pass taskId in the loop options to name the gate",
);
}
}
const handler = until ?? (async () => undefined);
```
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]