activate: async()

in labextension/src/index.ts [35:92]


  activate: async (app: JupyterFrontEnd,
    restorer: ILayoutRestorer,
    iconRegistry: IIconRegistry,
    rendermime: IRenderMimeRegistry,
    stateDB: IStateDB,) => {
    console.log('JupyterLab extension sagemaker-studio-autoshutdown is activated!');

    let IDLE_TIME = 120
    let KEEP_TERMINALS = false
    const KEY = 'sagemaker-auto-shutdown:settings:data';

    // Create the schedule widget sidebar
    const autoShutDownWidget = new AutoShutDownWidget(stateDB);
    autoShutDownWidget.id = 'jp-autoshutdown';
    autoShutDownWidget.title.iconClass = 'jp-SideBar-tabIcon jp-sampleIcon autoshutdown-sidebar-icon';
    autoShutDownWidget.title.caption = 'Auto ShutDown';

    // Let the application restorer track the running panel for restoration of
    // application state (e.g. setting the running panel as the current side bar
    // widget).
    restorer.add(autoShutDownWidget, 'autoshutdown-sidebar');

    // Rank has been chosen somewhat arbitrarily to give priority to the running
    // sessions widget in the sidebar.
    app.shell.add(autoShutDownWidget, 'left', { rank: 220 });
    // POST request

    app.restored.then(() => stateDB.fetch(KEY)).then((s) => {
      const state = s as ReadonlyJSONObject;
      if (state) {
        if (state['IDLE_TIME']) {
          console.log(state['IDLE_TIME'])
          IDLE_TIME = Number(state['IDLE_TIME'])
        }
        if (state['keepTerminals']) {
          console.log(state['keepTerminals'])
          KEEP_TERMINALS = Boolean(state['keepTerminals'])
        }
      }
    }).then(async () => {
      const dataToSend = {
        idle_time: IDLE_TIME,
        keep_terminals: KEEP_TERMINALS
      };
      try {
        const reply = await requestAPIServer<any>('idle_checker', {
          body: JSON.stringify(dataToSend),
          method: 'POST'
        });
        console.log(reply);
      } catch (reason) {
        console.error(
          `Error on POST /sagemaker-studio-autoshutdown/idle_checker ${dataToSend}.\n${reason}`
        );
      }
    });

  }