<commit_before>// @flow

import childProcess from 'child_process';
import geoip from 'geoip-lite';
import cluster from 'cluster';

import { handleAsyncPromise } from '../responders/handlers';
import geoipLicense from '../../secrets/geoip_license';

function updateGeoipDB(): Promise<void> {
  const spawned = childProcess.spawn(
    process.execPath,
    [
      '../node_modules/geoip-lite/scripts/updatedb.js',
      `license_key=${geoipLicense.key}`,
    ],
  );
  return new Promise((resolve, reject) => {
    spawned.on('error', reject);
    spawned.on('exit', () => resolve());
  });
}

function reloadGeoipDB(): Promise<void> {
  return new Promise(resolve => geoip.reloadData(resolve));
}

type IPCMessage = {|
  type: 'geoip_reload',
|};
const reloadMessage: IPCMessage = { type: 'geoip_reload' };

async function updateAndReloadGeoipDB(): Promise<void> {
  await updateGeoipDB();
  await reloadGeoipDB();<commit_msg>[server] Don't require server/secrets/geoip_license.json to exist<commit_after>// @flow

import childProcess from 'child_process';
import geoip from 'geoip-lite';
import cluster from 'cluster';

import { handleAsyncPromise } from '../responders/handlers';

let cachedGeoipLicense = undefined;
async function getGeoipLicense() {
  if (cachedGeoipLicense !== undefined) {
    return cachedGeoipLicense;
  }
  try {
    const geoipLicenseImport = await import('../../secrets/geoip_license');
    if (cachedGeoipLicense !== undefined) {
      cachedGeoipLicense = geoipLicenseImport.default;
    }
  } catch {
    if (cachedGeoipLicense !== undefined) {
      cachedGeoipLicense = null;
    }
  }
  return cachedGeoipLicense;
}

async function updateGeoipDB(): Promise<void> {
  const geoipLicense = await getGeoipLicense();
  if (!geoipLicense) {
    console.log('no server/secrets/geoip_license.json so skipping update');
    return;
  }
  await spawnUpdater(geoipLicense);
}

function spawnUpdater(geoipLicense: { key: string }): Promise<void> {
  const spawned = childProcess.spawn(
    process.execPath,
    [
      '../node_modules/geoip-lite/scripts/updatedb.js',
      `license_key=${geoipLicense.key}`,
    ],
  );
  return new Promise((resolve, reject) => {
    spawned.on('error', reject);
    spawned.on('exit', () => resolve());
  });
}

function reloadGeoipDB(): Promise<void> {
  return new Promise(resolve => geoip.reloadData(resolve));
}

type IPCMessage = {|
  type: 'geoip_reload',
|};
const reloadMessage: IPCMessage = { type: 'geoip_reload' };

async function updateAndReloadGeoipDB(): Promise<void> {
  await updateGeoipDB();
  await reloadGeoipDB();
{'fraction_non_alphanumeric': 0.14340786430223593, 'fraction_numerical': 0.0, 'mean_word_length': 4.350515463917525, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 16, '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': '801374', 'n_tokens_mistral': 854, 'n_tokens_neox': 842, 'n_words': 175}