<commit_before>var _ = require("underscore");
var moment = require("moment");
var q = require("q");
var logger = require("../Application/Logger");
var util = require("util");

var Lock = require("../Application/ExecuteLocked");
var lock = new Lock();

var partitions = [];
var cleanIdlePartitionsAfterMinutes;

function Partition(partitionId, worker) {
    var self = this;
    this.partitionId = partitionId;
    this.worker = worker;
    this.updatedAt = moment().utc().format();
    
    setInterval(function() {
        logger.info(util.format("Partition cleanup fired for %d", partitionId));
        if(self.updatedAt < moment().utc().subtract(cleanIdlePartitionsAfterMinutes, 'minutes').format())
            lock.execWrite(function(){ 
                return q.Promise(function(resolve){
                  partitions.splice(_.findIndex(partitions, self), 1);
                  logger.info(util.format("Partition %d cleaned", partitionId));
                  resolve();
                });
            });
        logger.info(util.format("Partition cleanup completed for %d", partitionId));
    }, cleanIdlePartitionsAfterMinutes * 60 * 1000);
}

function PartitionService(cleanIdlePartitions){
    if(cleanIdlePartitions === null || cleanIdlePartitions === undefined || isNaN(parseInt(cleanIdlePartitions)) || cleanIdlePartitions <= 0)
        throw new Error("cleanIdlePartitionsAfterMinutes required integer greater than 0");
    
    cleanIdlePartitionsAfterMinutes = cleanIdlePartitions;
}

PartitionService.prototype.get = function(partitionId) {
    return lock.execRead(function(){
       return q.Promise(function(resolve, reject){
            try {<commit_msg>improvement<commit_after>var _ = require("underscore");
var moment = require("moment");
var q = require("q");
var logger = require("../Application/Logger");
var util = require("util");

var Lock = require("../Application/ExecuteLocked");
var lock = new Lock();

var partitions = [];
var cleanIdlePartitionsAfterMinutes;

function Partition(partitionId, worker) {
    var self = this;
    this.partitionId = partitionId;
    this.worker = worker;
    this.updatedAt = moment().utc().format();
    
    setInterval(function() {
        logger.info(util.format("Partition cleanup fired for %d", partitionId));
        if(self.updatedAt <= moment().utc().subtract(cleanIdlePartitionsAfterMinutes, 'minutes').format())
            lock.execWrite(function(){ 
                return q.Promise(function(resolve){
                  partitions.splice(_.findIndex(partitions, self), 1);
                  logger.info(util.format("Partition %d cleaned", partitionId));
                  resolve();
                });
            });
        logger.info(util.format("Partition cleanup completed for %d", partitionId));
    }, cleanIdlePartitionsAfterMinutes * 60 * 1000);
}

function PartitionService(cleanIdlePartitions){
    if(cleanIdlePartitions === null || cleanIdlePartitions === undefined || isNaN(parseInt(cleanIdlePartitions)) || cleanIdlePartitions <= 0)
        throw new Error("cleanIdlePartitionsAfterMinutes required integer greater than 0");
    
    cleanIdlePartitionsAfterMinutes = cleanIdlePartitions;
}

PartitionService.prototype.get = function(partitionId) {
    return lock.execRead(function(){
       return q.Promise(function(resolve, reject){
            try {
{'fraction_non_alphanumeric': 0.13040883318412413, 'fraction_numerical': 0.005371530886302597, 'mean_word_length': 3.5114401076716018, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '1754142', 'n_tokens_mistral': 921, 'n_tokens_neox': 887, 'n_words': 188}