Skip to content

Commit

Permalink
feat: Watch job changes when the trigger is already running
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleface committed Apr 5, 2022
1 parent 5cf479c commit 7fcdac1
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
25 changes: 21 additions & 4 deletions packages/cozy-harvest-lib/src/models/ConnectionFlow.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ export class ConnectionFlow {
this.twoFAWaiters = this.twoFAWaiters || []

this.realtime = new Realtime({ client })

this.watchCurrentJobIfTriggerIsAlreadyRunning()
}

getTwoFACodeProvider() {
Expand Down Expand Up @@ -577,16 +579,29 @@ export class ConnectionFlow {
logger.info('Found no client connector launcher')
}
}
this.watchJob({ autoSuccessTimer: computedAutoSuccessTimer })
}

/**
* If the trigger we display is already running, subscribe to the associated job
*/
watchCurrentJobIfTriggerIsAlreadyRunning() {
if (get(this, 'trigger.current_state.status') === 'running') {
this.job = {
_id: get(this, 'trigger.current_state.last_executed_job_id')
}
this.watchJob()
}
}

watchJob(options = {autoSuccessTimer: false}) {
this.realtime.subscribe(
'updated',
JOBS_DOCTYPE,
this.job._id,
this.handleJobUpdated.bind(this)
)
this.jobWatcher = watchKonnectorJob(this.client, this.job, {
autoSuccessTimer: computedAutoSuccessTimer
})
this.jobWatcher = watchKonnectorJob(this.client, this.job, options)
logger.info(`ConnectionFlow: Subscribed to ${JOBS_DOCTYPE}:${this.job._id}`)

for (const ev of JOB_EVENTS) {
Expand Down Expand Up @@ -622,7 +637,9 @@ export class ConnectionFlow {
const { status, accountError } = this.state
const triggerError = triggersModel.getKonnectorJobError(trigger)
return {
running: ![ERRORED, IDLE, SUCCESS].includes(status),
running:
get(trigger, 'current_state.status') === 'running' ||
![ERRORED, IDLE, SUCCESS].includes(status),
twoFARunning: status === RUNNING_TWOFA,
twoFARetry: status == TWO_FA_MISMATCH,
triggerError: triggerError,
Expand Down
37 changes: 36 additions & 1 deletion packages/cozy-harvest-lib/src/models/ConnectionFlow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {
launchTrigger
} from '../connections/triggers'
import CozyRealtime from 'cozy-realtime'
import KonnectorJobWatcher from './konnector/KonnectorJobWatcher'
import KonnectorJobWatcher, { watchKonnectorJob } from './konnector/KonnectorJobWatcher'
import { konnectorPolicy as biKonnectorPolicy } from '../services/budget-insight'
import fixtures from '../../test/fixtures'
import sentryHub from '../sentry'
import { Q } from 'cozy-client'

jest.mock('./konnector/KonnectorJobWatcher')
jest.mock('../sentry', () => {
const mockScope = {
setTag: jest.fn()
Expand Down Expand Up @@ -116,6 +117,17 @@ describe('ConnectionFlow', () => {
const isSubmitting = flow => {
return flow.getState().running === true
}
beforeAll(() => {
watchKonnectorJob.mockReturnValue({on: () => ({})})
})

afterEach(() => {
jest.clearAllMocks()
})

afterAll(() => {
jest.restoreAllMocks()
})

it('should render as submitting when there is no account', async () => {
const { flow } = setup()
Expand Down Expand Up @@ -213,6 +225,10 @@ describe('ConnectionFlow', () => {
userCredentials: {
login: 'foo',
password: 'bar'
},
account: {
login: 'old',
password: 'old'
}
})

Expand Down Expand Up @@ -314,6 +330,7 @@ describe('ConnectionFlow', () => {
describe('ensureTriggerAndLaunch', () => {
beforeAll(() => {
jest.spyOn(cronHelpers, 'fromFrequency').mockReturnValue('0 0 0 * * 0')
watchKonnectorJob.mockReturnValue({on: () => ({})})
})

afterEach(() => {
Expand Down Expand Up @@ -481,6 +498,24 @@ describe('ConnectionFlow', () => {
delete window.cozy
delete window.ReactNativeWebView
})

describe('contructor', () => {
beforeAll(() => {
watchKonnectorJob.mockReturnValue({on: () => ({})})
})

afterEach(() => {
jest.clearAllMocks()
})

afterAll(() => {
jest.restoreAllMocks()
})
it('should watch a running trigger', () => {
setup({trigger: fixtures.runningTrigger})
expect(watchKonnectorJob).toHaveBeenCalledWith(expect.any(Object), {_id: 'runningjobid'}, {autoSuccessTimer: false})
})
})
})

// it should have running false on trigger updates
Expand Down
17 changes: 17 additions & 0 deletions packages/cozy-harvest-lib/test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ const fixtures = {
}
}
},
runningTrigger: {
id: 'running-trigger-id',
_type: 'io.cozy.triggers',
current_state: {
status: 'running',
last_executed_job_id: 'runningjobid'
},
attributes: {
arguments: '0 0 0 * * 0',
type: '@cron',
worker: 'konnector',
message: {
account: 'updated-account-id',
konnector: 'konnectest'
}
}
},
createdTriggerWithFolder: {
id: 'created-trigger-id',
_type: 'io.cozy.triggers',
Expand Down

0 comments on commit 7fcdac1

Please sign in to comment.