menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

A Practica...
source image

Medium

3w

read

196

img
dot

Image Credit: Medium

A Practical Guide to Elixir’s Task Module — Let’s create a Cron Job

  • Task module in Elixir is often underutilized compared to GenServer for concurrent operations.
  • Tasks are lightweight processes for performing defined actions without much inter-process communication.
  • They serve well in transforming sequential code into concurrent code, handling tasks like large file uploads concurrently.
  • Task processes are preferred for short-lived operations without state management, unlike GenServer.
  • Task.start/1 is used for side-effects, while Task.async and Task.await are for received results.
  • A simple cron job for clearing expired tokens is an example of Task module practical usage.
  • The code snippet showcases a background process for token cleanup at specified intervals.
  • By adding the module to the supervision tree, the task starts with the app and gains fault tolerance.
  • Supervised modules need to implement start_link/1 function to receive a single argument.
  • With the added supervision, Elixir's supervisor ensures automatic restarts on task crashes for improved fault tolerance.

Read Full Article

like

11 Likes

For uninterrupted reading, download the app