KekKekington

KekKekington

Best practices when working with periodic tasks

I was tasked with a low-priority project at work that will be used to monitor any websites/services belonging to our clients (mantained by us) and figured that it could be a good time to show the benefits of using Elixir. My thought was that since this is a very simple task, I could focus on following the best practices and as a practical excercise for me, since I always read/listen about Elixir but don’t have much time to actually write it…

What I have planned so far is a supervised GenServer that routinely calls itself to execute the task every X seconds, where the task is performing a GET at /healthz and looking for a 200 reply or a basic ping if I get a 404 there. That task would run that function for each domain registered using a Task. All of this would be published to a very simple LiveView dashboard (to show LiveView’s potential, too).

I was also planning on using a document-store in GCE or AWS to store the registered domains (keeping the program very simple and sort of stateless, I’d like to try and run it in Kubernetes for testing).

I’m aware that this is a very simple task but I’d love any opinions or suggestions on this. I don’t want to over-engineer since this will be used only for a few websites, but I also don’t want to “brute force” everything, so to speak.

As a sidenote, I had the idea of doing this with LiveView but optimizing it for viewing through a terminal with cURL or similar programs. e.g. calling the service’s endpoint and start viewing a TUI dashbord. Would this be too crazy?

Thanks in advance.

First 9 of 9 Posts Switch mode

mindok

mindok

There’s a good writeup (plus a library) for period tasks on @sasajuric 's blog - The Erlangelist - Periodic jobs. This approach is very light on infrastructure. If you need full tracking, retry etc then the community seems to lean towards @sorentwo 's Oban project - GitHub - oban-bg/oban: 💎 Robust job processing in Elixir, backed by modern PostgreSQL, SQLite3, and MySQL · GitHub - but that comes with more infrastructure (i.e database) to persist job status etc.

They should be able to give you some inspiration.

webuhu

webuhu

I did a similar project for monitoring, but never had time to finish it to be ready to open source it.

First of all often there aren’t such a thing as best practices in Elixir.
There could be more then one suitable good solution.
That’s one of the beautiful things in Elixir.

In my project for the scheduled “ping” requests it just used Task and TaskSupervisor.

The periodic task in its simplest form (in my opinion):

use Task, restart: :transient

# 60 s
@interval 60 * 1_000

def start_link(_) do
  Task.start_link(&process/0)
end

def process() do
  receive do
  after
    @interval ->
      # do your work here
      IO.inspect "ping"

      process()
  end
end

For storing the registered domains I just used a File which I read at application start.
About the TUI thing I can’t give any suggestions.

al2o3cr

al2o3cr

You mentioned you were tasked with the project at work; was there any discussion of build-vs-buy? It’s going to be challenging to show the benefits of Elixir when competing with off-the-shelf tools like Pingdom that do this exact job (plus alerting, dashboards, etc) for about 1 US dollar per month per site.

If there are requirements that aren’t served by the market - could be anything from regulatory requirements, to IP filtering on the target sites, to client confidentiality - then make sure your Elixir hype focuses on how it can help solve those unique problems.

KekKekington

KekKekington OP

We’re a VERY small team so we try to keep our tooling costs down, specially when they won’t really benefit our productivity. The truth is that this project is very low priority and it wouldn’t even be a problem to just not do it. As someone who struggles finding or coming up with projects to practice, this seemed like a perfect opportunity.

Otherwise, I’d agree with you that buying would be better in most cases but I’m looking at this as a small personal challenge.

KekKekington

KekKekington OP

Thank you. I read up Dockyard’s write up about not using external dependencies for periodic tasks, but that was from 2017… I’ll check the links you shared :grin:

KekKekington

KekKekington OP

Thank you for sharing your code and insight, I’ll look into Task and TaskSupervisor more.

mpope

mpope

A very simple way is using Process.send_after/4. A gen_server can just send itsself a repeating message.

cnck1387

cnck1387

There’s also https://uptimerobot.com/ which is free up to 50 sites and polls your site every 5 minutes. You can get alerts, a 30 day uptime history and response times on the free tier. I’ve been using them for years.

But if you do roll your own, database persistence seems reasonable so you can measure uptime over X time. That would likely mean storing the results in a DB, in which case Oban wouldn’t introduce additional complexity if you’re already using Postgres.

KekKekington

KekKekington OP

That might actually be a good option, I guess we could use that but I’ll still build the app either way as practice.

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement