Openstatus www.openstatus.dev
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

๐Ÿš‘ oupsi

+46 -20
+46 -20
apps/web/src/app/api/checker/cron/_cron.ts
··· 1 1 import type { NextRequest } from "next/server"; 2 2 import type { SignedInAuthObject } from "@clerk/nextjs/api"; 3 - import { Client } from "@upstash/qstash"; 4 3 import type { z } from "zod"; 5 4 6 5 import { createTRPCContext } from "@openstatus/api"; ··· 9 8 10 9 import { env } from "@/env"; 11 10 import type { payloadSchema } from "../schema"; 11 + import { CloudTasksClient } from "@google-cloud/tasks"; 12 + import type { google } from "@google-cloud/tasks/build/protos/protos"; 12 13 13 14 const periodicityAvailable = selectMonitorSchema.pick({ periodicity: true }); 14 15 ··· 27 28 periodicity, 28 29 req, 29 30 }: z.infer<typeof periodicityAvailable> & { req: NextRequest }) => { 30 - const c = new Client({ 31 - token: env.QSTASH_TOKEN, 31 + const client = new CloudTasksClient({ 32 + projectId: env.GCP_PROJECT_ID, 33 + credentials: { 34 + client_email: process.env.GCP_CLIENT_EMAIL, 35 + private_key: env.GCP_PRIVATE_KEY.replaceAll("\\n", "\n"), 36 + }, 32 37 }); 38 + const parent = client.queuePath( 39 + env.GCP_PROJECT_ID, 40 + env.GCP_LOCATION, 41 + periodicity, 42 + ); 43 + 33 44 console.log(`Start cron for ${periodicity}`); 34 45 const timestamp = Date.now(); 35 46 ··· 61 72 status: row.status, 62 73 }; 63 74 64 - const result = c.publishJSON({ 65 - url: `https://api.openstatus.dev/checker`, 66 - body: payload, 67 - delay: Math.random() * 90, 68 - headers: { 69 - "Upstash-Forward-fly-prefer-region": region, 75 + const task: google.cloud.tasks.v2beta3.ITask = { 76 + httpRequest: { 77 + headers: { 78 + "Content-Type": "application/json", // Set content type to ensure compatibility your application's request parsing 79 + "fly-prefer-region": region, 80 + Authorization: `Basic ${env.CRON_SECRET}`, 81 + }, 82 + httpMethod: "POST", 83 + url: "https://api.openstatus.dev/checkerV2", 84 + body: Buffer.from(JSON.stringify(payload)).toString("base64"), 70 85 }, 71 - }); 72 - allResult.push(result); 86 + }; 87 + const request = { parent: parent, task: task }; 88 + const [response] = await client.createTask(request); 89 + 90 + allResult.push(response); 73 91 } 74 92 } 75 93 // our first legacy monitor ··· 86 104 status: "active", 87 105 }; 88 106 89 - // TODO: fetch + try - catch + retry once 90 - const result = c.publishJSON({ 91 - url: `https://api.openstatus.dev/checker`, 92 - body: payload, 93 - headers: { 94 - "Upstash-Forward-fly-prefer-region": region, 107 + const task: google.cloud.tasks.v2beta3.ITask = { 108 + httpRequest: { 109 + headers: { 110 + "Content-Type": "application/json", // Set content type to ensure compatibility your application's request parsing 111 + "fly-prefer-region": region, 112 + Authorization: `Basic ${env.CRON_SECRET}`, 113 + }, 114 + httpMethod: "POST", 115 + url: "https://api.openstatus.dev/checkerV2", 116 + body: Buffer.from(JSON.stringify(payload)).toString("base64"), 95 117 }, 96 - delay: Math.random() * 90, 97 - }); 98 - allResult.push(result); 118 + }; 119 + 120 + // TODO: fetch + try - catch + retry once 121 + const request = { parent, task }; 122 + const [response] = await client.createTask(request); 123 + 124 + allResult.push(response); 99 125 } 100 126 } 101 127 await Promise.all(allResult);