Sha256: 9ea7a81a2c9d592a4100250e8c137bc80f03d6d4d87cdcd5db42967266afdc32

Contents?: true

Size: 1.98 KB

Versions: 50

Compression:

Stored size: 1.98 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 => async (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) {
      try {
        const data = await API.get(url);
        onGetJobInvocationsSuccess(data);
      } catch (error) {
        onGetJobInvocationsFailed(error);
      } finally {
        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

50 entries across 50 versions & 1 rubygems

Version Path
foreman_remote_execution-8.1.2 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-8.1.1 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-8.1.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-7.2.2 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-7.2.1 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-7.2.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-5.0.8 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-8.0.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-7.1.1 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-7.1.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-7.0.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-5.0.7 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-5.0.6 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-6.2.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-5.0.5 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-6.1.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-5.0.4 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-5.0.3 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-6.0.0 webpack/react_app/redux/actions/jobInvocations/index.js
foreman_remote_execution-5.0.2 webpack/react_app/redux/actions/jobInvocations/index.js