Leveraging Power of Appwrite to Build Scheduling Applications

Building a Simple Pill Reminding Application using Appwrite in Golang

Ujjwal Sharma
6 min readOct 2, 2021
Photo by k on Unsplash

This Tutorial will take you through setting up the Appwrite Backend, Creation of Tasks with Cron Jobs in Appwrite and extending the functionality to users using Golang. I have made a very simple Golang server. However, usage of Tasks(which provides the Cron functionality) in Appwrite can be done using a server implemented in any language.

What is Appwrite and How to set it up?

Appwrite is a highly performant and secure open-source backend server that implements the tedious repetitive tasks we otherwise would have to implement from scratch again and again for our application. Having an Appwrite backend server running provides immense functionalities ranging from user authentication, database management, user management, storage utilities to team-wise solutions and health monitoring. Appwrite basically eases the developers day to day life and so it is a strong tool made completely open source. The Community builds and maintains appwrite with awesome contributors from all parts of the world.

Appwrite is completely based on Docker and can run on any machine with just Docker support. This makes Appwrite platform-independent. To install Appwrite, you can run this command:-

docker run -it --rm \
--volume /var/run/docker.sock:/var/run/docker.sock \
--volume "$(pwd)"/appwrite:/usr/src/code/appwrite:rw \
--entrypoint="install" \
appwrite/appwrite:0.10.4

This fetches the latest Appwrite image and installs the required dependencies. If you do docker ps -a you will be able to see Appwrite’s support containers up and running. After the download is complete, you will be prompted to fill in some basic information. After this, visit http://localhost/ on your browser. You will be prompted to signup for Appwrite.

Appwrite’s Signup Page on http://localhost

Fill in the details and you are good to go. Congratulations, you have set up Appwrite.

Configuring Appwrite according to your Application

Currently, you don't have any projects set up. The first thing you want to do is, create a project. We are making a basic pill reminding application. You can simply add the name and your project will be created [It might take some time based on your processing speed].

Moving forward you need an API Key. Although it is not necessary for this example if you are working with Appwrite console or some other functionality requiring read-write access, you will need this. Generate the API Key and select the functionality you will need with your application. After it is generated, you can see the secret.

If you want to initialize the Appwrite console, you can pass the Project ID found in project settings and this secret for the API Key by writing appwrite init in the console.

You have now configured Appwrite to work with your application.

appwrite init — how to pass the project credentials

Tasks in Appwrite

Tasks in Appwrite are schedules that you might want to constantly be kept running in the background. You want them to be triggered after a certain interval or at a certain point in time. If you generally implement a Cron scheduler on your own system, you might find that the performance becomes deprecated and your schedules might run later than the expected time. Appwrite handles this for us and provides us with a highly performant and accurate Cron job scheduler. All we have to do is add tasks.

To further ease our task, it provides us with an option where to send the request when a certain time or interval is hit. This means Appwrite will send the POST/GET/PUT etc request based on our configuration to this. All we have to do is configure a router to listen to this endpoint for the specific request.

Task Descriptive

I have entered the Cron 0 * * * * here. It will be triggered once every hour at the starting of the hour. Whenever this Cron’s time is met, a POST request will go to http://localhost:9090/remindtask. Here a simple CRON Syntax is used of the format * * * * *. These are space-separated. For example, if you want the Request to be triggered every one minute, you can use the CRON 1 * * * * . If you want the request to be sent at 10 AM every morning, you may use the CRON 0 10 * * * . Syntax of a CRON looks something like this :-

*-1: This represents the minutes of the schedule. [0–59]

*-2: This represents the hours of the schedule. [0–23]

*-3: This represents the day of the month of the schedule. [1–31]

*-4: This represents the month of the schedule. [1–12]

*-5: This represents the day of the week of the schedule. [ 0 -6 ]

If you input any wrong Cron Syntax, Appwrite will throw an error and not create that task for you citing failed task notification. This makes the process error-proof. By default, a failed cron query only runs 5 times and then the task is set to be paused to prevent unnecessary and extra usage. This also prevents extreme load on the server. So, faulty requests are handled automatically and we can breathe a sigh of releif.

One of my favourite features of this Task scheduler is the ability to provide HTTP headers that we can read using the router. You can add multiple Tasks with different Headers and configure the router to handle different scenarios.

Configuring Headers in a Task

I have made 2 headers named pillname and pillquantity. These will be sent along with the POST request to my endpoint. It also gives the option to limit the requests to a certain user which means that the authentication support is well extended.

Implementing a Simple Server in Golang

We have come to the final step of this tutorial. I have used Gorilla/Mux to build a simple efficient router to handle post requests. My router works on port 9090 and listens to only 1 route.

In the CronHandler function, you can see I am reading 2 HTTP headers named pillname and pillquantity and displaying the update to our targeted customer.

We can run this using go run main.go

Conclusion

Now whenever an hour starts, we get an output message like this.

Sample Output for Pill Reminder

I hope you liked the article I presented. This is a basic Proof of Concept. We can extend this concept to build a fully functional Accessibility based application. If you liked the article, do Star the Github Repository of Appwrite. Appwrite’s community support is one of the best I have come across so far! Do contribute if you like it.

--

--