Code Monkey home page Code Monkey logo

tachyon's Introduction

Tachyon ⚡️

Simple, small, and flexible task scheduler for Deno, backed by MongoDB.

NOTICE: This library is not 100% ready so use it with caution. 🙏

Simple Usage

You will also need a working Mongo database (v3) to point it to.

import { Tachyon } from "https://deno.land/x/[email protected]/mod.ts";

const tachyon = new Tachyon({
  maxConcurrency: 20,
  db: {
    uri: "mongodb://localhost:27017",
    name: "my-db",
    collection: "_tasks",
  }
});

tachyon.define("echo-date", () => {
  console.log(new Date());
});

await tachyon.start();
await tachyon.every(2000, "echo-date");

The available config options can be found here: https://github.com/alexalikiotis/tachyon/blob/master/lib/types.ts#L13

Methods

Methods starting with a $ are meant to be used internally by the runtime and not by the user !!!

define(name, fn)

The define method lets you define the task function that the runtime will execute when called.

tachyon.define("echo-date", () => {
  console.log(new Date());
});

start()

By using the start method you essentially start the task processing by the runtime. (async)

await tachyon.start();

Top level await supported by Deno really gets handy here 🎉

now(name, [data], [options])

The now method subscribes a task to runtime to run as soon as possible. (async)

tachion.define("print", ({ message }: any) => {
  console.log(message);
});

await achyon.now("print", { message: "Hello, Deno!" });

For available options check here: https://github.com/alexalikiotis/tachyon/blob/master/lib/types.ts#L18

every(interval, name, [data], [options])

The every method subscribes a recurring task to runtime's task queue. Keep in mind, interval supports only milliseconds for now. (async)

await tachyon.every(2000, "echo-date");

schedule(cron, name, [data], [repeat], [options])

The schedule method allows you to create a task based on a cron syntax (repeated or not). (async)

await tachyon.schedule("* * * * *", "print", { message: "this is a cron based task!" });

Timezones are not supported yet, but there coming eventually!

create(name, [data], [options])

Although the recommended way to register tasks is with the now, every and schedule methods, there is also the option to create tasks manually. (cool thing is you can chain task methods using this approach)

// create `raw` task
const task = create("send-email", "[email protected]")
  .interval("0 0 * * THU")
  .timeout(10000)
  .retries(2)
  .repeat(true);

await task.save(); // save task to queue

stop()

Use the stop method if you want to gracefully shutdown the runtime.

tachyon.stop() // graceful shutdown

makes process-interval undefined and clears the task queue

Events

ready

When the runtime will be ready for processing tasks will emit a ready event.

tachyon.on("ready", () => {
  console.log("tachyon runtime is ready now 🖖");
});

start

When a task is ready to be processed the runtime will emit the start event with the task instance as a parameter.

tachyon.on("start", (task: Task) => {
  console.log(`task ${task.name} is about to run...`);
});

start:[task]

With the generic start event emitted (for all tasks), the runtime will also emit a start event for the specific task.

tachyon.on("start:echo-date", (task: Task) => {
  console.log(`task ${task.name} is about to run...`);
});

success

When a task finishes successfully the runtime will emit a success event with the task instance and the task's result as parameters.

tachyon.on("success", (task: Task, result: any) => {
  console.log(`task ${task.name}'s result is ${result}`);
});

success:[task]

Following the pattern from the start event, the runtime will also emit a specific success event for task.

tachyon.on("success:fetch-api", (task: Task, result: any) => {
  console.log(`API response is ${JSON.parse(result)}`);
});

fail

When a task fails the runtime will emit a fail event. (this is the recommended way to handle task errors)

tachyon.on("fail", (task: Task, err: any) => {
  console.log(`task ${task.name} failed with error: ${err}`);
});

fail:[task]

Once again there is also a fail event for a specific task. (this is the recommended way to handle task errors)

tachyon.on("fail:fetch-api", (task: Task, result: any) => {
  console.log(`can't fetch API: ${result}`);
});

complete

After a task is run and finishes (success / fail) the runtime will emit a complete event for clean up purposes.

tachyon.on("complete", (task: Task) => {
  console.log(`task ${task.name} completed 😎`);
});

complete:[task]

It gets boring now but the runtime will also emit a complete event for targeted tasks.

tachyon.on("complete:fetch-api", (task: Task) => {
  console.log(`task ${task.name} completed 😎`);
});

Contributing

Pull requests are welcome; My free time is limited but I'll try to review them as soon as possible... ❤️

License

The MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.