Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes yarn rw jobs clear command #11578

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading