"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runAgent = void 0;
const logger_1 = require("@refloat/logger");
const api_queue_1 = require("../api/api-queue");
const graph_1 = require("../run/graph");
const scheduler_1 = require("../run/scheduler");
const sleep_1 = require("../utils/sleep");
const logger = logger_1.Logger.create('build-agent');
async function runAgent(ctx, api, queue = new api_queue_1.APIQueue(api)) {
    const scheduler = new scheduler_1.Scheduler(queue);
    logger.info('waiting for jobs');
    await queue.wait(async () => {
        try {
            while (ctx.running && queue.running) {
                const job = await api.acquireJob();
                if (job) {
                    try {
                        logger.info('running', { job: job.path });
                        await scheduler.run(graph_1.Graph.from(job));
                        logger.info('completed', { job: job.path });
                    }
                    catch (error) {
                        logger.error('failed', { job: job.path, error });
                    }
                }
                else {
                    await ctx.wait((0, sleep_1.sleep)(1000));
                }
            }
        }
        finally {
            queue.running = false;
        }
    });
}
exports.runAgent = runAgent;
//# sourceMappingURL=build-agent.js.map