|
| 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 | +}); |
0 commit comments