Skip to content

Instantly share code, notes, and snippets.

View rainabba's full-sized avatar
🎧
Working from home

Michael Richardson rainabba

🎧
Working from home
  • Asurion
  • Manassas, VA
View GitHub Profile
@rainabba
rainabba / README.md
Last active May 8, 2023 16:24
Bash retry with exponential backoff

A 1-liner bash function to provide basic non-zero exit retry with exponential backoff:

Minified

function retry_backoff {local m=${ATTEMPTS:-5};local t=${TIMEOUT:-1};local a=0;local e=1;while (($a < $m));do "$@";e=$?;if (($e == 0)); then break;fi;echo "Failure! Retrying in $t.." 1>&2;sleep $t;((a++));t=$((t*2));done;if (($e != 0));then echo "Max attempts reached!" 1>&2;fi;return $e; }

Use

retry_backoff $YOUR_COMMAND # default of 5 retries, starting with 1 second timeout
@rainabba
rainabba / README.md
Created December 13, 2021 20:25
How to IIFE the switch statement and more

First, credit to the answer at https://stackoverflow.com/a/55944296/901899

Second, if you aren't comfortable with an IIFE (immediately executed function), you will want to check out https://www.tektutorialshub.com/javascript/immediately-invoked-function-expressions-iife/.

Now, because switch() {} is a "statement" in JavaScript and not a function, it can't be IIFE executed with the following

let failure = ( switch(x) { default: return true; } )(x);

You will get: "Uncaught SyntaxError: Unexpected token 'switch'"

@rainabba
rainabba / index.js
Last active November 23, 2021 17:08
Understanding execution order in Express handlers
/*
To demonstrate the importance of understanding async execution order in the context of Express route handlers
Try in your browser at https://runkit.com/rainabba/understanding-execution-order-in-express-handlers
**/
const express = require('express'),
app = express(),
PORT = 3000,
@rainabba
rainabba / index.js
Created October 12, 2021 01:37
Clarifying javascript execution order
console.time('full execution');
console.time('code block');
function showExecutionOrder(parm) {
console.time('outside promise');
new Promise((resolve, reject) => {
console.time('inside promise');
resolve(parm);
console.timeEnd('inside promise');
}).then((data) => {
// Lookup mdn and invoke the account sync as required
@rainabba
rainabba / input_template.yaml
Created March 24, 2021 20:22
Where did 'IntelDataApi' come from and why is my deploy failing because of it?
AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::Serverless-2016-10-31'
# ... much omitted because it's unrelated
Globals:
Api:
OpenApiVersion: 3.0.1
Auth:
DefaultAuthorizer: NONE # AWS_IAM changed because we can't override on lambdas at this time. See https://github.com/aws/serverless-application-model/issues/984
@rainabba
rainabba / template-using-sam.yaml
Created March 10, 2021 21:28
Binding AWS Lambda, API Gateway, Route 53 (DNS) and SSL; with and without SAM
Parameters:
StageName:
Type: String
Description: Major version changes should get new values (v1, v2, etc..)
Default: v1
Conditions:
CreateZone:
!Equals [!Ref ZoneId, 'none']
CreateCert:
@rainabba
rainabba / README.md
Last active October 23, 2020 21:40
Quickly and easily map/include field from an array of objects

My use case was an array of objects coming from a document-store so they're extremely variable and we only want a sub-set of the returned fields. There are plenty of ways to go about this and I hope snippet will serve that purpose, but for me this was more about realizing the power of () to do a bit more with the Lambda passed to map(). I regularly pull up ANY chrome page, hit Ctrl+Shift+J and develop small snippets right in the console, which is what led to 1 of 2 discoveries today.

How I would have gone about this in the past

let result = { Items: [{ a: 1, b: 2, x: 3 }] },
 newList = result.map( o => { return { a: o.a, b: o.b }; );

My new solution

@rainabba
rainabba / cp-error.sh
Created September 16, 2020 23:27
cp - 'are the same file'
## Below is a script/log (anonymized) of an instance where I'm getting "are the same file" errors from `cp`
## Clearly they are not "the same file", but have identical contents and the 2nd is from a `git clone` command
#!/bin/bash
# me@myhost:/mnt/c/prj/someorg/some-project$ ls -lah;
ls -lah;
# total 0
# drwxr-xr-x 1 someuser root 512 Sep 15 10:26 .
# drwxr-xr-x 1 someuser root 512 Jun 3 06:48 ..
# drwxr-xr-x 1 someuser root 512 Apr 16 10:46 .vscode
@rainabba
rainabba / pdfToImageTest.js
Created February 8, 2019 16:58
Testing common solutions to PDF -> image conversion
// SPOILER: Only 1 of these tests passes at this point and it bypasses
// all the fluff and complication so it's the one I will end up using
const { expect } = require('code'),
Lab = require('lab'),
lab = exports.lab = Lab.script(),
fs = require("fs"),
path = require("path"),
mkdirp = require('mkdirp'),
rimraf = require('rimraf'),
@rainabba
rainabba / config-docker.sh
Last active September 6, 2022 03:46
Install docker-ce, docker-compose and docker-sync in Ubuntu for WSL
#!/bin/bash
#
#This script assumes very little other than a fresh Ubuntu install (using the Windows store) on Win10 1709 or newer with WSL installed already
#In Powershell, run the following to install WSL and Ubuntu
#
#Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
#Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1604 -OutFile ~/Ubuntu.zip -UseBasicParsing
#Expand-Archive ~/Ubuntu.zip ~/Ubuntu
#~/Ubuntu/ubuntu.exe
#