Skip to content

Commit

Permalink
Convert remove-stale-job to TS (#23538)
Browse files Browse the repository at this point in the history
* chore(ts): migrate remove-stale-jobs to typescript

* test: update related test files

* fix(type): fix some typecheck errors
  • Loading branch information
gabrieluizramos authored Apr 29, 2020
1 parent 2c75a81 commit b35c398
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
jest.mock(`../../utils/jobs-manager`)

const { isJobStale } = require(`../../utils/jobs-manager`)
const { internalActions, publicActions } = require(`../../redux/actions`)
import { isJobStale } from "../../utils/jobs-manager"
import { internalActions, publicActions } from "../../redux/actions"

jest.spyOn(internalActions, `removeStaleJob`)

const removeStaleJobs = require(`../remove-stale-jobs`)
import { removeStaleJobs } from "../remove-stale-jobs"

describe(`remove-stale-jobs`, () => {
let state
Expand All @@ -19,7 +19,7 @@ describe(`remove-stale-jobs`, () => {
}

publicActions.createJobV2 = jest.fn()
internalActions.removeStaleJob.mockClear()
;(internalActions.removeStaleJob as jest.Mock).mockClear()
})

it(`should remove stale jobs from complete cache`, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const report = require(`gatsby-cli/lib/reporter`)
import { getConfigFile } from "./get-config-file"
const tracer = require(`opentracing`).globalTracer()
const preferDefault = require(`./prefer-default`)
const removeStaleJobs = require(`./remove-stale-jobs`)
import { removeStaleJobs } from "./remove-stale-jobs"

// Show stack trace on unhandled promises.
process.on(`unhandledRejection`, (reason, p) => {
Expand Down
25 changes: 0 additions & 25 deletions packages/gatsby/src/bootstrap/remove-stale-jobs.js

This file was deleted.

35 changes: 35 additions & 0 deletions packages/gatsby/src/bootstrap/remove-stale-jobs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
IGatsbyState,
IRemoveStaleJobAction,
IGatsbyJobV2,
} from "../redux/types"

import { isJobStale } from "../utils/jobs-manager"
import { publicActions, internalActions } from "../redux/actions"

export const removeStaleJobs = (
state: IGatsbyState
): IRemoveStaleJobAction[] => {
const actions: IRemoveStaleJobAction[] = []

// If any of our finished jobs are stale we remove them to keep our cache small
state.jobsV2.complete.forEach(
(job: IGatsbyJobV2, contentDigest: string): void => {
if (isJobStale(job)) {
actions.push(internalActions.removeStaleJob(contentDigest))
}
}
)

// If any of our pending jobs do not have an existing inputPath or the inputPath changed
// we remove it from the queue as they would fail anyway
state.jobsV2.incomplete.forEach(({ job, plugin }: IGatsbyJobV2): void => {
if (isJobStale(job)) {
actions.push(internalActions.removeStaleJob(job.contentDigest))
} else {
actions.push(publicActions.createJobV2(job, plugin))
}
})

return actions
}
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/actions/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export const pageQueryRun = (
*/
export const removeStaleJob = (
contentDigest: string,
plugin: IGatsbyPlugin,
plugin?: IGatsbyPlugin,
traceId?: string
): IRemoveStaleJobAction => {
return {
Expand Down
17 changes: 14 additions & 3 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ export interface IGatsbyStaticQueryComponents {

type GatsbyNodes = Map<string, IGatsbyNode>

export interface IGatsbyJobContent {
inputPaths: string[]
contentDigest: string
}

export interface IGatsbyJobV2 {
job: IGatsbyJobContent
plugin: IGatsbyPlugin
traceId?: string
}

export interface IGatsbyState {
program: IProgram
nodes: GatsbyNodes
Expand Down Expand Up @@ -156,8 +167,8 @@ export interface IGatsbyState {
done: any[] // TODO
}
jobsV2: {
incomplete: Map<any, any> // TODO
complete: Map<any, any>
incomplete: Map<Identifier, IGatsbyJobV2>
complete: Map<Identifier, IGatsbyJobV2>
}
webpack: any // TODO This should be the output from ./utils/webpack.config.js
webpackCompilationHash: string
Expand Down Expand Up @@ -314,7 +325,7 @@ export interface IPageQueryRunAction {

export interface IRemoveStaleJobAction {
type: `REMOVE_STALE_JOB_V2`
plugin: IGatsbyPlugin
plugin: IGatsbyPlugin | undefined
traceId?: string
payload: { contentDigest: string }
}
Expand Down

0 comments on commit b35c398

Please sign in to comment.