npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tinyjobs

v0.1.2

Published

TinyJobs is tiny user-friendly background jobs framework for JavaScript runtimes.

Downloads

226

Readme

tinyjobs

TinyJobs is tiny user-friendly background jobs framework for JavaScript runtimes.

Getting Started

Optional

To make it easier to use TinyJobs, you can install the CLI globally:

npm i -g tinyjobs

Install the package:

npm i tinyjobs

Usage

To make a job, create a new file in the jobs directory. You can use the tinyjobs CLI to generate a new job:

tinyjobs init

This command will create an example job in the jobs directory and setup the necessary configuration.

Instantiate a new TinyJobs instance and add the job:

import TinyJobs from 'tinyjobs';

const tinyJobs = new TinyJobs();

// Load jobs from the jobs directory
await tinyJobs.loadJobs();

// Invoke the job to run in the background
await tinyJobs.invoke('exampleJob', { name: 'world' });

Creating a Job

Your First Job

Let's create a new job in the jobs directory:

import { Job } from 'tinyjobs';

export default class FirstJob extends Job {
  constructor() {
    super({
        name: "firstJob",
    });
  }

  async run({ name }: { name: string }) {
    console.log(`Hello, ${data.name}!`);
  }
}

Delayed Job

You can also delay how soon this jobs run after invoking it:

import { Job } from 'tinyjobs';

export default class FirstJob extends Job {
  constructor() {
    super({
        name: "firstJob",
        delay: 1000, // 1 second in milliseconds
    });
  }

  async run({ name }: { name: string }) {
    console.log(`Hello, ${data.name}!`);
  }
}

Cron Job

You can also schedule jobs to run at specific times using cron syntax:

import { Job } from 'tinyjobs';

export default class FirstJob extends Job {
  constructor() {
    super({
        name: "firstJob",
        cron: '0 0 * * *', // Run every day at midnight
    });
  }

  async run({ name }: { name: string }) {
    console.log(`Hello, ${data.name}!`);
  }
}

Concurrent Jobs

You can specify how many instances of a job can run concurrently:

import { Job } from 'tinyjobs';

export default class FirstJob extends Job {
  constructor() {
    super({
        name: "firstJob",
        concurrency: 2, // Run 2 instances concurrently
    });
  }

  async run({ name }: { name: string }) {
    console.log(`Hello, ${data.name}!`);
  }
}

Events

TinyJobs emits events that you can listen to, currently only supporting JOB_COMPLETE and JOB_FAILED.

import TinyJobs from 'tinyjobs';
import { TinyJobEvents } from "tinyjobs";

const tinyJobs = new TinyJobs();

tinyJobs.events.on(TinyJobEvents.JOB_COMPLETE, (job) => {
  console.log(job);
});

tinyJobs.events.on(TinyJobEvents.JOB_FAILED, (job) => {
  console.error(job);
});

Typed Parameters

TinyJobs uses TypeScript to provide type safety for job parameters. You can define the type of parameters your job expects in the run method.

Using the CLI, you can generate types that can be added to the TinyJobs client:

tinyjobs generate types

This will create a tinyjobs.d.ts file in the jobs folder configured in your project that you can import in your client code:

import TinyJobs from 'tinyjobs';

import type TinyJobsTypes from "./jobs/jobs.types";

const tinyJobs = new TinyJobs<TinyJobsTypes>();

This will provide type safety for the parameters passed to the job during invocation.

Roadmap

TinyJobs is not feature complete and still in early stages, you can refer to the roadmap for the list of currently planend features and their progress here.

License

TinyJobs is licensed under the Apache-2.0.