- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
This blog post covers how to implement Round Robin Scheduling in Python. We’ll look at what is required to get started with Nylas. We recently discussed this topic on our Coding with Nylas Livestream:
Let’s dive in!
Round Robin Scheduling is a method to handle multiple requests given limited resources. For example, when we share calendar availability across multiple service providers, a limited resource, allowing customers to book an appointment.
The Nylas Scheduler allows us to implement round robin scheduling where we select different scheduling algorithms programmatically. Applying efficient scheduling algorithms enables us to optimize for our specific use case.
When a user wants to book an appointment, you can configure different approaches to surfacing the available time slots. This is when round robin scheduling is useful. Using round robin scheduling allows you to modify displaying availability across different calendars. We will take a look at the different round robin configuration options shortly.
There are different algorithms available when scheduling the processing of different tasks. Round robin scheduling originates from processing different tasks by a central processing unit (CPU) in your computer. The goal of round robin scheduling is to allocate a fixed amount of time to process a task before moving to the next one.
We’ve recently explored the Round Robin algorithm on a past Coding with Nylas livestream:
With the Nylas Scheduler, we implement a similar round robin algorithm which we will take a look at shortly.
Python gives you the flexibility to programmatically create multiple calendars for your users (e.g. health care service providers) and source availability to their users. With the Smart Scheduler integration, you can use our API to create and manage calendars.
In this blog post, we will be modifying an existing Nylas Quickstart Guide to build round robin scheduling in python. First head over to Nylas.com and register to create an account or login.
We do not need to generate API keys to start using the quickstart guide! We can use the free tier that comes with 10 connected accounts. And each quickstart guide includes the Nylas API keys, so you can download and run the code locally.
We are building off an existing Nylas Quickstart Guide for implementing the Nylas Scheduler:
The scheduler quickstart will provide us with a basic scheduling application from scratch that we can modify with the ability to apply round robin scheduling:
We start with an instant scheduler and will need to implement a smart scheduler integration. Check out our docs on the different scheduler implementations.
From the Nylas docs on scheduler implementations, we will need to pass an edit token to the user instead of the access token. An edit token will only allow the user to edit an existing calendar, whereas an access token can be used to both create and edit calendars. This is important from the perspective of who controls calendar creation. Using the smart scheduler integration, we can create calendars via the API and collect availability across all the different calendars using round robin scheduling.
We will first modify the quickstart guide backend to create a new calendar for each user on authentication:
# file in /backend/server.py # added to def exchange_code_for_token() method scheduler = nylas.scheduler.create() # Create an scheduler with a url slug for each user connected scheduler.name = "name" # Note that the slug must be unique for each user scheduler.slug = "scheduler-smart-1" scheduler.config = { "event": { "title": "Service", }, } scheduler.access_tokens = [access_token] scheduler.save() return { 'id': user['id'], 'emailAddress': user['email_address'], 'accessToken': user['access_token'], # We are passing back the editToken to the frontend 'editToken': scheduler['edit_token'] }
Now we’ve update the backend application to create a new scheduler for each user to modify.
Next we will modify the scheduler implementation on the frontend by modifying the authentication to only allow for editing a scheduler:
nylas.scheduler.show({ auth: { //Instant Scheduler:access token to create/modify calendars //accessToken: sessionStorage.getItem('accessToken'), //Smart Scheduler: edit token to edit a single calendar pageEditToken: sessionStorage.getItem('editToken'), }, // ...Rest of the scheduling configuration...
Now we’ve updated the frontend application to allow the user to modify a single calendar created for them.
Here we will further explore how to implement Round Robin scheduling. The idea behind creating a single scheduler for each user is that this allows us to collect availability across multiple users to share at once. So for example, if we are creating appointment availability for patients, we can collect the availability across all calendars using the calendar/availability endpoint:
curl --location --request POST 'https://api.nylas.com/calendars/availability' \ --header 'Accept: application/json' \ --header 'Content-Type: application/json' \ # Add your access token for the calendar availability request --header 'Authorization: Bearer <ACCESS_TOKEN>' \ --data-raw '{ "duration_minutes": 15, "start_time": 1682678845, "end_time": 1682765245, "interval_minutes": 5, "emails": [ "[email protected]", // ADD MORE EMAILS HERE ], "free_busy": [], "buffer": 30, "round_robin": "max-availability", "open_hours": [ { "emails": [ "[email protected]", // ADD MORE EMAILS HERE ], "days": [0, 1, 2, 3, 4], "timezone": "America/Los_Angeles", "start": "10:00", "end": "16:00", "object_type": "open_hours" } ] }'
Here is an example response for calendar availability:
{ "object": "availability", "order": [ "[email protected]", "[email protected]" ], "time_slots": [ { "emails": [ "[email protected]" ], "end": 1682702100, "end_time": 1682702100, "object": "time_slot", "start": 1682701200, "start_time": 1682701200, "status": "free" }, { "emails": [ "[email protected]" ], "end": 1682702400, "end_time": 1682702400, "object": "time_slot", "start": 1682701500, "start_time": 1682701500, "status": "free" }, { "emails": [ "[email protected]" ], "end": 1682702700, "end_time": 1682702700, "object": "time_slot", "start": 1682701800, "start_time": 1682701800, "status": "free" }, { "emails": [ "[email protected]" ], "end": 1682703000, "end_time": 1682703000, "object": "time_slot", "start": 1682702100, "start_time": 1682702100, "status": "free" }, { "emails": [ "[email protected]" ], "end": 1682703300, "end_time": 1682703300, "object": "time_slot", "start": 1682702400, "start_time": 1682702400, "status": "free" }, { "emails": [ "[email protected]" ], "end": 1682703600, "end_time": 1682703600, "object": "time_slot", "start": 1682702700, "start_time": 1682702700, "status": "free" }, { "emails": [ "[email protected]", "[email protected]" ], "end": 1682722800, "end_time": 1682722800, "object": "time_slot", "start": 1682721900, "start_time": 1682721900, "status": "free" } ] }
In the above we are using the round robin option of max-availability
allows for showing the most available time slots. The alternative option is max-fairness
which shares time slots by the least booked providers to spread the bookings across more providers.
In this section, we looked at the different steps to build round robin scheduler in python.
In this blog, we covered how to implement Round Robin Scheduling using Python and Nylas. You can sign up for Nylas for free and start building!
Continue building with Nylas by exploring different quickstart guides or visiting our developer documentation.
Ram loves teaching, building and exploring technologies. He is passionate about empowering developers to ship amazing products to market as fast as possible ????. Ram is excited to share knowledge and help others. He’s a Relaxed Tomato ????.