### Install Queuebase dependencies
Source: https://github.com/brockherion/queuebase/blob/main/packages/queuebase/README.md
Installs the required Queuebase and Zod packages via npm to enable job management and schema validation.
```shell
npm i queuebase zod
```
--------------------------------
### Queuebase Logger Methods (TypeScript)
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/guides/logging.mdx
Provides examples of using various logging methods exposed by the Queuebase logger object. These methods allow for different levels of message logging, from trace to fatal errors. Each method takes a string message as input.
```typescript
logger.trace("This is a trace message!");
```
```typescript
logger.debug("This is a debug message!");
```
```typescript
logger.info("Hello there!");
```
```typescript
logger.success("Job completed successfully!");
```
```typescript
logger.warn("This is a warning!");
```
```typescript
logger.error("Something went wrong!");
```
```typescript
logger.fatal("This is a fatal error!");
```
--------------------------------
### Enqueueing a job with a delay in Queuebase
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/guides/delay.mdx
Demonstrates how to schedule a job for future execution by passing a delay option in seconds to the enqueue method. This example uses a React client component to trigger the job.
```jsx
"use client";
import { jobs } from "@/utils/queuebase";
export default function ExecuteWithDelay() {
return (
);
}
```
--------------------------------
### Enqueue a Queuebase Job from a React Component
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/getting-started/pagesdir.mdx
Demonstrates how to enqueue a Queuebase job from a React component in a Next.js application. This example shows a button that, when clicked, calls the `enqueue` method on the 'sayHello' job using the type-safe client.
```jsx
// components/execute-job.tsx
import { jobs } from "@/utils/queuebase";
export default function ExecuteJob() {
return (
);
}
```
--------------------------------
### Setup Type-Safe Queuebase Job Client in Next.js
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/getting-started/pagesdir.mdx
Creates a type-safe client for interacting with Queuebase jobs within a Next.js application. This utility function, `createQueuebaseClient`, uses the defined job router to provide an auto-completed interface for enqueuing jobs.
```typescript
// utils/queuebase.ts
import type { JobRouter } from "@/pages/api/queuebase";
import { createQueuebaseClient } from "queuebase/client";
export const { jobs } = createQueuebaseClient({
apiKey: process.env.NEXT_PUBLIC_QUEUEBASE_API_KEY!,
});
```
--------------------------------
### Configure Job Retries in Queuebase
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/guides/retries.mdx
Demonstrates how to set the 'retries' property within a Queuebase job configuration using the job builder pattern. This example uses Zod for input validation and sets a retry limit of 1.
```javascript
jobWithRetries: j()
.input(
z.object({
name: z.string(),
}),
)
.config({ retries: 1 })
.handler(({ input }) => {
console.log(`Hello, ${input.name}!`);
})
```
--------------------------------
### Configure Queuebase Environment Variables
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/getting-started/pagesdir.mdx
Sets up essential environment variables for Queuebase integration in a Next.js application. These variables, typically stored in a .env.local file, include API keys, secret keys, and the application URL.
```shell
# .env.local
NEXT_PUBLIC_QUEUEBASE_API_KEY=... # Your Queuebase API key
QUEUEBASE_SECRET_KEY=... # Your Queuebase secret key
QUEUEBASE_URL=... # Your app URL
```
--------------------------------
### Configure Queuebase environment variables
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/getting-started/appdir.mdx
Set the required API and secret keys in your .env.local file to authenticate your application with the Queuebase service.
```bash
# .env.local
NEXT_PUBLIC_QUEUEBASE_API_KEY=... # Your Queuebase API key
QUEUEBASE_SECRET_KEY=... # Your Queuebase secret key
```
--------------------------------
### Using Delay Constants in Queuebase
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/guides/delay.mdx
Shows how to utilize built-in delay constants from the Queuebase SDK to define job scheduling times more clearly. This approach replaces raw integers with readable constants like DELAY_TIMES.ONE_DAY.
```jsx
"use client";
import { jobs } from "@/utils/queuebase";
import { DELAY_TIMES } from "queuebase/constants";
export default function ExecuteWithDelay() {
return (
);
}
```
--------------------------------
### Configure Next.js App with Plausible Analytics
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/_app.mdx
This component serves as the root wrapper for the Next.js application. It imports global styles and initializes the PlausibleProvider to track site analytics for the docs.queuebase.com domain.
```javascript
import "@/styles/globals.css";
import PlausibleProvider from "next-plausible";
export default function App({ Component, pageProps }) {
return (
);
}
```
--------------------------------
### Initialize Queuebase job client
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/getting-started/appdir.mdx
Create a type-safe client instance to interact with your defined job router from anywhere in your application.
```typescript
// utils/queuebase.ts
import type { JobRouter } from "@/app/api/queuebase/core";
import { createQueuebaseClient } from "queuebase/client";
export const { jobs } = createQueuebaseClient({
apiKey: process.env.NEXT_PUBLIC_QUEUEBASE_API_KEY!,
});
```
--------------------------------
### Configure Queuebase environment variables
Source: https://github.com/brockherion/queuebase/blob/main/packages/queuebase/README.md
Defines the necessary environment variables for Queuebase authentication, including the public API key and secret key.
```env
NEXT_PUBLIC_QUEUEBASE_API_KEY=... # Your Queuebase API key
QUEUEBASE_SECRET_KEY=... # Your Queuebase secret key
```
--------------------------------
### Initialize Queuebase job client
Source: https://github.com/brockherion/queuebase/blob/main/packages/queuebase/README.md
Sets up the type-safe Queuebase client using the job router definition. This client is used to enqueue jobs throughout the application.
```typescript
import type { JobRouter } from "@/app/api/queuebase/core";
import { createQueuebaseClient } from "queuebase/client";
export const { jobs } = createQueuebaseClient({
apiKey: process.env.NEXT_PUBLIC_QUEUEBASE_API_KEY!,
});
```
--------------------------------
### Invoke Queuebase jobs
Source: https://github.com/brockherion/queuebase/blob/main/packages/queuebase/README.md
Demonstrates how to trigger defined jobs using the Queuebase client in both client-side components and server actions.
```jsx
"use client";
import { jobs } from "@/utils/queuebase";
export default function ExecuteJob() {
return (
);
}
```
```typescript
"use server";
import { jobs } from "@/utils/queuebase";
export default async function executeServer() {
await jobs("sayHello").enqueue();
}
```
--------------------------------
### Adding Logging to Queuebase Job Handlers (TypeScript)
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/guides/logging.mdx
Demonstrates how to use the 'logger' object provided by Queuebase within a job handler function to log messages. The logger outputs to the console and the Queuebase dashboard. It's exposed as a parameter in the handler.
```typescript
export const jobRouter = {
sayHello: j().handler(({ logger }) => {
logger.info("Hello there!");
}),
} satisfies QueuebaseJobRouter;
```
--------------------------------
### Create Next.js API Route Handler for Queuebase
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/getting-started/pagesdir.mdx
Sets up the API route handler in Next.js to process incoming Queuebase job requests. This handler is crucial for receiving job triggers and executing the defined job router logic.
```typescript
// api/queuebase.ts
import { createPagesApiHandler } from "queuebase/next";
const handler = createPagesApiHandler({
router: jobRouter,
});
export default handler;
```
--------------------------------
### Define Queuebase job router
Source: https://github.com/brockherion/queuebase/blob/main/packages/queuebase/README.md
Creates a type-safe job router using Queuebase and Zod. This file acts as the central registry for all background jobs.
```typescript
import { type JobRouter as QueuebaseJobRouter } from "queuebase/lib/types";
import { createQueuebase } from "queuebase/next";
import { z } from "zod";
export const jobRouter = {
sayHello: j().handler(() => {
console.log("Hello there!");
}),
} satisfies QueuebaseJobRouter;
export type JobRouter = typeof jobRouter;
```
--------------------------------
### Define Queuebase job router
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/getting-started/appdir.mdx
Create a type-safe job router using the Queuebase library. This defines the available jobs and their respective handlers.
```typescript
// api/queuebase/core.ts
import { type JobRouter as QueuebaseJobRouter } from "queuebase/lib/types";
import { createQueuebase } from "queuebase/next";
import { z } from "zod";
const j = createQueuebase();
export const jobRouter = {
sayHello: j().handler(() => {
console.log("Hello there!");
}),
} satisfies QueuebaseJobRouter;
export type JobRouter = typeof jobRouter;
```
--------------------------------
### Execute Queuebase jobs
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/getting-started/appdir.mdx
Trigger jobs using the enqueue method within Next.js client components or server actions.
```jsx
// app/execute-job.tsx
"use client";
import { jobs } from "@/utils/queuebase";
export default function ExecuteJob() {
return (
);
}
```
```typescript
"use server";
import { jobs } from "@/utils/queuebase";
export default async function executeServer() {
await jobs("sayHello").enqueue();
}
```
--------------------------------
### Create Queuebase Job Router in Next.js
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/getting-started/pagesdir.mdx
Defines a job router for Queuebase within a Next.js application's API routes. This TypeScript code snippet shows how to create a simple 'sayHello' job using Queuebase's handler.
```typescript
// api/queuebase.ts
import { type JobRouter as QueuebaseJobRouter } from "queuebase/lib/types";
import { createQueuebase } from "queuebase/next";
const j = createQueuebase();
export const jobRouter = {
sayHello: j().handler(() => {
console.log("Hello there!");
}),
} satisfies QueuebaseJobRouter;
export type JobRouter = typeof jobRouter;
```
--------------------------------
### Define a CRON job in Queuebase router
Source: https://github.com/brockherion/queuebase/blob/main/docs/pages/guides/cron.mdx
Defines a standard job handler within the Queuebase job router that can be triggered periodically. The job is defined using the Queuebase router syntax and requires a unique route name.
```typescript
// api/queuebase/core.ts
export const jobRouter = {
sendWeeklyReport: j().handler(async () => {
console.log("Sending weekly report...");
}),
} satisfies QueuebaseJobRouter;
```
--------------------------------
### Create Queuebase route handler
Source: https://github.com/brockherion/queuebase/blob/main/packages/queuebase/README.md
Exposes the job router via a Next.js POST route handler. This endpoint processes incoming job requests from the Queuebase service.
```typescript
import { createRouteHandler } from "queuebase/next";
import { jobRouter } from "./core";
export const { POST } = createRouteHandler({
router: jobRouter,
});
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.