

Using Elixir instead of cron
source link: https://www.tuicool.com/articles/hit/RvUFVrf
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Why use multiple lines of Elixir instead of a single line of cron ? Cron has been a stable in the toolbox for developers making web applications. But with Elixir (or other languages based on the Erlang virtual machine) it is not as necessary anymore.
Using Elixir over cron has some advantages, including:
- If your application runs, your scheduled job will run. The supervisor takes care of this.
- If there are errors, they will be reported to the same place as other errors from your app.
- One less runtime dependency - you don’t need cron.
- The source code that schedules the job to run is in the same language and codebase as the rest of the app.
Before Elixir

Imagine that it is the year 2000 and you are developing a web application using PHP. You want to periodically run a command to delete some temporary files from a directory. PHP application code is usually run when someone goes to their browser and clicks a link to you web site. However you do not want to run that command every time someone clicks a link to you website. So you decide to use cron. Just a single line of slightly cryptic code will make cron execute a command of your choosing. You manually add the cron entry to the server and verify that it works.
Then a year later you move the application to a new server. You make sure that PHP is there and that it has access to a MySQL server. Everything seems to be working fine. Shortly after you stop working on that application and new developers take over. They notice that server is starting to run out of space. A directory with files is growing and growing in size. They are not sure if the files can be deleted or not. Oops. When moving to the new server you forgot to add the cron job to the new server. However there were no warnings. And the PHP source code did not have any code that deleted the files.
The single line added to the crontab was a piece of the system. But because it manually added on a server and not added to the PHP source code, it was not part of the documented history of the system.
Of course instead of manually adding the cron tab, you can add the cron job to provisioning and deployment scripts which are tied to the application. The provisioning script would install things necessary for the cron job. Suddenly it is not just a single line of crontab entry that is necessary. Now it is that line, plus making sure that cron is present, plus code to update the crontab when there are changes.
A new solution
Fast forward to 2018. You are making a web app again. This time using Elixir. Again you need to periodically execute a command to delete temporary files. Instead of using cron, you use Elixir. With Elixir it is easy to add extra processes that can run code when you want. A very simple GenServer is all it takes to periodically run a command. If you prefer, there is a library, Quantum , that uses the same kind of syntax as cron.
A single line of code can add it to the supervision tree of the application. This means that when the application runs - the code is executed. If something goes wrong, there are error messages. The code to schedule is no longer outside the code base. It no longer logs errors to a different place. It no longer requires cron to be available at runtime and the cron job to be set up and configured.
As with other uses of supervised processes in Elixir, you can trust that if the application is up, then the code is executed. If anything goes wrong there are errors that can be monitored in the same way as other errors in the system.
Comparing apples to oranges
What at first seems like “just one line in the crontab” is not the equivalent of adding a line to the Elixir source code. If provisioning is not already there for setting up the crontab and maintaining changes, that has to be added. This is just in order to make sure that the cron jobs are available and up to date.
When looking at the system as a whole the cron jobs are a part of what is necessary. If you already have procedures for developing, versioning and deploying code, you probably want the same for cron jobs. Going from not using cron to using cron is more than just manually logging on to a production server and running crontab -e
if you want to have the same maintanability, version history and deployment process as your existing code.
New possibilities
Languages running on the BEAM (Erlang, Elixir , LFE etc.) add new possibilities compared to less concurrent languages that many developers have been using before. Suddenly things that required a dependency can now be done in the language itself. In fact in some cases, such as RabbitMQ, those dependencies were written in Erlang to begin with.
This means possibilities to reduce dependencies, simplify development, deployment, provisioning and monitoring.
Recommend
-
78
-
52
README.md
-
30
简介 cron 一个用于管理定时任务的库,用 Go 实现 Linux 中 crontab 这个命令的效果。之前我们也介绍过一个类似的 Go 库——
-
11
Run Your Flask Regularly Scheduled Jobs with Cron A common need of web applications is to have a periodically running task in th...
-
6
Reasons for using Elixir instead of cron Sep 4, 2018 Why use multiple lines of Elixir instead of a single line of cron? Cron has bee...
-
12
Using a Cron Job to Run Rake Tasks Inside Your Docker Container Nov 29, 2019 Despite cron being an age old *nix tech...
-
16
Adding Cron to a Dockerized Rails Application Using Clockwork May 11, 2017 If you talk to any computer scientist they will easily tell you that 50 plus years into the computer age, scheduling is NOT a solved problem. E...
-
6
How has my time management improved after using Cron?The app changed my view of the calendar
-
7
How to schedule jobs using the Linux 'cron' utility Scheduling tasks to run automatically at specific times is essential knowled...
-
2
Prune any cache directory using cron (prune Magento cache/sessions)...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK