Skip to content

Commit

Permalink
Fixes yarn rw jobs clear command (#11578)
Browse files Browse the repository at this point in the history
Closes #11577

---------

Co-authored-by: Josh GM Walker <[email protected]>
  • Loading branch information
cannikin and Josh-Walker-GM authored Sep 19, 2024
1 parent 0081d3a commit d27b0ce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions .changesets/11578.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixes `yarn rw jobs clear` command (#11578) by @cannikin
23 changes: 22 additions & 1 deletion packages/jobs/src/bins/__tests__/rw-jobs.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { mockLogger } from '../../core/__tests__/mocks.js'
import { buildNumWorkers, startWorkers } from '../rw-jobs.js'
import { buildNumWorkers, clearQueue, startWorkers } from '../rw-jobs.js'

vi.mock('@redwoodjs/cli-helpers/loadEnvFiles', () => {
return {
Expand Down Expand Up @@ -123,3 +123,24 @@ describe('startWorkers()', () => {
)
})
})

describe('clearQueue()', () => {
beforeEach(() => {
vi.resetAllMocks()
})

it('forks a single worker', () => {
const mockWorker = {
on: () => {},
}
mocks.fork.mockImplementation(() => mockWorker)

clearQueue({ logger: mockLogger })

// single worker only
expect(mocks.fork).toHaveBeenCalledWith(
expect.stringContaining('rw-jobs-worker.js'),
['--clear', '--index', '0', '--id', '0'],
)
})
})
10 changes: 6 additions & 4 deletions packages/jobs/src/bins/rw-jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ setupEnv()

process.title = 'rw-jobs'

const WORKER_PATH = path.join(__dirname, 'rw-jobs-worker.js')

const parseArgs = (argv: string[]) => {
const commandString = hideBin(argv)

Expand Down Expand Up @@ -125,7 +127,7 @@ export const startWorkers = ({
}

// fork the worker process
const worker = fork(path.join(__dirname, 'rw-jobs-worker.js'), workerArgs, {
const worker = fork(WORKER_PATH, workerArgs, {
detached: detach,
stdio: detach ? 'ignore' : 'inherit',
env: process.env,
Expand All @@ -143,7 +145,7 @@ export const startWorkers = ({
}

// TODO add support for stopping with SIGTERM or SIGKILL?
const stopWorkers = async ({
export const stopWorkers = async ({
numWorkers,
signal = 'SIGINT',
logger,
Expand Down Expand Up @@ -174,9 +176,9 @@ const stopWorkers = async ({
}
}

const clearQueue = ({ logger }: { logger: BasicLogger }) => {
export const clearQueue = ({ logger }: { logger: BasicLogger }) => {
logger.warn(`Starting worker to clear job queue...`)
fork(path.join(__dirname, 'rw-jobs-worker.js'), ['--clear'])
fork(WORKER_PATH, ['--clear', '--index', '0', '--id', '0'])
}

const signalSetup = ({
Expand Down

0 comments on commit d27b0ce

Please sign in to comment.