All files / src/projects/cyberia/procedural/jobs job.js

0% Statements 0/46
0% Branches 0/30
0% Functions 0/8
0% Lines 0/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                                                                                                                                                                                                                                                     
/**
 * Job creation in the Procedural Engine. A job holds the spec of {@link module:src/projects/cyberia/procedural/spec.js}
 * and one item for each candidate the spec lists. The items are stored whole, so a request that
 * failed halfway completes when it is sent again.
 *
 * @module src/projects/cyberia/procedural/jobs/job.js
 * @namespace CyberiaProceduralJob
 */
import { createHash } from 'node:crypto';
import canonicalize from 'canonicalize';
import { itemSeed } from '../core/seed.js';
import { recordAudit, requestError, syncJob } from '../engine.js';
import { resolveSpec } from '../spec.js';
 
/** Where a source came from: the procedural revision it is, if it is one. */
async function lineageOf({ CyberiaProceduralJobItem }, sourceCid, generationRunId) {
  const parent = sourceCid
    ? await CyberiaProceduralJobItem.findOne({ 'revisions.cid': sourceCid }, { targetItemId: 1, revisions: 1 }).lean()
    : null;
  return {
    parentCid: sourceCid,
    parentItemId: parent?.targetItemId ?? '',
    parentRevision: parent?.revisions.find(({ cid }) => cid === sourceCid).revision ?? null,
    generationRunId,
  };
}
 
/** Refuses a proposed item id some definition or live candidate already holds. */
async function assertIdsFree({ CyberiaItemCatalog, CyberiaProceduralJobItem }, itemIds) {
  const bound = await CyberiaItemCatalog.findOne({ itemId: { $in: itemIds } }, { itemId: 1 }).lean();
  if (bound) throw requestError(`Item ${bound.itemId} already runs a definition`, 409);
  const live = await CyberiaProceduralJobItem.findOne(
    { targetItemId: { $in: itemIds }, status: { $nin: ['rejected', 'cancelled', 'failed'] } },
    { targetItemId: 1 },
  ).lean();
  if (live) throw requestError(`Item ${live.targetItemId} belongs to another generation`, 409);
}
 
/** Stores the items of a job that are not stored yet; safe to run again. */
async function ensureItems(models, job) {
  const { CyberiaProceduralJobItem } = models;
  const { spec } = job;
  const stored = new Set(
    (await CyberiaProceduralJobItem.find({ jobId: job._id }, { ordinal: 1 }).lean()).map(({ ordinal }) => ordinal),
  );
  const lineages = new Map();
  const missing = [];
  for (const { ordinal, targetItemId, sourceCid, templateId, paletteId } of spec.candidates) {
    if (stored.has(ordinal)) continue;
    if (!lineages.has(sourceCid)) lineages.set(sourceCid, await lineageOf(models, sourceCid, job.generationRunId));
    missing.push({
      jobId: job._id,
      ordinal,
      targetItemId,
      sourceCid,
      paletteId: paletteId ?? '',
      templateId,
      lineage: lineages.get(sourceCid),
      seed: itemSeed({
        rootSeed: job.rootSeed,
        sourceCid,
        itemId: targetItemId,
        ordinal,
        generationRunId: job.generationRunId,
        generatorId: spec.generator.id,
        profileId: spec.profile.profileId,
      }),
    });
  }
  if (missing.length === 0) return;
  try {
    await CyberiaProceduralJobItem.insertMany(missing, { ordered: false });
  } catch (error) {
    const failures = error.writeErrors ?? [error];
    if (!failures.every((failure) => (failure.code ?? failure.err?.code) === 11000)) throw error;
  }
}
 
/**
 * Creates the job of a request, or returns the one an equal spec created.
 * @param {Object} params
 * @param {Object} params.models
 * @param {Object} params.request - See {@link module:src/projects/cyberia/procedural/spec.js}.
 * @param {string} params.actor - User id of the moderator.
 * @returns {Promise<Object>} The job.
 * @throws {Error} 400 for an invalid request; 409 when a proposed item id is taken.
 * @memberof CyberiaProceduralJob
 */
export async function createJob({ models, request, actor }) {
  const { spec } = await resolveSpec(models, request);
 
  const { CyberiaProceduralJob } = models;
  const requestFingerprint = createHash('sha256').update(canonicalize(spec)).digest('hex');
  let job = await CyberiaProceduralJob.findOne({ requestFingerprint }).lean();
  if (!job) {
    if (spec.mode === 'create-new')
      await assertIdsFree(
        models,
        spec.candidates.map(({ targetItemId }) => targetItemId),
      );
    try {
      job = (
        await CyberiaProceduralJob.create({
          generatorId: spec.generator.id,
          proceduralProfileId: spec.profile.profileId,
          generationRunId: `run-${requestFingerprint.slice(0, 16)}`,
          requestFingerprint,
          rootSeed: spec.seed.value,
          spec,
          createdBy: actor,
        })
      ).toObject();
      await recordAudit(models, { action: 'job.created', actor, jobId: job._id });
    } catch (error) {
      if (error?.code !== 11000) throw error;
      job = await CyberiaProceduralJob.findOne({ requestFingerprint }).lean();
    }
  }
  await ensureItems(models, job);
  await syncJob({ models, jobId: job._id });
  return await CyberiaProceduralJob.findById(job._id).lean();
}