<commit_before>/**
 * @module fs-kernels
 */
import { ExecaChildProcess } from "execa";
import pidusage from "pidusage";

import { Observable, Observer, of, merge } from "rxjs";
import {
  map,
  mergeMap,
  catchError,
  timeout,
  first,
  toArray
} from "rxjs/operators";

import {
  Channels,
  shutdownRequest,
  childOf,
  ofMessageType
} from "@nteract/messaging";
import { createKernelRef, KernelRef } from "@nteract/types";

import { JupyterConnectionInfo } from "enchannel-zmq-backend";

import { KernelSpec } from "./kernelspecs";
import { cleanup, launch, LaunchedKernel, launchSpec } from "./spawnteract";

interface Usage {
  /**
   * percentage (from 0 to 100*number of cores)
   */
  cpu: number;

  /**
   * bytes
   */
  memory: number;

  /**
   * PPID - Parent PID
   */
  ppid: number;

  /**
   * PID - Process ID
   */
  pid: number;

  /**
   * ms user + system time (in local time)
   */
  ctime: number;

  /**
   * ms since the start of the process (local time)
   */
  elapsed: number;

  /**
   * ms since epoch (local time)
   */
  timestamp: number;
}

export class Kernel {
  id: KernelRef;
  kernelSpec: KernelSpec;
  process: ExecaChildProcess;
  connectionInfo: JupyterConnectionInfo;
  connectionFile: string;
  channels: Channels;

  constructor(launchedKernel: LaunchedKernel) {
    this.id = createKernelRef();
    this.process = launchedKernel.spawn;
    this.connectionInfo = launchedKernel.config;
    this.kernelSpec = launchedKernel.kernelSpec;
    this.connectionFile = launchedKernel.connectionFile;
    this.channels = launchedKernel.channels;
  }

  shutdownEpic(timeoutMs: number = 2000) {
    const request = shutdownRequest({ restart: false });

    // Try to make a shutdown request
    // If we don't get a response within X time, force a shutdown
    // Either way do the same cleanup
    const shutDownHandling = this.channels.pipe(
      childOf(request),
      ofMessageType("shutdown_reply"),
      first(),
      // If we got a reply, great! :)
      map((msg: { content: { restart: boolean } }) => {
        return {
          status: "shutting down",
          content: msg.content,
          id: this.id
        };
      }),
      // If we don't get a response within 2s, assume failure :(
      timeout(timeoutMs),
      catchError(err => of({ error: err, id: this.id })),
      /**
       * Even if we don't receive a shutdown_reply from the kernel to our
       * shutdown_request, we will go forward with cleaning up the RxJS
       * subject and killing the kernel process.
       */
      mergeMap(async () => {
        // End all communication on the channels
        this.channels.complete();
        await this.shutdownProcess();
        return of({ status: "shutdown", id: this.id });
      }),
      catchError(err =>
        // Catch all, in case there were other errors here
        of({ error: err, id: this.id, status: "error" })
      )
    );
<commit_msg>Clean up comments on shutdown epic<commit_after>/**
 * @module fs-kernels
 */
import { ExecaChildProcess } from "execa";
import pidusage from "pidusage";

import { Observable, Observer, of, merge } from "rxjs";
import {
  map,
  mergeMap,
  catchError,
  timeout,
  first,
  toArray
} from "rxjs/operators";

import {
  Channels,
  shutdownRequest,
  childOf,
  ofMessageType
} from "@nteract/messaging";
import { createKernelRef, KernelRef } from "@nteract/types";

import { JupyterConnectionInfo } from "enchannel-zmq-backend";

import { KernelSpec } from "./kernelspecs";
import { cleanup, launch, LaunchedKernel, launchSpec } from "./spawnteract";

interface Usage {
  /**
   * percentage (from 0 to 100*number of cores)
   */
  cpu: number;

  /**
   * bytes
   */
  memory: number;

  /**
   * PPID - Parent PID
   */
  ppid: number;

  /**
   * PID - Process ID
   */
  pid: number;

  /**
   * ms user + system time (in local time)
   */
  ctime: number;

  /**
   * ms since the start of the process (local time)
   */
  elapsed: number;

  /**
   * ms since epoch (local time)
   */
  timestamp: number;
}

export class Kernel {
  id: KernelRef;
  kernelSpec: KernelSpec;
  process: ExecaChildProcess;
  connectionInfo: JupyterConnectionInfo;
  connectionFile: string;
  channels: Channels;

  constructor(launchedKernel: LaunchedKernel) {
    this.id = createKernelRef();
    this.process = launchedKernel.spawn;
    this.connectionInfo = launchedKernel.config;
    this.kernelSpec = launchedKernel.kernelSpec;
    this.connectionFile = launchedKernel.connectionFile;
    this.channels = launchedKernel.channels;
  }

  shutdownEpic(timeoutMs: number = 2000) {
    const request = shutdownRequest({ restart: false });

    // Try to make a shutdown request
    // If we don't get a response within X time, force a shutdown
    // Either way do the same cleanup
    const shutDownHandling = this.channels.pipe(
      /* Get the first response to our message request. */
      childOf(request),
      ofMessageType("shutdown_reply"),
      first(),
      // If we got a reply, great! :)
      map((msg: { content: { restart: boolean } }) => {
        return {
          status: "shutting down",
          content: msg.content,
          id: this.id
        };
      }),
      /**
       * If we don't get a response within timeoutMs, then throw an error.
       */
      timeout(timeoutMs),
      catchError(err => of({ error: err, id: this.id })),
      /**
       * Even if we don't receive a shutdown_reply from the kernel to our
       * shutdown_request, we will go forward with cleaning up the RxJS
       * subject and killing the kernel process.
       */
      mergeMap(async () => {
        // End all communication on the channels
        this.channels.complete();
        await this.shutdownProcess();
        return of({ status: "shutdown", id: this.id });
      }),
      catchError(err =>
        // Catch all, in case there were other errors here
        of({ error: err, id: this.id, status: "error" })
      )
    );

{'fraction_non_alphanumeric': 0.11743057878889261, 'fraction_numerical': 0.002843760455001673, 'mean_word_length': 2.617059891107078, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4718621', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1788, 'n_words': 551}