Skip to content

Instantly share code, notes, and snippets.

@siteslave
Created January 13, 2023 07:38
Show Gist options
  • Save siteslave/c1d74878feb190ba40bc53ed37297dd6 to your computer and use it in GitHub Desktop.
Save siteslave/c1d74878feb190ba40bc53ed37297dd6 to your computer and use it in GitHub Desktop.
const { Queue } = require('bullmq')
// Add your own configuration here
const redisConfiguration = {
connection: {
host: "localhost",
port: 6379,
enableOfflineQueue: false,
// username: "default",
// password: "redispw"
}
}
const myQueue = new Queue('R7Platform', {
connection: redisConfiguration.connection,
defaultJobOptions: {
delay: 1000,
attempts: 1,
backoff: {
type: 'exponential',
delay: 3000,
},
removeOnComplete: {
age: 3600, // keep up to 1 hour
count: 1000, // keep up to 1000 jobs
},
removeOnFail: {
age: 24 * 3600, // keep up to 24 hours
},
},
});
async function personQueue(hn, cid, fname, lname) {
await myQueue.add('person',
{ hn, cid, fname, lname }
);
}
async function opdQueue(hn, vn) {
await myQueue.add('opd',
{ hn, vn }
);
}
for (let index = 0; index < 20; index++) {
personQueue("0041228", "3440000000232", "สถิตย์", "เรียนพิศ");
opdQueue("0041228", "66011025665005");
}
@siteslave
Copy link
Author

Consumer

const { Worker } = require('bullmq')

const redisConfiguration = {
  connection: {
    host: "localhost",
    port: 6379,
    enableOfflineQueue: false,
    // username: "default",
    // password: "redispw"
  }
}

async function importPerson(job) {
  const { fname, lname } = job.data;
  console.log(`Patient name: ${fname} ${lname}.`)
  return job.id;
}

async function importOpd(job) {
  const { vn, hn } = job.data;
  console.log(`vn: ${vn}, hn: ${hn}.`)
  return job.id;
}

const worker = new Worker('R7Queue', async job => {
  switch (job.name) {
    case 'PERSON': {
      await importPerson(job);
      break;
    }
    case 'opd': {
      await importOpd(job);
      break;
    }
  }
}, {
  limiter: {
    max: 10,
    duration: 1000,
  },
  concurrency: 4, connection: redisConfiguration.connection,
  defaultJobOptions: {
    attempts: 3,
    backoff: {
      type: 'exponential',
      delay: 3000,
    },
  }
});

worker.on('completed', job => {
  console.info(`${job.id} has completed!`);
});

worker.on('failed', (job, err) => {
  console.error(`${job.id} has failed with ${err.message}`);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment