summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/stores/modules/terminal/actions/session_status.js
blob: 4aa0768d39441883e09bfb8e0eac3fdf97ea53c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { createAlert } from '~/flash';
import axios from '~/lib/utils/axios_utils';
import * as messages from '../messages';
import * as types from '../mutation_types';
import { isEndingStatus } from '../utils';

export const pollSessionStatus = ({ state, dispatch, commit }) => {
  dispatch('stopPollingSessionStatus');
  dispatch('fetchSessionStatus');

  const interval = setInterval(() => {
    if (!state.session) {
      dispatch('stopPollingSessionStatus');
    } else {
      dispatch('fetchSessionStatus');
    }
  }, 5000);

  commit(types.SET_SESSION_STATUS_INTERVAL, interval);
};

export const stopPollingSessionStatus = ({ state, commit }) => {
  const { sessionStatusInterval } = state;

  if (!sessionStatusInterval) {
    return;
  }

  clearInterval(sessionStatusInterval);

  commit(types.SET_SESSION_STATUS_INTERVAL, 0);
};

export const receiveSessionStatusSuccess = ({ commit, dispatch }, data) => {
  const status = data && data.status;

  commit(types.SET_SESSION_STATUS, status);

  if (isEndingStatus(status)) {
    dispatch('killSession');
  }
};

export const receiveSessionStatusError = ({ dispatch }) => {
  createAlert({ message: messages.UNEXPECTED_ERROR_STATUS });
  dispatch('killSession');
};

export const fetchSessionStatus = ({ dispatch, state }) => {
  if (!state.session) {
    return;
  }

  const { showPath } = state.session;

  axios
    .get(showPath)
    .then(({ data }) => {
      dispatch('receiveSessionStatusSuccess', data);
    })
    .catch((error) => {
      dispatch('receiveSessionStatusError', error);
    });
};