Giter Club home page Giter Club logo

taskman's Introduction

Taskman

A simple and lightweight async/sync scheduling library for running tasks at certain times.

Supports

  • Running on an interval e.g. every 5 minutes, every 2.5 seconds
  • Running on a set date
  • Run after an amount of time e.g. run after 10 hours
  • Run on cron format e.g. 10 4 * * * (Run on the 10th minute of the 4th hour everyday)

Go to the docs here for more information and examples

Installation

Either run nimble install taskman or add requires "taskman" to your .nimble file

Examples

Every day get the IP address of the computer

import std/[httpclient, json]

let tasks = newAsyncScheduler()

var ip = ""

tasks.every(1.days) do () {.async.}:
  let client = newAsyncHttpClient()
  defer: client.close()
  ip = client
    .getContent("https://httpbin.org/ip")
    .parseJson()["origin"]
    .getStr()

waitFor tasks.start()

When the client first connects they get added to a queue and after 5 mins they are allowed to see the actual page

import src/taskman
import std/asynchttpserver
import std/sets

let tasks = newAsyncScheduler()


var allowed: HashSet[string]

proc cb(req: Request) {.async, gcsafe.} =
  if req.hostname notin allowed:
    # Add the client to the wait list
    tasks.wait(5.minutes) do () {.async.}:
      # Once the wait is over, allow them in
      allowed.incl req.hostname
    await req.respond(Http200, "You can join in 5 minutes")
  else:
    await req.respond(Http200, "Hello again!")

# This is NOT how you should be using asyncHttpServer
# This is just a basic example
var server = newAsyncHttpServer()
server.listen 8080.Port
echo "Running on port 8080"
while true:
  waitFor server.acceptRequest(cb)

taskman's People

Contributors

ire4ever1190 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

taskman's Issues

newTask[AsyncTaskHandler] cannot be called

The following expression worked fine:

    let task = taskman.newTask[taskman.TaskHandler](cronTiming, notification.notifyFunc())

Where notification.notifyFunc is a function which returns another function, and cronTiming is of type Cron.
Upon switching to an AsyncScheduler, though, I needed to switch the handler to an AsyncTaskHandler. Now I get the following error:

schedule.nim:34:57 Error: expression 'newTask[AsyncTaskHandler](cronTiming, notifyFunc(notification))' cannot be called

To reproduce, clone my repository, checkout commit 1148d054c349308a07d5c6c9533bb52ce22ca2e0, and run nimble build --threads=on

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.