Skip to content

Instantly share code, notes, and snippets.

View AndrewDryga's full-sized avatar

Andrew Dryga AndrewDryga

View GitHub Profile
defmodule SageExample do
import Sage
require Logger
# It is better to build Saga struct in a module attribute,
# we don't need to spend resources each time we want to execute it
@create_and_subscribe_user_sage
new()
|> run(:user, &create_user/2)
|> run(:plans, &fetch_subscription_plans/2)
@AndrewDryga
AndrewDryga / application.ex
Last active February 20, 2018 22:58
Adding runtime config via Application.put_env/2 in Application start/2 callback
defmodule Sample.Application do
use Application
# ...
def start(_type, _args) do
import Supervisor.Spec
# ...
# Update application environment from system environment variable when application starts
Application.put_env(:sample_app, :configurable_env_var, System.get_env("CONFIGURABLE_ENV_VAR"))
@AndrewDryga
AndrewDryga / mix_test_watch.sh
Last active November 4, 2020 17:27
Watch for file changes and run tests for stale modules
#!/bin/bash
command -v fswatch >/dev/null 2>&1 || { echo >&2 "fswatch is not installed. To install it use 'brew install fswatch'. Aborting."; exit 1; }
command -v mix >/dev/null 2>&1 || { echo >&2 "mix is not installed. Aborting."; exit 1; }
PROJECT_PATH=$(git rev-parse --show-toplevel)
[[ "$?" != "0" ]] && echo "Stale test watch works only within git project." && exit 1;
declare -a IGNORED_PATHS
declare -i throttle_by=2
FROM nebo15/alpine-elixir:1.5.1 as builder
MAINTAINER Nebo#15 support@nebo15.com
# Always build with production environment
ENV MIX_ENV=prod
# `/opt` is a common place for third-party provided packages that are not part of the OS itself
WORKDIR /opt/app
# Required in elixir_make
{
"name": "myappapi",
"description": "MyApp Backend Application",
"scripts": {
"postdeploy": "/app/bin/myapp seed"
},
"env": {
"MY_ENV_VARIABLE": {
"required": true
}
#!/usr/bin/env bash
set -xe
# Create a release tarball
mix release --verbose
# Find tarball location
APP_NAME="myapp"
APP_TARBALL=$(find _build/${MIX_ENV}/rel/${APP_NAME}/releases -maxdepth 2 -name ${APP_NAME}.tar.gz)
@AndrewDryga
AndrewDryga / Procfile
Last active August 19, 2017 15:22
Files for Elixir deployment to Heroku
release: /app/bin/myapp migrate
web: /app/bin/myapp foreground
@AndrewDryga
AndrewDryga / rel_config.exs
Created August 19, 2017 14:54
Release config with commands
use Mix.Releases.Config,
default_release: :default,
default_environment: :default
environment :default do
set dev_mode: false
set include_erts: false
set include_src: false
set cookie: :"this_is_a_secret"
# Add release commands
@AndrewDryga
AndrewDryga / migrate.sh
Last active August 19, 2017 14:51
rel/commands
#!/usr/bin/env bash
/app/bin/myapp command "Elixir.MyAppAPI.ReleaseTasks" migrate
@AndrewDryga
AndrewDryga / release_tasks.ex
Last active July 30, 2018 08:29
Elixir Release Tasks
defmodule MyAppAPI.ReleaseTasks do
alias Ecto.Migrator
@otp_app :myapp_api
@start_apps [:logger, :ssl, :postgrex, :ecto]
def migrate do
init(@otp_app, @start_apps)
run_migrations_for(@otp_app)