Skip to content

Instantly share code, notes, and snippets.

@JoeZ99
JoeZ99 / myaws_metadata.ex
Last active April 12, 2021 12:51
to get region an instance is running at
defmodule MyAws.Metadata
# AWS InstanceMetaData URL
@meta_path_region "http://169.254.169.254/latest/meta-data/placement/availability-zone"
@region_regexp ~r/(?<continent>eu|us)-(?<zone>[^-]+)-(?<number>[\d]+)/i
@doc """
Get aws region.
First, it tries with :some_app, :aws_region variable stablished in config (hopefuly using env vars), and if no
it assumes the code is running in an ec2 instance, so it tries to get the region from instance metadata.
@JoeZ99
JoeZ99 / xml_event_handler.ex
Last active May 8, 2022 23:18
An elixir module sax parser
defmodule XmlEventHandler do
@moduledoc """
Module to parse xml with lists of items based on events
"""
@behaviour Saxy.Handler
@type element :: %{
name: binary,
attributes: [{binary, binary}],
@JoeZ99
JoeZ99 / xml.ex
Last active May 8, 2022 23:29
Use sax to make stream output out of parsing xml files
defmodule XmlStreamer do
defp to_elements_stream(xml_stream) do
{:ok, partial} = Saxy.Partial.new(XmlEventHandler)
Stream.chunk_while(xml_stream, partial, &maybe_items/2, &finalize/1)
end
defp maybe_items(chunk, partial) do
with {:cont, partial} <- Saxy.Partial.parse(partial, chunk),
{:ok, partial, items} <- fetch_items(partial) do
@JoeZ99
JoeZ99 / memory_monitor.ex
Last active April 9, 2024 10:19
A simple elixir "memory monitor"
defmodule MemoryMonitor do
@moduledoc """
This module implements a GenServer that monitors the memory usage of a process.
It periodically checks the memory consumption.
"""
use GenServer
@time_to_check :timer.seconds(10)