<commit_before>'use strict'
const _ = require('lodash')

function Job(row, queue) {
    _.extend(this, row)
    this.queue = queue
    this.db = queue.db
    this.handled = false
}

Job.prototype.finish = function() {
    this.handled = true
    this.state = 'finished'
    return this.db.none('UPDATE "JobQueue" SET "state"=${state}, "lastRun"=NOW() WHERE "id"=${id}', {
        state: this.state,
        id: this.id
    })
    .then(() => this.queue.logEvent('finished', this))
}

Job.prototype.destroy = function() {
    this.handled = true
    this.state = 'destroyed'
    return this.db.none('DELETE FROM "JobQueue" WHERE "id"=${id}', {
        id: this.id
    })
    .then(() => this.queue.logEvent('destroyed', this))
}

Job.prototype.reschedule = function(date) {
    this.handled = true
    this.state = 'waiting'
    this.scheduledFor = date
    return this.db.none('UPDATE "JobQueue" SET "state"=${state}, "scheduledFor"=${scheduledFor} WHERE "id"=${id}', {
        state: this.state,
        id: this.id,
        scheduledFor: this.scheduledFor
    })
    .then(() => this.queue.logEvent('rescheduled', this))
    .then(() => this.queue.logEvent('finished', this))
}

Job.prototype.fail = function(error, rescheduleFor) {
    this.handled = true
    return this.db.none('UPDATE "JobQueue" SET "lastFailureMessage"=${failureMessage}, "failedAttempts"="failedAttempts"+1, "lastRun"=NOW() WHERE "id"=${id}', {
        id: this.id,
        failureMessage: error.message
    }).then(() => {
        return this.db.one('SELECT * FROM "JobQueue" WHERE "id"=${id}', {id: this.id}).then((result) => {
            if (result.failedAttempts >= result.maxAttempts) {
                // complete failure, ensure the job isn't run again
                this.state = 'failed'
                return this.db.none('UPDATE "JobQueue" SET "state"=${state} WHERE "id"=${id}', {
                    id: this.id,
                    state: this.state
                })
            }
            else {
                // reschedule the job to run in 3 minute
                var now = new Date()
                var minute = 3 * 60 * 1000
                this.scheduledFor = rescheduleFor || new Date(now.getTime() + minute)
                this.state = 'waiting'
                return this.db.none('UPDATE "JobQueue" SET "state"=${state}, "scheduledFor"=${scheduledFor} WHERE "id"=${id}', {
                    id: this.id,
                    state: this.state,
                    scheduledFor: this.scheduledFor
                })
            }
        })
    })
    .then(() => this.queue.logEvent('failed', this))
    .then(() => this.queue.logError(error, this))
}

module.exports = Job<commit_msg>Update job.js<commit_after>'use strict'
const _ = require('lodash')

function Job(row, queue) {
    _.extend(this, row)
    this.queue = queue
    this.db = queue.db
    this.handled = false
}

Job.prototype.finish = function() {
    this.handled = true
    this.state = 'finished'
    return this.db.none('UPDATE "JobQueue" SET "state"=${state}, "lastRun"=NOW() WHERE "id"=${id}', {
        state: this.state,
        id: this.id
    })
    .then(() => this.queue.logEvent('finished', this))
}

Job.prototype.destroy = function() {
    this.handled = true
    this.state = 'destroyed'
    return this.db.none('DELETE FROM "JobQueue" WHERE "id"=${id}', {
        id: this.id
    })
    .then(() => this.queue.logEvent('destroyed', this))
}

Job.prototype.reschedule = function(date) {
    this.handled = true
    this.state = 'waiting'
    this.scheduledFor = date
    return this.db.none('UPDATE "JobQueue" SET "state"=${state}, "scheduledFor"=${scheduledFor} WHERE "id"=${id}', {
        state: this.state,
        id: this.id,
        scheduledFor: this.scheduledFor
    })
    .then(() => this.queue.logEvent('rescheduled', this))
    .then(() => this.queue.logEvent('finished', this))
}

Job.prototype.fail = function (error, rescheduleFor) {
    var self = this;
    self.handled = true;
    return self.db.task(function*(t) {
        yield t.none('UPDATE "JobQueue" SET "lastFailureMessage"=${failureMessage}, "failedAttempts"="failedAttempts"+1, "lastRun"=NOW() WHERE "id"=${id}', {
            id: self.id,
            failureMessage: error.message
        });
        let JobQueue = yield t.one('SELECT * FROM "JobQueue" WHERE "id"=${id}', {id: self.id});
        if (JobQueue.failedAttempts >= JobQueue.maxAttempts) {
            // complete failure, ensure the job isn't run again
            self.state = 'failed';
            return yield t.none('UPDATE "JobQueue" SET "state"=${state} WHERE "id"=${id}', {
                id: self.id,
                state: self.state
            });
        }
        // reschedule the job to run in 3 minute
        var now = new Date();
        var minute = 3 * 60 * 1000;
        self.scheduledFor = rescheduleFor || new Date(now.getTime() + minute);
        self.state = 'waiting';
        return yield t.none('UPDATE "JobQueue" SET "state"=${state}, "scheduledFor"=${scheduledFor} WHERE "id"=${id}', {
            id: self.id,
            state: self.state,
            scheduledFor: self.scheduledFor
        });
    })
        .then(() => self.queue.logEvent('failed', self))
        .then(() => self.queue.logError(error, self));
};

module.exports = Job

{'fraction_non_alphanumeric': 0.16475240067783845, 'fraction_numerical': 0.0033891922425155338, 'mean_word_length': 2.359898798228969, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '282328', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1645, 'n_words': 366}