Sometimes you want to do something at a specific time, or interval. These "tasks" are usually called background jobs. Luckily there are a couple of libraries that make background job handling super easy!
Tasker is a great CRON type scheduler for Crystal. It uses fibers to create in-memory background jobs that either run at a specific time, or at a specific interval. Let's look at a simple example bot which posts to a channel at a specific interval:
Mosquito is a bit more advanced than Tasker and uses a Redis backend to keep track of jobs. It comes with two different job types:
QueuedJob, which is a job that gets inserted into a queue and processed sequentially. Queued jobs can be rate limited so that only N number of jobs are performed every X amount of time. This can be really useful if you want to use a request that is heavily rate limited by Telegram, such as
PeriodicJob, which is a job that runs accoding to a predefined schedule. They can be used just like the above Tasker example to perform a task at specific intervals.
Let's see the above example, but with Mosquito: