-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
recurring scheduler: purge the scheduler
- Loading branch information
1 parent
49bcd0a
commit fa607eb
Showing
11 changed files
with
189 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
import { call } from 'redux-saga/effects' | ||
import { call, put, delay } from 'redux-saga/effects' | ||
import Debug from 'debug' | ||
import { isFuture } from 'date-fns' | ||
import scheduleFetchingRecurringAOStatus from './schedule_fetching_recurring_ao_status' | ||
import fetchRecurringAoAtomics from './fetch_recurring_ao_atomics' | ||
import scheduleRecurringAo from './schedule_recurring_ao' | ||
import AOActions from '../../actions/ao' | ||
import { RECURRING_DELAY_FOR_FETCH } from '../../helpers/recurring_ao' | ||
|
||
const debug = Debug('hfui:recurring-ao') | ||
|
||
export default function* processRecurringAO({ payload }) { | ||
const { ao: { gid, args } } = payload | ||
const { | ||
ao: { | ||
gid, args, createdAt, lastActive, | ||
}, | ||
} = payload | ||
|
||
const { startedAt, recurrence, endedAt = null } = args | ||
|
||
const shouldAtomicsBeFetched = !isFuture(new Date(startedAt)) | ||
const isOrderNew = createdAt === lastActive | ||
const shouldAtomicsBeFetched = !isFuture(new Date(startedAt)) && isOrderNew | ||
|
||
if (!shouldAtomicsBeFetched) { | ||
yield call(scheduleFetchingRecurringAOStatus, { | ||
yield call(scheduleRecurringAo, { | ||
gid, startedAt, endedAt, recurrence, | ||
}) | ||
return | ||
} | ||
yield call(fetchRecurringAoAtomics, { gid, firstDataRequest: false }) | ||
debug('fetch recurring ao %s atomics in 1.5m', gid) | ||
yield delay(RECURRING_DELAY_FOR_FETCH) | ||
yield put(AOActions.requestRecurringAoAtomics({ gid, firstDataRequest: false })) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
src/redux/sagas/ao/schedule_fetching_recurring_ao_status.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { | ||
call, delay, put, race, take, | ||
} from 'redux-saga/effects' | ||
import _max from 'lodash/max' | ||
import Debug from 'debug' | ||
import { | ||
RECURRING_DELAY_FOR_FETCH, | ||
calculateNextExecutionTime, | ||
} from '../../helpers/recurring_ao' | ||
import AOActions from '../../actions/ao' | ||
import WSActions from '../../actions/ws' | ||
import WSTypes from '../../constants/ws' | ||
|
||
const debug = Debug('hfui:recurring-ao') | ||
|
||
function* purgeSchedulerOnAlgoOrderChange({ gid, sagaToExecute }) { | ||
console.log('before race') | ||
const { cancel } = yield race({ | ||
delay: sagaToExecute, | ||
cancel: take((action) => { | ||
const { | ||
type, | ||
payload, | ||
} = action | ||
let shouldBeCanceled = false | ||
if (type === WSTypes.DATA_ALGO_ORDER) { | ||
const { ao: { gid: _gid } } = payload | ||
shouldBeCanceled = gid === _gid | ||
} | ||
if (type === WSTypes.DATA_ALGO_ORDER_STOPPED) { | ||
const { gid: _gid } = payload | ||
shouldBeCanceled = gid === _gid | ||
} | ||
if (shouldBeCanceled) { | ||
debug('scheduler of %s was cancelled', gid) | ||
} | ||
return shouldBeCanceled | ||
}), | ||
}) | ||
|
||
return cancel | ||
} | ||
|
||
function* scheduleCancellation({ gid, endedAtTime, endedAt }) { | ||
const delayTime = _max(endedAtTime - Date.now(), 0) | ||
debug('scheduled cancelation of %s', gid, { | ||
endedAt, | ||
delayTime, | ||
}) | ||
yield delay(delayTime) | ||
yield put(WSActions.cancelAlgoOrder(gid)) | ||
} | ||
|
||
function* scheduleFetching({ | ||
gid, nextExecutionTime, startedAt, endedAt, recurrence, | ||
}) { | ||
const delayTime = nextExecutionTime - Date.now() + RECURRING_DELAY_FOR_FETCH | ||
|
||
debug('scheduled fetching for %s', gid, { | ||
startedAt, | ||
endedAt, | ||
recurrence, | ||
nextExecutionTime: new Date(nextExecutionTime).toISOString(), | ||
fetchInMs: delayTime, | ||
}) | ||
|
||
yield delay(delayTime) | ||
yield put( | ||
AOActions.requestRecurringAoAtomics({ gid, firstDataRequest: false }), | ||
) | ||
} | ||
|
||
export default function* scheduleRecurringAo({ | ||
gid, | ||
startedAt, | ||
endedAt, | ||
recurrence, | ||
}) { | ||
const nextExecutionTime = calculateNextExecutionTime( | ||
startedAt, | ||
endedAt, | ||
recurrence, | ||
) | ||
const endedAtTime = endedAt ? new Date(endedAt).getTime() : null | ||
|
||
const shouldBeCancelled = endedAtTime && endedAtTime < nextExecutionTime | ||
if (shouldBeCancelled) { | ||
yield call(purgeSchedulerOnAlgoOrderChange, { | ||
gid, | ||
sagaToExecute: scheduleCancellation({ gid, endedAtTime, endedAt }), | ||
}) | ||
return | ||
} | ||
yield call(purgeSchedulerOnAlgoOrderChange, { | ||
gid, | ||
sagaToExecute: scheduleFetching({ | ||
gid, nextExecutionTime, startedAt, endedAt, recurrence, | ||
}), | ||
}) | ||
} |