Skip to content

Commit 1dbfdce

Browse files
committed
added seed reference project, mostly for generating logs right now
1 parent 54ba468 commit 1dbfdce

7 files changed

Lines changed: 271 additions & 0 deletions

File tree

references/seed/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { logger, task, wait } from "@trigger.dev/sdk/v3";
2+
3+
const LONG_TEXT = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`;
4+
5+
const SEARCHABLE_TERMS = [
6+
"authentication_failed",
7+
"database_connection_error",
8+
"payment_processed",
9+
"user_registration_complete",
10+
"api_rate_limit_exceeded",
11+
"cache_invalidation",
12+
"webhook_delivery_success",
13+
"session_expired",
14+
"file_upload_complete",
15+
"email_sent_successfully",
16+
];
17+
18+
function generateLargeJson(index: number) {
19+
return {
20+
requestId: `req_${Date.now()}_${index}`,
21+
timestamp: new Date().toISOString(),
22+
metadata: {
23+
source: "log-spammer-task",
24+
environment: "development",
25+
version: "1.0.0",
26+
region: ["us-east-1", "eu-west-1", "ap-southeast-1"][index % 3],
27+
},
28+
user: {
29+
id: `user_${1000 + index}`,
30+
email: `testuser${index}@example.com`,
31+
name: `Test User ${index}`,
32+
preferences: {
33+
theme: index % 2 === 0 ? "dark" : "light",
34+
notifications: { email: true, push: false, sms: index % 3 === 0 },
35+
language: ["en", "es", "fr", "de"][index % 4],
36+
},
37+
},
38+
payload: {
39+
items: Array.from({ length: 5 }, (_, i) => ({
40+
itemId: `item_${index}_${i}`,
41+
name: `Product ${i}`,
42+
price: Math.random() * 100,
43+
quantity: Math.floor(Math.random() * 10) + 1,
44+
tags: ["electronics", "sale", "featured"].slice(0, (i % 3) + 1),
45+
})),
46+
totals: {
47+
subtotal: Math.random() * 500,
48+
tax: Math.random() * 50,
49+
shipping: Math.random() * 20,
50+
discount: Math.random() * 30,
51+
},
52+
},
53+
debugInfo: {
54+
stackTrace: `Error: ${SEARCHABLE_TERMS[index % SEARCHABLE_TERMS.length]}\n at processRequest (/app/src/handlers/main.ts:${100 + index}:15)\n at handleEvent (/app/src/events/processor.ts:${50 + index}:8)\n at async Runtime.handler (/app/src/index.ts:25:3)`,
55+
memoryUsage: { heapUsed: 45000000 + index * 1000, heapTotal: 90000000 },
56+
cpuTime: Math.random() * 1000,
57+
},
58+
longDescription: LONG_TEXT.repeat(2),
59+
};
60+
}
61+
62+
export const logSpammerTask = task({
63+
id: "log-spammer",
64+
maxDuration: 300,
65+
run: async () => {
66+
logger.info("Starting log spammer task for search testing");
67+
68+
for (let i = 0; i < 50; i++) {
69+
const term = SEARCHABLE_TERMS[i % SEARCHABLE_TERMS.length];
70+
const jsonPayload = generateLargeJson(i);
71+
72+
logger.log(`Processing event: ${term}`, { data: jsonPayload });
73+
74+
if (i % 5 === 0) {
75+
logger.warn(`Warning triggered for ${term}`, {
76+
warningCode: `WARN_${i}`,
77+
details: jsonPayload,
78+
longMessage: LONG_TEXT,
79+
});
80+
}
81+
82+
if (i % 10 === 0) {
83+
logger.error(`Error encountered: ${term}`, {
84+
errorCode: `ERR_${i}`,
85+
stack: jsonPayload.debugInfo.stackTrace,
86+
context: jsonPayload,
87+
});
88+
}
89+
90+
logger.debug(`Debug info for iteration ${i}`, {
91+
iteration: i,
92+
searchTerm: term,
93+
fullPayload: jsonPayload,
94+
additionalText: `${LONG_TEXT} --- Iteration ${i} complete with term ${term}`,
95+
});
96+
97+
if (i % 10 === 0) {
98+
await wait.for({ seconds: 0.5 });
99+
}
100+
}
101+
102+
logger.info("Log spammer task completed", {
103+
totalLogs: 50 * 4,
104+
searchableTerms: SEARCHABLE_TERMS,
105+
});
106+
107+
return { success: true, logsGenerated: 200 };
108+
},
109+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { task, batch } from "@trigger.dev/sdk/v3";
2+
import { ErrorTask } from "./throwError.js";
3+
import { SpanSpammerTask } from "./spanSpammer.js";
4+
import { logSpammerTask } from "./logSpammer.js";
5+
6+
export const seedTask = task({
7+
id: "seed-task",
8+
run: async (payload: any, { ctx }) => {
9+
let tasksToRun = [];
10+
11+
for (let i = 0; i < 10; i++) {
12+
tasksToRun.push({
13+
id: "simple-throw-error",
14+
payload: {},
15+
options: { delay: `${i}s` },
16+
});
17+
}
18+
19+
tasksToRun.push({
20+
id: "span-spammer",
21+
payload: {},
22+
});
23+
24+
tasksToRun.push({
25+
id: "log-spammer",
26+
payload: {},
27+
});
28+
29+
await batch.triggerAndWait(tasksToRun);
30+
return;
31+
},
32+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { logger, task, wait } from "@trigger.dev/sdk/v3";
2+
3+
const CONFIG = {
4+
delayBetweenBatchesSeconds: 0.2,
5+
logsPerBatch: 30,
6+
totalBatches: 100,
7+
initialDelaySeconds: 5,
8+
} as const;
9+
10+
export const SpanSpammerTask = task({
11+
id: "span-spammer",
12+
maxDuration: 300,
13+
run: async (payload: any, { ctx }) => {
14+
const context = { payload, ctx };
15+
let logCount = 0;
16+
17+
logger.info("Starting span spammer task", context);
18+
logger.warn("This will generate a lot of logs", context);
19+
20+
21+
const emitBatch = (prefix: string) => {
22+
logger.debug("Started spam batch emit!", context);
23+
24+
for (let i = 0; i < CONFIG.logsPerBatch; i++) {
25+
logger.log(`${prefix} ${++logCount}`, context);
26+
}
27+
28+
logger.debug('Completed spam batch emit!', context);
29+
};
30+
31+
emitBatch("Log number");
32+
await wait.for({ seconds: CONFIG.initialDelaySeconds });
33+
34+
for (let batch = 0; batch < CONFIG.totalBatches; batch++) {
35+
await wait.for({ seconds: CONFIG.delayBetweenBatchesSeconds });
36+
emitBatch("This is a test log!!! Log number: ");
37+
}
38+
39+
logger.info("Completed span spammer task", context);
40+
return { message: `Created ${logCount} logs` };
41+
},
42+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { logger, task, wait } from "@trigger.dev/sdk/v3";
2+
3+
4+
export const ErrorTask = task({
5+
id: "simple-throw-error",
6+
maxDuration: 60,
7+
run: async (payload: any, { ctx }) => {
8+
logger.log("This task is about to throw an error!", { payload, ctx });
9+
10+
await wait.for({ seconds: 9 });
11+
throw new Error("This is an expected test error from ErrorTask!");
12+
},
13+
onFailure: async ({ payload, error, ctx }) => {
14+
logger.warn("ErrorTask failed!", { payload, error, ctx });
15+
}
16+
});

references/seed/trigger.config.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { defineConfig } from "@trigger.dev/sdk/v3";
2+
import { syncEnvVars } from "@trigger.dev/build/extensions/core";
3+
import { lightpanda } from "@trigger.dev/build/extensions/lightpanda";
4+
5+
export default defineConfig({
6+
compatibilityFlags: ["run_engine_v2"],
7+
project: process.env.TRIGGER_PROJECT_REF!,
8+
experimental_processKeepAlive: {
9+
enabled: true,
10+
maxExecutionsPerProcess: 20,
11+
},
12+
logLevel: "debug",
13+
maxDuration: 3600,
14+
retries: {
15+
enabledInDev: true,
16+
default: {
17+
maxAttempts: 3,
18+
minTimeoutInMs: 1000,
19+
maxTimeoutInMs: 10000,
20+
factor: 2,
21+
randomize: true,
22+
},
23+
},
24+
machine: "small-2x",
25+
build: {
26+
extensions: [
27+
lightpanda(),
28+
syncEnvVars(async (ctx) => {
29+
return [
30+
{ name: "SYNC_ENV", value: ctx.environment },
31+
{ name: "BRANCH", value: ctx.branch ?? "NO_BRANCH" },
32+
{ name: "BRANCH", value: "PARENT", isParentEnv: true },
33+
{ name: "SECRET_KEY", value: "secret-value" },
34+
{ name: "ANOTHER_SECRET", value: "another-secret-value" },
35+
];
36+
}),
37+
{
38+
name: "npm-token",
39+
onBuildComplete: async (context, manifest) => {
40+
if (context.target === "dev") {
41+
return;
42+
}
43+
44+
context.addLayer({
45+
id: "npm-token",
46+
build: {
47+
env: {
48+
NPM_TOKEN: manifest.deploy.env?.NPM_TOKEN,
49+
},
50+
},
51+
});
52+
},
53+
},
54+
],
55+
},
56+
});

references/seed/tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2023",
4+
"module": "Node16",
5+
"moduleResolution": "Node16",
6+
"esModuleInterop": true,
7+
"strict": true,
8+
"skipLibCheck": true,
9+
"customConditions": ["@triggerdotdev/source"],
10+
"jsx": "preserve",
11+
"lib": ["DOM", "DOM.Iterable"],
12+
"noEmit": true
13+
},
14+
"include": ["./src/**/*.ts", "trigger.config.ts"]
15+
}

0 commit comments

Comments
 (0)