Skip to content

a1im/job-allocation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published