Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Test multiple parsing outputs instead of hardcoding each of them into a constant Node js

So in my application I am cron parsing multiple database data and I have it right now to where I create a const run for each data to find the dag_id and test the determineCron one at a time manually. How would I make an array so that the "determineCron()" at the bottom will test every run instead of me just manually coding determineCron(run), determineCron(run2), determineCron(run3), determineCron(run4) etc.

const run = {
    start_date: '2022-07-01T13:45:01.221636+00:00',
        end_date: '2022-07-01T14:11:01.864293+00:00',
    state: 'success',
    dag_run_id: 'scheduled__2022-06-30T13:45:00+00:00',
    dag_id: 'IPP_CYCLE_PARMS',
}

const run2 = {
    start_date: '2022-06-26T23:00:00.742495+00:00',
    end_date: '2022-06-27T14:10:23.108401+00:00',
    state: 'failed',
    dag_run_id: 'scheduled__2022-06-25T23:00:00+00:00',
    dag_id: 'EFS-Winning-Route-daily-batch'
}

async function determineCron(result){
    /*dagID = result?.[0]?.dag_id || 0*/
    dagID = result ? result.dag_id : 0
    console.log(dagID)
    job = await bqConnection().query(`SELECT * FROM \`np-inventory-planning-thd.IPP_SLA.expected_sla\` where dag_id = "${dagID}"`)
    console.log(job[0][0]);
    cronTime = job[0][0].cron_time
    var interval = parser.parseExpression(cronTime);
    console.log('Date: ', interval.next().toString());
}
determineCron(run)
determineCron(run2)

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const runs = [
{
    start_date: '2022-07-01T13:45:01.221636+00:00',
        end_date: '2022-07-01T14:11:01.864293+00:00',
    state: 'success',
    dag_run_id: 'scheduled__2022-06-30T13:45:00+00:00',
    dag_id: 'IPP_CYCLE_PARMS',
},
{
    start_date: '2022-06-26T23:00:00.742495+00:00',
    end_date: '2022-06-27T14:10:23.108401+00:00',
    state: 'failed',
    dag_run_id: 'scheduled__2022-06-25T23:00:00+00:00',
    dag_id: 'EFS-Winning-Route-daily-batch'
}
]
runs.forEach(determineCron)
about 4 years ago · Juan Pablo Isaza Report

0

Put your runs into an array and then use array.forEach, like this:

interface CronRun {
  start_date: string;
  end_date: string;
  state: string;
  dag_run_id: string;
  dag_id: string;
}

const runs: CronRun[] = [
  {
    start_date: '2022-07-01T13:45:01.221636+00:00',
    end_date: '2022-07-01T14:11:01.864293+00:00',
    state: 'success',
    dag_run_id: 'scheduled__2022-06-30T13:45:00+00:00',
    dag_id: 'IPP_CYCLE_PARMS',
  },
  {
    start_date: '2022-06-26T23:00:00.742495+00:00',
    end_date: '2022-06-27T14:10:23.108401+00:00',
    state: 'failed',
    dag_run_id: 'scheduled__2022-06-25T23:00:00+00:00',
    dag_id: 'EFS-Winning-Route-daily-batch'
  }
];

async function determineCron(result: ChronRun){
    /*dagID = result?.[0]?.dag_id || 0*/
    dagID = result ? result.dag_id : 0
    console.log(dagID)
    job = await bqConnection().query(`SELECT * FROM \`np-inventory-planning-thd.IPP_SLA.expected_sla\` where dag_id = "${dagID}"`)
    console.log(job[0][0]);
    cronTime = job[0][0].cron_time
    var interval = parser.parseExpression(cronTime);
    console.log('Date: ', interval.next().toString());
}

runs.forEach(determineCron);

like your original code, this will run them all in parallel. If you want them in serial you'll have to do

for await (const run of runs) {
  determineCron(run);
}

instead.

about 4 years ago · Juan Pablo Isaza Report

0

You could create an array with the references of the constants, but it's tricky after that, because if you use any built-in array method like map, forEach, reduce... to execute the function it will not await the previous finish to execute the next call. Therefore, what you should do is to use for and their respective variations..

// if you are in a async scope you can remove that self invoked function

(async function () {
  const runs = [
    {
      start_date: "2022-07-01T13:45:01.221636+00:00",
      end_date: "2022-07-01T14:11:01.864293+00:00",
      state: "success",
      dag_run_id: "scheduled__2022-06-30T13:45:00+00:00",
      dag_id: "IPP_CYCLE_PARMS",
    },
    {
      start_date: "2022-06-26T23:00:00.742495+00:00",
      end_date: "2022-06-27T14:10:23.108401+00:00",
      state: "failed",
      dag_run_id: "scheduled__2022-06-25T23:00:00+00:00",
      dag_id: "EFS-Winning-Route-daily-batch",
    },
  ];

  async function determineCron(result) {
    /*dagID = result?.[0]?.dag_id || 0*/
    dagID = result ? result.dag_id : 0;
    console.log(dagID);
    job = await bqConnection().query(
      `SELECT * FROM \`np-inventory-planning-thd.IPP_SLA.expected_sla\` where dag_id = "${dagID}"`
    );
    console.log(job[0][0]);
    cronTime = job[0][0].cron_time;
    var interval = parser.parseExpression(cronTime);
    console.log("Date: ", interval.next().toString());
  }

  for (let run of runs) {
    await determineCron(run);
  }
})();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!