Skip to content

Instantly share code, notes, and snippets.

View lpeppe's full-sized avatar
🎯
Focusing

Luca Peppe lpeppe

🎯
Focusing
View GitHub Profile
@lpeppe
lpeppe / fib.scala
Last active July 2, 2018 08:53
Fibonacci implementation in scala
def fib(n: Int): Int = {
def go(n: Int, prev: Int, curr: Int): Int = {
if(n == 0) prev
else go(n - 1, curr, prev + curr)
}
go(n, 0, 1)
}
fib(5)
@lpeppe
lpeppe / gcd.scala
Created July 4, 2018 14:26
Greatest common divisor in scala
def gcd(a: Int, b: Int) =
if(b == 0) a
else gcd(b, a%b)
// const prom1 = new Promise((resolve, reject) => {
// resolve(2 + 2);
// });
// const prom2 = new Promise((resolve, reject) => {
// setTimeout(() => resolve(1 + 1), 2000);
// });
// // prom1.then(console.log);
// Promise.all([prom1, prom2]).then(console.log)

Keybase proof

I hereby claim:

  • I am lpeppe on github.
  • I am luca_peppe (https://keybase.io/luca_peppe) on keybase.
  • I have a public key ASC-ca8kfZMvNK-cxab93IDHZRoFfiHkL-TZzZ-Ei8kmGAo

To claim this, I am signing this object:

@lpeppe
lpeppe / phx_healthcheck.json
Created April 12, 2021 14:36
phx_healthcheck_json
{
"database": "healthy",
"document_processor": "healthy",
"payment_system": "unhealthy"
}
defmodule PhxHealthcheck.Healthcheck do
@callback check_status() :: String.t()
end
defmodule PhxHealthcheck.Healthcheck.Services.Database do
alias PhxHealthcheck.{Healthcheck, Repo}
import Ecto.Adapters.SQL
@behaviour Healthcheck
def check_status() do
try do
query(Repo, "SELECT 1")
rescue
defmodule PhxHealthcheck.Healthcheck.Worker do
use GenServer
@spec start_link(list()) :: GenServer.on_start()
def start_link([service | _] = args) do
GenServer.start_link(__MODULE__, args, name: service)
end
@impl true
def init([service, refresh_interval]) do
defmodule PhxHealthcheck.Healthcheck.Supervisor do
alias PhxHealthcheck.Healthcheck.Worker
use Supervisor
def start_link(init_arg) do
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
@impl true
config :phx_healthcheck, :healthcheck,
services: [
{PhxHealthcheck.Healthcheck.Services.Database, [name: "database", refresh_interval: 10000]}
]