Skip to content

Commit

Permalink
Naming updates, handle all job status cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yscik committed Jul 31, 2020
1 parent aa61172 commit ad1806d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
58 changes: 38 additions & 20 deletions assets/data-port/export/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
const EXPORT_REST_API = '/sensei-internal/v1/export';

/**
* @typedef Job
* @typedef JobResponse
*
* @property {Object} status Job status.
* @property {string} status.status Job status name.
Expand All @@ -21,16 +21,16 @@ const EXPORT_REST_API = '/sensei-internal/v1/export';
*/

/**
* Set job state.
* Set job state. Clears request error.
*
* @param {Job} job Job state.
* @param {JobResponse} job Job state.
*/
export const setJob = ( job ) => ( { type: 'SET_JOB', job } );

/**
* Set error.
* Set request error.
*
* @param {string} error Error message.#
* @param {string} error Error message.
*/
export const setRequestError = ( error ) => ( { type: 'SET_ERROR', error } );

Expand All @@ -50,9 +50,12 @@ export const start = function* ( types ) {
status: 'started',
percentage: 0,
} );

yield createJob();
const jobId = yield select( EXPORT_STORE, 'getJobId' );
if ( ! jobId ) {
yield createJob();
}
yield startJob( types );
yield update();
};

/**
Expand All @@ -61,8 +64,8 @@ export const start = function* ( types ) {
* @access public
*/
export const reset = function* () {
yield clearJob();
yield clearSchedule();
yield clearJob();
};

/**
Expand All @@ -82,14 +85,28 @@ export const cancel = function* () {
* Update job state from REST API.
*/
export const update = function* () {
yield sendJobRequest();
const job = yield sendJobRequest();

// TODO handle outdated response

if ( job && ! job.error && 'pending' === job.status.status ) {
yield schedule( update, 1000 );
}
};

/**
* Check if there is an active job and load it.
*/
export const checkForActiveJob = function* () {
yield sendJobRequest( { jobId: 'active' } );
const job = yield* sendJobRequest( { jobId: 'active' } );

if ( job ) {
if ( 'setup' === job.status.status ) {
yield cancel();
} else {
yield update();
}
}
};

/**
Expand All @@ -98,6 +115,7 @@ export const checkForActiveJob = function* () {
* @param {Object} options apiFetch request object.
* @param {string?} options.path Request sub-path in exporter API.
* @param {string?} options.jobId Override job ID
* @return {JobResponse} job
*/
export const sendJobRequest = function* ( options = {} ) {
let { path, jobId, ...requestOptions } = options;
Expand All @@ -106,11 +124,11 @@ export const sendJobRequest = function* ( options = {} ) {
jobId = yield select( EXPORT_STORE, 'getJobId' );
if ( ! jobId ) {
yield setRequestError( 'No job ID' );
return;
return undefined;
}
}

yield sendRequest( { path, jobId, ...requestOptions } );
return yield* sendRequest( { path, jobId, ...requestOptions } );
};

/**
Expand All @@ -128,7 +146,8 @@ export const sendRequest = function* ( options = {} ) {
try {
const job = yield apiFetch( { path, ...requestOptions } );

yield handleResult( job );
if ( ! job || ! jobId || jobId === job.id || 'active' === jobId )
return yield handleJobResponse( job );
} catch ( error ) {
if (
'active' === jobId &&
Expand Down Expand Up @@ -166,16 +185,15 @@ export const startJob = function* ( types ) {
* Set job state from response.
* Start polling for changes if the job status is not completed.
*
* @param {Job} job
* @param {JobResponse} job
* @return {JobResponse} Job.
*/
export const handleResult = function* ( job ) {
export const handleJobResponse = function* ( job ) {
if ( ! job || ! job.id || job.deleted ) {
return yield clearJob();
yield clearJob();
return undefined;
}
const { status, error } = job;
yield setJob( job );

if ( ! error && 'pending' === status.status ) {
yield* schedule( update, 1000 );
}
return job;
};
3 changes: 1 addition & 2 deletions assets/data-port/export/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ const selectors = {

export const EXPORT_STORE = 'sensei/export';

const registerExportStore = () => {
const registerExportStore = () =>
registerStore( EXPORT_STORE, {
reducer: createReducerFromActionMap( reducers, {} ),
actions,
selectors,
resolvers,
controls: { ...dataControls, ...scheduleControls },
} );
};

export default registerExportStore;

0 comments on commit ad1806d

Please sign in to comment.