2.0.6 • Published 6 months ago

job-allocation v2.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

job-allocation

NPM Downloads

Library for distributing jobs between nodes

Install

yarn add job-allocation
# or
npm i job-allocation

Example

import {
    JAWorker,
    JARedisRemoteQueue,
    JAJob,
    createWaitJobsCompleted,
} from 'job-allocation';

const action = async (job: JAJob<{name: string}>) => `Hello world, ${job.data.name}`;
const remoteQueue = new JARedisRemoteQueue<{name: string}>({
    name: 'redis-key-queue',
    host: process.env.REDIS_HOST,
    port: process.env.REDIS_PORT,
});
const worker = new JAWorker({
    remoteQueue,
    action,
    concurrency: 2,
});
const { waitJobsCompleted } = createWaitJobsCompleted(worker);
const startTask = async () => {
    /**
     * Adding jobs to work
     */
    const jobs = await remoteQueue.add(
        { name: 'foo' },
        { name: 'bar' },
    );
    /**
     * Wait for the jobs to be completed
     * ! does not guarantee order
     */
    const jobsCompleted = await waitJobsCompleted(jobs);

    /**
     * Log the result:
     * Hello world, bar
     * Hello world, foo
     */
    jobsCompleted.forEach((job) => {
        console.log(job.returnData);
    });
};

/**
 * Run in nodes that perform tasks
 */
worker.start();
/**
 * Run in the control node
 */
startTask();

License

MIT