Skip to content

Commit

Permalink
Fixed Job.attachToClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ericallam committed Dec 8, 2023
1 parent c04cdfd commit 6a3c563
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-chefs-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---

Fixed Job.attachToClient
8 changes: 4 additions & 4 deletions packages/trigger-sdk/src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ export class Job<
* Attaches the job to a client. This is called automatically when you define a job using `client.defineJob()`.
*/
attachToClient(client: TriggerClient) {
this.client = client;
this.trigger.attachToJob(client, this);
client.attach(this);
return this;
}

get id() {
Expand Down Expand Up @@ -185,8 +185,8 @@ export class Job<
typeof this.options.concurrencyLimit === "number"
? this.options.concurrencyLimit
: typeof this.options.concurrencyLimit === "object"
? { id: this.options.concurrencyLimit.id, limit: this.options.concurrencyLimit.limit }
: undefined,
? { id: this.options.concurrencyLimit.id, limit: this.options.concurrencyLimit.limit }
: undefined,
};
}

Expand Down
28 changes: 14 additions & 14 deletions packages/trigger-sdk/src/triggerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,8 @@ export class TriggerClient {

attach(job: Job<Trigger<any>, any>): void {
this.#registeredJobs[job.id] = job;
job.attachToClient(this);
job.trigger.attachToJob(this, job);
job.client = this;
}

attachDynamicTrigger(trigger: DynamicTrigger<any, any>): void {
Expand Down Expand Up @@ -1683,15 +1684,15 @@ export class TriggerClient {
auth:
resolvedAuth.type === "apiKey"
? {
type: "apiKey",
accessToken: resolvedAuth.token,
additionalFields: resolvedAuth.additionalFields,
}
type: "apiKey",
accessToken: resolvedAuth.token,
additionalFields: resolvedAuth.additionalFields,
}
: {
type: "oauth2",
accessToken: resolvedAuth.token,
additionalFields: resolvedAuth.additionalFields,
},
type: "oauth2",
accessToken: resolvedAuth.token,
additionalFields: resolvedAuth.additionalFields,
},
};
} catch (resolverError) {
if (resolverError instanceof Error) {
Expand All @@ -1708,9 +1709,8 @@ export class TriggerClient {

return {
ok: false,
error: `Auth could not be resolved for ${
integration.id
}: auth resolver threw an unknown error: ${JSON.stringify(resolverError)}`,
error: `Auth could not be resolved for ${integration.id
}: auth resolver threw an unknown error: ${JSON.stringify(resolverError)}`,
};
}
}
Expand All @@ -1737,8 +1737,8 @@ export class TriggerClient {
typeof job.options.concurrencyLimit === "number"
? job.options.concurrencyLimit
: typeof job.options.concurrencyLimit === "object"
? { id: job.options.concurrencyLimit.id, limit: job.options.concurrencyLimit.limit }
: undefined,
? { id: job.options.concurrencyLimit.id, limit: job.options.concurrencyLimit.limit }
: undefined,
};
}

Expand Down
12 changes: 11 additions & 1 deletion references/job-catalog/src/edge-cases.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createExpressServer } from "@trigger.dev/express";
import { TriggerClient, invokeTrigger } from "@trigger.dev/sdk";
import { Job, TriggerClient, invokeTrigger } from "@trigger.dev/sdk";
import fs from "node:fs";
import fsPromises from "node:fs/promises";

Expand Down Expand Up @@ -104,4 +104,14 @@ client.defineJob({
},
});

const job = new Job({
id: "attach-to-client",
name: "Attach to Client",
version: "1.0.0",
trigger: invokeTrigger(),
run: async (payload, io, ctx) => {
await io.logger.info("Hello from job", { ctx })
},
}).attachToClient(client);

createExpressServer(client);

0 comments on commit 6a3c563

Please sign in to comment.