Skip to content

Background Jobs

@granit/background-jobs provides framework-agnostic types for background job monitoring — mirroring Granit.BackgroundJobs on the .NET backend. @granit/react-background-jobs wraps these into React hooks with TanStack Query integration: auto-polling job status every 15 seconds, plus pause/resume/trigger mutations.

Peer dependencies: axios, react ^19, @tanstack/react-query ^5

  • Directory@granit/background-jobs/ Job status type (framework-agnostic)
    • @granit/react-background-jobs Query/mutation hooks for job monitoring
PackageRoleDepends on
@granit/background-jobsBackgroundJobStatus type definition
@granit/react-background-jobsuseBackgroundJobs, usePauseJob, useResumeJob, useTriggerJob@granit/background-jobs, @tanstack/react-query, axios, react
import { useBackgroundJobs, usePauseJob, useResumeJob, useTriggerJob } from '@granit/react-background-jobs';
import { api } from './api-client';
function JobDashboard() {
const { data: jobs, isLoading } = useBackgroundJobs({ client: api });
const { mutate: pause } = usePauseJob({ client: api });
const { mutate: resume } = useResumeJob({ client: api });
const { mutate: trigger } = useTriggerJob({ client: api });
// Render job list with action buttons...
}
interface BackgroundJobStatus {
readonly jobName: string;
readonly cronExpression: string;
readonly isEnabled: boolean;
readonly lastExecutedAt: string | null;
readonly nextExecutionAt: string | null;
readonly consecutiveFailures: number;
readonly deadLetterCount: number;
readonly lastError: string | null;
}
MethodEndpointDescription
GET/background-jobsList all jobs with current status
POST/background-jobs/{jobName}/pausePause a job
POST/background-jobs/{jobName}/resumeResume a paused job
POST/background-jobs/{jobName}/triggerManually trigger a job

All hooks accept the same options object:

interface BackgroundJobsOptions {
readonly client: AxiosInstance;
readonly basePath?: string; // default: '/api/v1/background-jobs'
}

Fetches all background jobs with 15-second auto-polling.

function useBackgroundJobs(
options: BackgroundJobsOptions
): UseQueryResult<readonly BackgroundJobStatus[]>;

Pauses a job by name. Invalidates the job list on success.

function usePauseJob(
options: BackgroundJobsOptions
): UseMutationResult<void, Error, string>;

Resumes a paused job by name. Invalidates the job list on success.

function useResumeJob(
options: BackgroundJobsOptions
): UseMutationResult<void, Error, string>;

Manually triggers a job by name. Invalidates the job list on success.

function useTriggerJob(
options: BackgroundJobsOptions
): UseMutationResult<void, Error, string>;
const backgroundJobKeys = {
all: ['background-jobs'] as const,
list: () => [...backgroundJobKeys.all, 'list'] as const,
job: (name: string) => [...backgroundJobKeys.all, 'job', name] as const,
};
CategoryKey exportsPackage
TypesBackgroundJobStatus@granit/background-jobs
Query hookuseBackgroundJobs()@granit/react-background-jobs
MutationsusePauseJob(), useResumeJob(), useTriggerJob()@granit/react-background-jobs
Cache keysbackgroundJobKeys@granit/react-background-jobs