Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 2 additions & 6 deletions src/server/api/cycleData/links/verifyLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { Response } from 'express'
import { CycleRequest } from 'meta/api/request/cycle'
import { AreaCode } from 'meta/area/areaCode'

import { LinksController } from 'server/controller/cycleData/links'
import { LinksService } from 'server/service/links'
import Requests from 'server/utils/requests'
import { triggerVerifyLinksWorker } from 'server/worker/tasks/verifyLinks/triggerVerifyLinksWorker'

type Request = CycleRequest<{ countryIso?: AreaCode }>

Expand All @@ -16,10 +15,7 @@ export const verifyLinks = async (req: Request, res: Response): Promise<void> =>

const user = Requests.getUser(req)

// Enqueue the verify-links job.
await LinksController.verify({ assessment, countryIso, cycle, user })
// Ensure the worker dyno (or local worker) is running to consume the queue.
await triggerVerifyLinksWorker()
await LinksService.enqueueAllLinksValidation({ assessment, countryIso, cycle, user })

Requests.send(res)
} catch (e) {
Expand Down
2 changes: 0 additions & 2 deletions src/server/controller/cycleData/links/linksController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { LinkRepository } from 'server/db/repository/assessmentCycle/links'
import { scheduleVerifyAllLinks } from 'server/worker/tasks/verifyLinks/visitCycleLinks/scheduleVerifyAllLinks'

import { getActiveVerifyJob } from './getActiveVerifyJob'
import { getManyExport } from './getManyExport'
Expand All @@ -13,5 +12,4 @@ export const LinksController = {
getManyExport,
getVerificationSummary,
update,
verify: scheduleVerifyAllLinks,
}
2 changes: 2 additions & 0 deletions src/server/service/links/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { enqueueDescriptionLinksValidation } from 'server/service/links/enqueueDescriptionLinksValidation'
import { enqueueNationalDataPointLinksValidation } from 'server/service/links/enqueueNationalDataPointLinksValidation'
import { verifyLinks } from 'server/service/links/verifyLinks'
import { scheduleVerifyAllLinks } from 'server/worker/tasks/verifyLinks/visitCycleLinks/scheduleVerifyAllLinks'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to rename this as well .
The exported method should have the same name


export const LinksService = {
enqueueAllLinksValidation: scheduleVerifyAllLinks,
enqueueDescriptionLinksValidation,
enqueueNationalDataPointLinksValidation,
verifyLinks,
Expand Down
8 changes: 2 additions & 6 deletions src/server/worker/cronJobs/notifyLinksInvalid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { AssessmentNames } from 'meta/assessment/assessment'
import { CycleNames } from 'meta/assessment/cycle/names'

import { AssessmentController } from 'server/controller/assessment'
import { LinksController } from 'server/controller/cycleData/links'
import { UserController } from 'server/controller/user'
import { MailService } from 'server/service'
import { LinksService } from 'server/service/links'
import { ProcessEnv } from 'server/utils'
import { Job } from 'server/worker/job/job'
import { triggerVerifyLinksWorker } from 'server/worker/tasks/verifyLinks/triggerVerifyLinksWorker'
import { VerifyLinksQueueFactory } from 'server/worker/tasks/verifyLinks/visitCycleLinks/queueFactory'

const name = 'Scheduler-NotifyLinksInvalid'
Expand All @@ -29,10 +28,7 @@ export class NotifyLinksInvalid extends Job {

const user = await UserController.getUserRobot()

// get existing job or create new job and process it
const existingJob = await VerifyLinksQueueFactory.getQueuedOrActiveVerifyAllLinksJob({ assessment, cycle })
const job = existingJob ?? (await LinksController.verify({ assessment, cycle, user }))
await triggerVerifyLinksWorker()
const job = await LinksService.enqueueAllLinksValidation({ assessment, cycle, user })

const connection = { url: ProcessEnv.redisQueueUrl }
const queueEvents = new QueueEvents(VerifyLinksQueueFactory.queueName, { connection })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LinksVerificationEvent } from 'meta/socket/event/links'

import { Logger } from 'server/utils/logger'
import { VerifyLinksJobName } from 'server/worker/tasks/verifyLinks/jobNames'
import { triggerVerifyLinksWorker } from 'server/worker/tasks/verifyLinks/triggerVerifyLinksWorker'
import { emitLinksVerificationEvent } from 'server/worker/tasks/verifyLinks/utils/emitLinksVerificationEvent'
import { VerifyLinksJob } from 'server/worker/tasks/verifyLinks/verifyLinksJob'

Expand All @@ -16,19 +17,21 @@ const jobOptions: JobsOptions = {
removeOnFail: true,
}

export const scheduleVerifyAllLinks = async (props: VerifyAllLinksJobProps): Promise<Job | undefined> => {
// Returns the job performing the verification, either already queued/running or newly scheduled.
export const scheduleVerifyAllLinks = async (props: VerifyAllLinksJobProps): Promise<Job> => {
const { assessment, countryIso, cycle } = props
const scope = countryIso
? `${assessment.props.name} / ${cycle.name} / ${countryIso}`
: `${assessment.props.name} / ${cycle.name}`

// Skip if a verification job is already active.
const activeJob = await VerifyLinksQueueFactory.getQueuedOrActiveVerifyAllLinksJob(props)
const isVerificationInProgress = Boolean(activeJob)

if (isVerificationInProgress) {
if (activeJob) {
Logger.debug(`[visitCycleLinks] skipping enqueue: verification already queued or running for ${scope}`)
return undefined
// Ensure a worker is running, in case the worker was lost.
await triggerVerifyLinksWorker()
return activeJob
}

const queue = VerifyLinksQueueFactory.getInstance()
Expand All @@ -43,5 +46,7 @@ export const scheduleVerifyAllLinks = async (props: VerifyAllLinksJobProps): Pro

emitLinksVerificationEvent({ assessment, countryIso, cycle, event: LinksVerificationEvent.queued })

await triggerVerifyLinksWorker()

return job
}
Loading