Skip to Content
Course content

194: Practice Exercise: Building a Simple Task Scheduler with ZIO

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was reviewing a PR from a developer who was trying to build a "cleanup" utility for a legacy system. He had a list of about 10,000 stale records to archive, and his solution was to loop through that list and wrap each archive call in a Scala Future. On his local machine with ten records, it worked beautifully. In production, it was a disaster. The application didn't just slow down; it crashed with an OutOfMemoryError because it tried to spawn thousands of futures simultaneously, overwhelming the execution context and the database connection pool.

That's the danger of "fire and forget" concurrency without a proper strategy. When you're building a task scheduler, you aren't just worried about *if* a task runs, but *how* and *when* it runs. This is where ZIO really shines. Unlike standard Futures, ZIO fibers are lightweight, and the library gives us the primitives to control concurrency, manage state, and handle interruptions without losing our minds to race conditions.

Orchestrating Execution with Fibers and Sleep

In a ZIO-based scheduler, we don't block threads. If a task needs to run in five minutes, we don't use Thread.sleep()—which would hold a thread hostage—we use ZIO.sleep. This tells the ZIO runtime to "wake this fiber up" later, freeing the underlying thread to do other useful work in the meantime. I've seen developers try to implement their own timer loops using while(true) and Thread.sleep, and it's almost always a recipe for a deadlock or a memory leak.

To actually run these tasks in the background, we use fork. When you fork an effect, ZIO creates a fiber—a lightweight "green thread." If you have a list of tasks to trigger, you can use ZIO.foreachPar to run them in parallel, but be careful. If you're dealing with 10,000 tasks like our unlucky friend from the anecdote, you'll want to use a Semaphore to limit the number of concurrent executions. It's a simple way to say, "I don't care how many tasks are queued; only five can talk to the database at once."

Managing Scheduler State with Ref and Queue

A scheduler isn't very useful if it can't keep track of what it's doing. You'll likely need a way to store pending tasks or track the status of completed ones. In the ZIO world, we avoid var and synchronized blocks. Instead, we use Ref for shared, mutable state. A Ref is essentially a thread-safe wrapper around a value that allows you to perform atomic updates.

For a more robust scheduler, I usually recommend a Queue. A Queue allows one part of your application to "push" a task into the scheduler while a separate "worker" fiber sits in a loop, pulling tasks out and executing them. This decouples the request to do work from the actual execution. If the worker crashes or the system slows down, the queue acts as a buffer. It’s a pattern I use in almost every production system I build because it makes the system resilient to spikes in load.

// A quick glimpse at how a worker loop looks
def workerLoop(queue: Queue[TaskDefinition]): ZIO[Any, Throwable, Unit] = {
  for {
    task <- queue.take // This will suspend the fiber until a task is available
    _    <- runTask(task).catchAll(err => ZIO.logError(s"Task failed: $err"))
  } yield ()
}.forever // Keep the worker running indefinitely



📋 Practical Task

Building a Priority-Based Background Job Processor

Your goal is to implement a simple task scheduler that can handle jobs with different priority levels. High-priority jobs should be processed before low-priority jobs, and you must ensure that no more than 3 jobs run concurrently to avoid overwhelming the system.

Requirements:

  • Define a Job case class that contains a name: String, a priority: Int (where higher is more urgent), and the action: ZIO[Any, Throwable, Unit] to execute.
  • Create a JobScheduler class that uses a Ref to store a list of pending jobs.
  • Implement a submitJob(job: Job) method that adds a job to the pending list.
  • Implement a processNextJob() method that:
    • Finds the job with the highest priority.
    • Removes it from the pending list atomically.
    • Executes the job.
  • Use a Semaphore to limit the concurrency of processNextJob executions to a maximum of 3.
  • Write a main method that submits 10 jobs with varying priorities and delays, then triggers the processing of these jobs in parallel to demonstrate that the priority is respected and the concurrency limit is enforced.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.