[Project A] Devlog #6: Task Scheduling
Before we begin, you can get the previous entry in this series here.
It's been an interesting couple of days. I'm about done with the MVP that I wanted to release. So I'll just recap the things I did within the period I didn't release any devlogs.
UI
I integrated the UI with the elixir backend. Currently, the UI gets the member and attendance data from the elixir backend instead of the supabase backend. I'll probably shut down the service soon or repurpose it for another project, not sure yet. I can create members and update members from the UI now with my backend. The attendance bit is pending the update but that will be the last feature to implement in the UI for now. Oh, and I also setup Sentry so that when the application is deployed, I can monitor it for crashes and other bugs that I didn't catch in development.
Backend
There are a few things I did on this side. I set up sentry here as well and I also set up task scheduling for the attendance. The attendance is supposed to be generated for a very specific day so I wrote a method to do that. My initial idea was to have the task run on the day it's supposed to be generated for but after a conversation with a friend I decided to have it run a day or two before(not decided yet) so that in case something goes wrong, I can debug and have enough time to fix the issue before it is supposed to be used. Setting up the task scheduling was rather straightforward. (I've noticed that some of these things are rather easy to set in elixir)
Setting up Task Scheduling
I used the quantum
library to schedule my tasks. You can add it as a dependency in your mix.exs
file. You'll then need to run mix deps.get
as you usually do when you add a new library.
The next thing was to create a module for handling my tasks. I will have more than one task to schedule so having a module to handle that will come in handy.
defmodule MyApp.Task do
...rest of module goes here
end
I also created a module that uses Quantum as per the documentation. Setting it up was also straightforward
defmodule MyApp.Scheduler do
use Quantum, otp_app: :my_app
end
I then added the scheduler to my config.exs
.
config :my_app, MyApp.Scheduler,
jobs: [
{"cron expression", {MyApp.Task, :method, []}}
]
I used a cron expression to set the time the task should run. That's about it for setting up the scheduled tasks. The next thing was to test the config so I set some arbitrary time for the task to run just to make sure that the attendance was being generated properly and it worked. The only thing left to do now is to handle the updates from the UI and make sure that on the day the app is meant to be used, the attendance is only fetched for that particular day. This way the UI doesn't get more data than it needs and the users aren't overwhelmed by the volume of data coming in.
All in all, it's been an interesting couple of days. I might take a short break while I figure out the best way to do what I laid out above. In the meantime, I have to update an old app that I worked on a couple of years ago.
Until next time, I bid you adieu.