<commit_before>'use strict';
/* eslint-disable no-console */

const CB = require('camda').CB;
const AWS = require('aws-sdk');

const gatherInfo = require('./lib/gatherInformation');
const scaleUp = require('./lib/scaleUp');
const scaleDown = require('./lib/scaleDown');

if(!process.env.AWS_REGION) {
    console.error('AWS_REGION not set');
    process.exit(1);
}
const as = new AWS.AutoScaling();

let ASGroupName = process.env.AS_GROUP_NAME;
let clusterName = process.env.CLUSTER_NAME;
let dryRun = process.env.DRY_RUN;
let interval = process.env.INTERVAL || 20000;
let loopsRemaining = process.env.MAX_LOOPS || 20;

if(!ASGroupName || !clusterName) {
    console.error('AS_GROUP_NAME or CLUSTER_NAME missing');
    process.exit(1);
}

//increaseASGroupCapacity :: CB (AutoscalingGroupName, currentCapacity) ()
const increaseASGroupCapacity = CB.create(params => ({
    AutoScalingGroupName: params.ASGroupName,
    DesiredCapacity: params.currentCapacity + 1,
    HonorCooldown: true
}))
    .compose(CB((asgRequest, cb) => {
        if(dryRun) {
            console.log('Dry run - will not increase capacity');
            return cb(null, null);
        }
        console.log('Increasing desired capacity');
        as.setDesiredCapacity(asgRequest, cb);
    }));

//removeInstance :: CB instanceId ()
const removeInstance = CB.create(instanceId => ({
    InstanceId: instanceId, ShouldDecrementDesiredCapacity: true
}))
    .compose(CB((terminateInstanceRequest, cb) => {
        if(dryRun) {
            console.log('Dry run - will not remove instance: ' + terminateInstanceRequest.InstanceId);
            return cb(null, null);
        }
        console.log('Removing instance: ' + terminateInstanceRequest.InstanceId);
        as.terminateInstanceInAutoScalingGroup(terminateInstanceRequest, cb);
    }));

const gc = () => {
    if(global.gc) global.gc();
};

const run = () => {
    if(loopsRemaining <= 0) {
        process.exit(0);
        return;
    };
    loopsRemaining--;

    gatherInfo({
        clusterName: clusterName,
        ASGroupName: ASGroupName
    }, (err, data) => {
        const scaleDownInstance = scaleDown(data);
        const scaleUpInstance = scaleUp(data);

        if(data.containerInstances.length !== data.desiredCapacity) {
            console.log('Still scaling up instances');
        } else if(scaleUpInstance) {
            return increaseASGroupCapacity({currentCapacity: data.desiredCapacity, ASGroupName: ASGroupName}, err => {
                if(err) return console.error(err);
                console.log('Capacity increased');
                gc();
            });
        } else if(data.awaitingTasks.length !== 0) {
            console.log('Still scaling up tasks');
        } else if(scaleDownInstance) {
            return removeInstance(scaleDownInstance, err => {
                if(err) return console.error(err);<commit_msg>Bad semicolon - picked up by automated process<commit_after>'use strict';
/* eslint-disable no-console */

const CB = require('camda').CB;
const AWS = require('aws-sdk');

const gatherInfo = require('./lib/gatherInformation');
const scaleUp = require('./lib/scaleUp');
const scaleDown = require('./lib/scaleDown');

if(!process.env.AWS_REGION) {
    console.error('AWS_REGION not set');
    process.exit(1);
}
const as = new AWS.AutoScaling();

let ASGroupName = process.env.AS_GROUP_NAME;
let clusterName = process.env.CLUSTER_NAME;
let dryRun = process.env.DRY_RUN;
let interval = process.env.INTERVAL || 20000;
let loopsRemaining = process.env.MAX_LOOPS || 20;

if(!ASGroupName || !clusterName) {
    console.error('AS_GROUP_NAME or CLUSTER_NAME missing');
    process.exit(1);
}

//increaseASGroupCapacity :: CB (AutoscalingGroupName, currentCapacity) ()
const increaseASGroupCapacity = CB.create(params => ({
    AutoScalingGroupName: params.ASGroupName,
    DesiredCapacity: params.currentCapacity + 1,
    HonorCooldown: true
}))
    .compose(CB((asgRequest, cb) => {
        if(dryRun) {
            console.log('Dry run - will not increase capacity');
            return cb(null, null);
        }
        console.log('Increasing desired capacity');
        as.setDesiredCapacity(asgRequest, cb);
    }));

//removeInstance :: CB instanceId ()
const removeInstance = CB.create(instanceId => ({
    InstanceId: instanceId, ShouldDecrementDesiredCapacity: true
}))
    .compose(CB((terminateInstanceRequest, cb) => {
        if(dryRun) {
            console.log('Dry run - will not remove instance: ' + terminateInstanceRequest.InstanceId);
            return cb(null, null);
        }
        console.log('Removing instance: ' + terminateInstanceRequest.InstanceId);
        as.terminateInstanceInAutoScalingGroup(terminateInstanceRequest, cb);
    }));

const gc = () => {
    if(global.gc) global.gc();
};

const run = () => {
    if(loopsRemaining <= 0) {
        process.exit(0);
        return;
    }
    loopsRemaining--;

    gatherInfo({
        clusterName: clusterName,
        ASGroupName: ASGroupName
    }, (err, data) => {
        const scaleDownInstance = scaleDown(data);
        const scaleUpInstance = scaleUp(data);

        if(data.containerInstances.length !== data.desiredCapacity) {
            console.log('Still scaling up instances');
        } else if(scaleUpInstance) {
            return increaseASGroupCapacity({currentCapacity: data.desiredCapacity, ASGroupName: ASGroupName}, err => {
                if(err) return console.error(err);
                console.log('Capacity increased');
                gc();
            });
        } else if(data.awaitingTasks.length !== 0) {
            console.log('Still scaling up tasks');
        } else if(scaleDownInstance) {
            return removeInstance(scaleDownInstance, err => {
                if(err) return console.error(err);
{'fraction_non_alphanumeric': 0.13158801240096452, 'fraction_numerical': 0.0044781260764726145, 'mean_word_length': 3.0298403886190144, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '7067393', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1663, 'n_words': 345}