Socket
Book a DemoInstallSign in
Socket

job-allocation

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

job-allocation

Library for distributing jobs between nodes

latest
Source
npmnpm
Version
2.0.6
Version published
Weekly downloads
3
50%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

job-allocation

FAQs

Package last updated on 25 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts