Putnami
DocsGitHub

Licensed under FSL-1.1-MIT

Getting Started
Concepts
How To
Build A Web App
Build An Api Service
Share Code Between Projects
Configure Your App
Add Persistence
Add Authentication
Add Background Jobs
Principles
Tooling & Workspace
Workspace Overview
Cli
Jobs & Commands
SDK
Error Handling
Extensions
Typescript
Go
Python
Docker
Ci
Frameworks
Typescript
OverviewWebReact RoutingForms And ActionsStatic FilesApiErrors And ResponsesConfigurationLoggingHttp And MiddlewareDependency InjectionPlugins And LifecycleSessionsAuthPersistenceEventsStorageCachingWebsocketsTestingHealth ChecksTelemetryProto GrpcSmart Client
Go
OverviewHttpDependency InjectionPlugins And LifecycleConfigurationSecurityPersistenceErrorsEventsStorageCachingLoggingTelemetryGrpcService ClientsValidationOpenapiTesting
Platform
  1. DocsSeparator
  2. How ToSeparator
  3. Add Background Jobs

Add background jobs

You will run a simple in-process background task and trigger it from a route.

Steps

1) Create a job function

Create apps/web/src/jobs/send-welcome.ts:

import { useLogger } from '@putnami/runtime';

const logger = useLogger('jobs');

export async function sendWelcomeEmail(email: string) {
  await new Promise((resolve) => setTimeout(resolve, 250));
  logger.info('Sent welcome email', { email });
}

2) Trigger it from a route

Create apps/web/src/app/jobs/post.ts:

import { endpoint } from '@putnami/application';
import { sendWelcomeEmail } from '../../jobs/send-welcome';

export default endpoint(async () => {
  void sendWelcomeEmail('hello@example.com');
  return { queued: true };
});

3) Observe output

putnami serve web

Call the route, then watch the logs.

Result

You now have a background task running asynchronously in-process.

On this page

  • Add background jobs
  • Steps
  • 1) Create a job function
  • 2) Trigger it from a route
  • 3) Observe output
  • Result