Skip to content

Instantly share code, notes, and snippets.

View Gazler's full-sized avatar

Gary Rennie Gazler

View GitHub Profile
@potatosalad
potatosalad / jws-payload-example.ex
Created September 22, 2015 15:53
Example of extracting the payload from a JWS signature
jwk_map = %{ "kty" => "oct", "k" => :base64url.encode("secret")}
jws_map = %{ "alg" => "HS256" }
jwt_map = %{ "test" => true }
signature = JOSE.JWT.sign(jwk_map, jws_map, jwt_map)
signature_compacted = JOSE.JWS.compact(signature)
{_, signature_map} = JOSE.JWS.expand(signature_compacted)
payload = JOSE.JWT.from(:base64url.decode(Map.get(signature_map, "payload")))
# one-liner version:
def update_company(company, company_params) do
changeset = Company.changeset(company, company_params)
if Ecto.Changeset.fetch_change(changeset, :shortname) != :error do
Logger.debug "Shortname changed"
#TODO dont allow shortname change if shortname is in use in video addresses
:ok
end
Repo.transaction( fn ->
case Repo.update(changeset) do
@jedsundwall
jedsundwall / measured-voice-terms-of-service.md
Last active December 18, 2015 18:29
This gist contains Measured Voice's government-friendly terms of service, which you can see at http://measuredvoice.com/terms. Measured Voice owns the copyright to these terms, but you are free to copy them for your own use under a the Creative Commons Attribution-ShareAlike 3.0 license (http://creativecommons.org/licenses/by-sa/3.0/). We want t…

Measured Voice Terms of Service

Amended on 25 January, 2013

1. Your Acceptance of Terms

1.1 Your use of Measured Voice and the Measured Voice web site (collectively referred to as the “Service”), is subject to the terms of a legal agreement (“Terms”) between you and Captura Group Inc., DBA Measured Voice (“Measured Voice”). The following Terms of Services ("TOS") explains the agreement and the terms of that agreement.

1.2 In order to use the Service, you must first agree to the Terms. You may not use the Service if you do not accept the Terms. You can accept the Terms by (a) actually using the Service, or (b) clicking a box where such box is made available to you. If you do not agree to any of the following Terms, please do not use the Service. You should print or otherwise save a copy of these TOS for your records.

@henrik
henrik / say_when.exs
Last active September 7, 2016 09:03
Example of using "guard clauses" in your own code with Elixir macros. Inspired by `plug :foo when action in [:create, :update]`. This implementation accepts any types of conditions, not just the subset allowed in actual guard clauses. But also see https://gist.github.com/henrik/68f136f9916165e0defb for an implementation with real guard clauses.
defmodule Lab do
defmacro say({:when, _, [message, condition]}) do
{result, _} = Code.eval_quoted(condition)
if result do
quote do
IO.puts unquote(message)
end
end
end
@astery
astery / mix_test_summary.sh
Created September 15, 2017 12:24
Bash script which add summary after running `mix tests` in umbrella root
#!/usr/bin/env bash
set +m
shopt -s lastpipe
app_name="unknown"
declare -A app_tests
declare -A app_fails
declare -A app_warnings
declare -A app_errors
@ottomata
ottomata / kafka-mirror
Created August 12, 2013 21:15
init.d script for Kafka MirrorMaker
#!/bin/sh
#
# /etc/init.d/kafka -- startup script for the kafka distributed publish-subscribe messaging system
#
# Written by Alexandros Kosiaris <akosiaris@wikimedia.org>
#
### BEGIN INIT INFO
# Provides: kafka
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
@bitwalker
bitwalker / config.ex
Created July 19, 2016 23:00
Useful config wrapper for Elixir
defmodule Config do
@moduledoc """
This module handles fetching values from the config with some additional niceties
"""
@doc """
Fetches a value from the config, or from the environment if {:system, "VAR"}
is provided.
An optional default value can be provided if desired.
@mmmries
mmmries / 01.README.md
Last active November 11, 2022 16:59
Load Test Phoenix Presence

Phoenix Nodes

First I created 3 droplets on digital ocean with 4-cores and 8GB of RAM. Login as root to each and run:

sysctl -w fs.file-max=12000500
sysctl -w fs.nr_open=20000500
ulimit -n 4000000
sysctl -w net.ipv4.tcp_mem='10000000 10000000 10000000'
@aaronjensen
aaronjensen / drain_stop.ex
Last active November 28, 2022 06:59
Phoenix Drain Stop
# ATTENTION: This is now supported in plug_cowboy as of 2.1.0:
# https://hexdocs.pm/plug_cowboy/Plug.Cowboy.Drainer.html
defmodule DrainStop do
@moduledoc """
DrainStop Attempts to gracefully shutdown an endpoint when a normal shutdown
occurs. It first shuts down the acceptor, ensuring that no new requests can be
made. It then waits for all pending requests to complete. If the timeout
expires before this happens, it stops waiting, allowing the supervision tree
to continue its shutdown order.
@jadeallenx
jadeallenx / discussion.md
Last active April 21, 2023 17:13
When does terminate/2 get called in a gen_server?

When does terminate/2 get called in a gen_server?

This is what the [official documentation][1] says about the terminate/2 callback for a gen_server:

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.

Reason is a term denoting the stop reason and State is the internal state of the gen_server.

Reason depends on why the gen_server is terminating. If it is because another callback function has returned a stop tuple {stop,..}, Reason will have the value specified in that tuple. If it is due to a failure, Reason is the error reason.