@orion-js/dogs 4.6.5 → 4.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -222,6 +222,7 @@ __runInitializers(_init, 1, JobsHistoryRepo);
222
222
  var import_helpers = require("@orion-js/helpers");
223
223
  var import_logger = require("@orion-js/logger");
224
224
  var import_mongodb2 = require("@orion-js/mongodb");
225
+ var import_schema3 = require("@orion-js/schema");
225
226
 
226
227
  // src/services/partitions.ts
227
228
  function validatePartitionsCount(nPartitions) {
@@ -661,13 +662,13 @@ var JobsRepo = class {
661
662
  if (jobs.length === 0) {
662
663
  return { scheduledCount: 0, skippedCount: 0, errors: [] };
663
664
  }
664
- let scheduledCount = 0;
665
- let skippedCount = 0;
666
665
  const errors = [];
666
+ const preparedJobs = [];
667
667
  for (let i = 0; i < jobs.length; i++) {
668
668
  const job = jobs[i];
669
669
  try {
670
- await this.jobs.insertOne({
670
+ const record = await (0, import_schema3.clean)(JobRecordSchema, {
671
+ _id: this.jobs.generateId(),
671
672
  jobName: job.name,
672
673
  uniqueIdentifier: job.uniqueIdentifier,
673
674
  params: job.params,
@@ -676,16 +677,60 @@ var JobsRepo = class {
676
677
  type: "event",
677
678
  ...job.partition === void 0 ? {} : { partition: job.partition }
678
679
  });
679
- scheduledCount++;
680
+ await (0, import_schema3.validate)(JobRecordSchema, record);
681
+ preparedJobs.push({ index: i, job, record });
680
682
  } catch (error) {
681
- if (error.isValidationError && Object.values(error.validationErrors).includes("notUnique") && job.uniqueIdentifier) {
682
- import_logger.logger.info(`Job "${job.name}" with identifier "${job.uniqueIdentifier}" already exists`);
683
- skippedCount++;
684
- } else {
683
+ errors.push({
684
+ index: i,
685
+ error: error instanceof Error ? error : new Error(String(error)),
686
+ job
687
+ });
688
+ }
689
+ }
690
+ if (preparedJobs.length === 0) {
691
+ return { scheduledCount: 0, skippedCount: 0, errors };
692
+ }
693
+ const rawCollection = await this.jobs.getRawCollection();
694
+ await this.jobs.createIndexesPromise;
695
+ const operations = preparedJobs.map(
696
+ ({ record }) => record.uniqueIdentifier === void 0 ? { insertOne: { document: record } } : {
697
+ updateOne: {
698
+ filter: { uniqueIdentifier: record.uniqueIdentifier },
699
+ update: { $setOnInsert: record },
700
+ upsert: true
701
+ }
702
+ }
703
+ );
704
+ let scheduledCount = 0;
705
+ let skippedCount = 0;
706
+ try {
707
+ const result = await rawCollection.bulkWrite(operations, { ordered: false });
708
+ scheduledCount = result.insertedCount + result.upsertedCount;
709
+ skippedCount = result.matchedCount;
710
+ } catch (error) {
711
+ const writeErrors = error instanceof import_mongodb2.MongoDB.MongoBulkWriteError ? Array.isArray(error.writeErrors) ? error.writeErrors : [error.writeErrors] : [];
712
+ if (!(error instanceof import_mongodb2.MongoDB.MongoBulkWriteError) || writeErrors.length === 0) {
713
+ for (const preparedJob of preparedJobs) {
685
714
  errors.push({
686
- index: i,
715
+ index: preparedJob.index,
687
716
  error: error instanceof Error ? error : new Error(String(error)),
688
- job
717
+ job: preparedJob.job
718
+ });
719
+ }
720
+ } else {
721
+ scheduledCount = error.result.insertedCount + error.result.upsertedCount;
722
+ skippedCount = error.result.matchedCount;
723
+ for (const writeError of writeErrors) {
724
+ const preparedJob = preparedJobs[writeError.index];
725
+ if (!preparedJob) continue;
726
+ if (writeError.code === 11e3 && preparedJob.record.uniqueIdentifier !== void 0) {
727
+ skippedCount++;
728
+ continue;
729
+ }
730
+ errors.push({
731
+ index: preparedJob.index,
732
+ error: new Error(writeError.errmsg),
733
+ job: preparedJob.job
689
734
  });
690
735
  }
691
736
  }
@@ -1597,7 +1642,7 @@ function setNameToJobs(jobs, nPartitions) {
1597
1642
  }
1598
1643
 
1599
1644
  // src/defineJob/index.ts
1600
- var import_schema3 = require("@orion-js/schema");
1645
+ var import_schema4 = require("@orion-js/schema");
1601
1646
  var import_cron_parser2 = require("cron-parser");
1602
1647
  var import_parse_duration = __toESM(require("parse-duration"), 1);
1603
1648
  function createEventJob(options) {
@@ -1611,7 +1656,7 @@ function createEventJob(options) {
1611
1656
  if (!jobDefinition.jobName) {
1612
1657
  throw new Error("This job has not been registered in the workers");
1613
1658
  }
1614
- const params = jobDefinition.params ? await (0, import_schema3.cleanAndValidate)(jobDefinition.params, scheduleOptions.params) : scheduleOptions.params;
1659
+ const params = jobDefinition.params ? await (0, import_schema4.cleanAndValidate)(jobDefinition.params, scheduleOptions.params) : scheduleOptions.params;
1615
1660
  return await scheduleJobInternal(
1616
1661
  {
1617
1662
  ...scheduleOptions,
@@ -1627,7 +1672,7 @@ function createEventJob(options) {
1627
1672
  }
1628
1673
  const processedJobs = await Promise.all(
1629
1674
  jobs.map(async (scheduleOptions) => {
1630
- const params = jobDefinition.params ? await (0, import_schema3.cleanAndValidate)(jobDefinition.params, scheduleOptions.params) : scheduleOptions.params;
1675
+ const params = jobDefinition.params ? await (0, import_schema4.cleanAndValidate)(jobDefinition.params, scheduleOptions.params) : scheduleOptions.params;
1631
1676
  return {
1632
1677
  ...scheduleOptions,
1633
1678
  name: jobDefinition.jobName,