Skip to content

Commit

Permalink
fix(jobs): Make deleteSuccessfulJobs configurable (#11459)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Sep 6, 2024
1 parent 7ee159b commit 1f81c6e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changesets/11459.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- fix(jobs): Make deleteSuccessfulJobs configurable (#11459) by @Tobbe

Make the jobs Executor respect the `deleteSuccessfulJobs` config option
18 changes: 9 additions & 9 deletions packages/jobs/src/core/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AdapterRequiredError, JobRequiredError } from '../errors.js'
import { loadJob } from '../loaders.js'
import type { BaseJob, BasicLogger } from '../types.js'

interface Options {
export interface ExecutorOptions {
adapter: BaseAdapter
job: BaseJob
logger?: BasicLogger
Expand All @@ -28,15 +28,15 @@ export const DEFAULTS = {
}

export class Executor {
options: Required<Options>
adapter: Options['adapter']
logger: NonNullable<Options['logger']>
options: Required<ExecutorOptions>
adapter: ExecutorOptions['adapter']
logger: NonNullable<ExecutorOptions['logger']>
job: BaseJob
maxAttempts: NonNullable<Options['maxAttempts']>
deleteFailedJobs: NonNullable<Options['deleteFailedJobs']>
deleteSuccessfulJobs: NonNullable<Options['deleteSuccessfulJobs']>
maxAttempts: NonNullable<ExecutorOptions['maxAttempts']>
deleteFailedJobs: NonNullable<ExecutorOptions['deleteFailedJobs']>
deleteSuccessfulJobs: NonNullable<ExecutorOptions['deleteSuccessfulJobs']>

constructor(options: Options) {
constructor(options: ExecutorOptions) {
this.options = { ...DEFAULTS, ...options }

// validate that everything we need is available
Expand Down Expand Up @@ -68,7 +68,7 @@ export class Executor {

await this.adapter.success({
job: this.job,
deleteJob: DEFAULT_DELETE_SUCCESSFUL_JOBS,
deleteJob: this.deleteSuccessfulJobs,
})
} catch (error: any) {
this.logger.error(
Expand Down
33 changes: 33 additions & 0 deletions packages/jobs/src/core/__tests__/Executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DEFAULT_LOGGER } from '../../consts.js'
import * as errors from '../../errors.js'
import type { BaseJob } from '../../types.js'
import { Executor } from '../Executor.js'
import type { ExecutorOptions } from '../Executor.js'

import { MockAdapter, mockLogger } from './mocks.js'

Expand Down Expand Up @@ -146,6 +147,38 @@ describe('perform', () => {
})
})

it('keeps the job around after successful job if instructed to do so', async () => {
const mockAdapter = new MockAdapter()
const mockJob = {
id: 1,
name: 'TestJob',
path: 'TestJob/TestJob',
args: ['foo'],
attempts: 0,

perform: vi.fn(),
}
const options: ExecutorOptions = {
adapter: mockAdapter,
logger: mockLogger,
job: mockJob,
deleteSuccessfulJobs: false,
}
const executor = new Executor(options)

// spy on the success function of the adapter
const adapterSpy = vi.spyOn(mockAdapter, 'success')
// mock the `loadJob` loader to return the job mock
mocks.loadJob.mockImplementation(() => mockJob)

await executor.perform()

expect(adapterSpy).toHaveBeenCalledWith({
job: options.job,
deleteJob: false,
})
})

it('invokes the `failure` method on the adapter when job fails', async () => {
const mockAdapter = new MockAdapter()
const mockError = new Error('mock error in the job perform method')
Expand Down

0 comments on commit 1f81c6e

Please sign in to comment.