Sha256: 3fe0d13558013f91bf7cea8fe8b9df4daf07b8c1025cda43be85d2a111ec43bf

Contents?: true

Size: 1.9 KB

Versions: 40

Compression:

Stored size: 1.9 KB

Contents

// eslint-disable-next-line import/no-extraneous-dependencies
import API from 'foremanReact/API';

import {
  JOB_INVOCATIONS_GET_JOB_INVOCATIONS,
  JOB_INVOCATIONS_POLLING_STARTED,
  JOB_INVOCATIONS_JOB_FINISHED,
} from '../../consts';

const defaultJobInvocationsPollingInterval = 1000;
const jobInvocationsInterval = process.env.JOB_INVOCATIONS_POLLING ||
  defaultJobInvocationsPollingInterval;

const getJobInvocations = url => (dispatch, getState) => {
  function onGetJobInvocationsSuccess({ data }) {
    // If the job has finished, stop polling
    if (data.finished) {
      dispatch({
        type: JOB_INVOCATIONS_JOB_FINISHED,
        payload: {
          jobInvocations: data,
        },
      });
    } else {
      dispatch({
        type: JOB_INVOCATIONS_GET_JOB_INVOCATIONS,
        payload: {
          jobInvocations: data,
        },
      });
    }
  }

  function onGetJobInvocationsFailed(error) {
    if (error.response.status === 401) {
      window.location.replace('/users/login');
    }
  }

  function triggerPolling() {
    if (jobInvocationsInterval) {
      setTimeout(
        () => dispatch(getJobInvocations(url)),
        jobInvocationsInterval,
      );
    }
  }

  const isDocumentVisible =
    document.visibilityState === 'visible' ||
    document.visibilityState === 'prerender';

  if (getState().foremanRemoteExecutionReducers.jobInvocations.isPolling) {
    if (isDocumentVisible) {
      API.get(url)
        .then(onGetJobInvocationsSuccess)
        .catch(onGetJobInvocationsFailed)
        .then(triggerPolling);
    } else {
      // document is not visible, keep polling without api call
      triggerPolling();
    }
  }
};

export const startJobInvocationsPolling = url => (dispatch, getState) => {
  if (getState().foremanRemoteExecutionReducers.jobInvocations.isPolling) {
    return;
  }
  dispatch({
    type: JOB_INVOCATIONS_POLLING_STARTED,
  });
  dispatch(getJobInvocations(url));
};

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
foreman_remote_execution-3.2.2 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-3.2.1 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-3.2.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-3.1.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-2.0.10 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-2.0.9 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-2.0.8 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-3.0.3 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-3.0.2 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-2.0.7 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-3.0.1 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-3.0.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-2.0.6 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-2.0.5 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-2.0.4 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-2.0.3 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-2.0.2 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-2.0.1 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-2.0.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-1.8.4 webpack/react_app/redux/actions/jobInvocations/index.js