<commit_before>'use strict';

var Q = require('q')
var C = require('spacebox-common')
var moment = require('moment')
var uuidGen = require('node-uuid')

var config = require('../config')
var db = config.db
var pubsub = require('../pubsub')
var inventory = require('../inventory')
var helpers = require('./helpers')

module.exports = {

    checkAndDeliverResources: function(ctx, uuid, db) {
        return db.tx(function(db) {
            return db.one("select * from facilities where id = $1 for update", uuid).
            then(function(facility) {
                return db.blueprints.get(facility.blueprint).
                then(function(blueprint) {
                    return [blueprint, facility]
                })
            }).spread(function(blueprint, facility) {
                ctx.old_debug('build', 'resource processing', facility, blueprint)

                if (facility.doc.resources_checked_at === undefined) {
                    facility.doc.resources_checked_at = moment()
                        // The first time around this is just a dummy
                    return db.query("update facilities set trigger_at = $2, doc = $3 where id = $1", [uuid, moment().add(blueprint.generating_period, 'm').toDate(), facility.doc])
                } else if (
                    moment(facility.doc.resources_checked_at).add(blueprint.generating_period, 'm').isBefore(moment())
                ) {
                    return helpers.produce(facility.inventory_id, 'default', [{
                        blueprint: blueprint.generated_resource,
                        quantity: blueprint.generating_quantity
                    }], db).
                    then(function() {
                        pubsub.publish(ctx, {
                            type: 'resources',
                            account: facility.account,
                            facility: uuid,
                            blueprint: blueprint.generated_resource,
                            quantity: blueprint.generating_quantity,
                            state: 'delivered'
                        })

                        facility.doc.resources_checked_at = moment()
                        return db.query("update facilities set trigger_at = $2, next_backoff = '1 second', doc = $3 where id = $1", [uuid, moment().add(blueprint.generating_period, 'm').toDate(), facility.doc])
                    }).fail(function(e) {
                        pubsub.publish(ctx, {
                            type: 'resources',
                            account: facility.account,
                            facility: uuid,
                            blueprint: blueprint.generated_resource,
                            quantity: blueprint.generating_quantity,
                            state: 'delivery_failed'
                        })

                        throw e
                    })
                } else {<commit_msg>change resource delivery errors<commit_after>'use strict';

var Q = require('q')
var C = require('spacebox-common')
var moment = require('moment')
var uuidGen = require('node-uuid')

var config = require('../config')
var db = config.db
var pubsub = require('../pubsub')
var inventory = require('../inventory')
var helpers = require('./helpers')

module.exports = {

    checkAndDeliverResources: function(ctx, uuid, db) {
        return db.tx(function(db) {
            return db.one("select * from facilities where id = $1 for update", uuid).
            then(function(facility) {
                return db.blueprints.get(facility.blueprint).
                then(function(blueprint) {
                    return [blueprint, facility]
                })
            }).spread(function(blueprint, facility) {
                ctx.old_debug('build', 'resource processing', facility, blueprint)

                if (facility.doc.resources_checked_at === undefined) {
                    facility.doc.resources_checked_at = moment()
                        // The first time around this is just a dummy
                    return db.query("update facilities set trigger_at = $2, doc = $3 where id = $1", [uuid, moment().add(blueprint.generating_period, 'm').toDate(), facility.doc])
                } else if (
                    moment(facility.doc.resources_checked_at).add(blueprint.generating_period, 'm').isBefore(moment())
                ) {
                    return helpers.produce(facility.inventory_id, 'default', [{
                        blueprint: blueprint.generated_resource,
                        quantity: blueprint.generating_quantity
                    }], db).
                    then(function() {
                        pubsub.publish(ctx, {
                            type: 'resources',
                            account: facility.account,
                            facility: uuid,
                            blueprint: blueprint.generated_resource,
                            quantity: blueprint.generating_quantity,
                            state: 'delivered'
                        })

                        facility.doc.resources_checked_at = moment()
                        return db.query("update facilities set trigger_at = $2, next_backoff = '1 second', doc = $3 where id = $1", [uuid, moment().add(blueprint.generating_period, 'm').toDate(), facility.doc])
                    }).fail(function(e) {
                        pubsub.publish(ctx, {
                            type: 'resources',
                            account: facility.account,
                            facility: uuid,
                            blueprint: blueprint.generated_resource,
                            quantity: blueprint.generating_quantity,
                            error: e,
                        })

                        throw e
                    })
                } else {
{'fraction_non_alphanumeric': 0.10521813515825491, 'fraction_numerical': 0.002737382378100941, 'mean_word_length': 1.437864887406172, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2461987', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1365, 'n_words': 307}