Skip to content

Instantly share code, notes, and snippets.

@mipearson
mipearson / elk.rb
Created July 5, 2018 06:07
Structured Logging with Rails & ELK example files
# Copyright 2018 Marketplacer. MIT License.
require 'json'
require 'active_support/core_ext/class/attribute'
require 'active_support/log_subscriber'
module Elk
class RequestLogSubscriber < ActiveSupport::LogSubscriber
PAYLOAD_KEYS_TO_COPY = %i{
controller
@mipearson
mipearson / linode_to_aws.md
Created August 7, 2011 14:10
Migration off of linode to AWS for thebonscotts.com

So, linode bailed (http://yfrog.com/kkrk4p) while I was in the middle of working on the band website (http://www.thebonscotts.com)

That annoyed me somewhat.

Here's what I did to move it off linode in under one hour (except for DNS TTLs, obvs):

  • logged in to my (already extant) AWS account
  • created an elastic IP
@mipearson
mipearson / nginx_status_codes.rb
Created August 15, 2011 12:44
Very simple munin plugin to graph nginx HTTP error codes
#!/usr/bin/env ruby
CODES = {
'400' => 'Bad Request',
'401' => 'Unauthorized',
'403' => 'Forbidden',
'404' => 'Not Found',
'405' => 'Method Not Allowed',
'406' => 'Not Acceptable',
'408' => 'Request Timeout',
@mipearson
mipearson / bm_test.go
Last active August 19, 2021 12:55 — forked from davecheney/bm_test.go
Add an increment of a global variable after each op to ensure the Go compiler isn't being clever about optimisation.
package bm
import (
"testing"
)
var mb = map[string]bool{
"alpha": true,
"beta": true,
"gamma": true,
@mipearson
mipearson / mtgtabsbackup.md
Last active August 15, 2021 02:54
Mountain Goats tabs index backup
@mipearson
mipearson / index.ts
Created August 19, 2018 08:16
Demonstration of using AWS CDK
/*
I was curious as to whether it was possible to make resources
created using CDK constructs work with raw CloudFormation
resources and vice versa.
This aws-cdk app demonstrates a VPC and an ELB that belongs to it
in different, dependant stacks, leveraging CFN outputs / imports,
and leveraging CDK's method of passing information from one
stack to another.
*/

Asynchronous Programming

Javascript code generally handles network & IO operations asynchronously: instead of blocking on a file read or database call, a callback is called when the data is ready.

There are three main ways of handling this in Javascript (and therefore Typescript) programs: callbacks, promises, and async/await.

TL;DR

Use async/await.

@mipearson
mipearson / typescriptAsyncLoops.ts
Created September 23, 2019 01:05
Demonstration of handling asynchronous functions in loops in typescript using async/await
// Demonstration of different ways of running asynchronous functions in loops
import util = require("util");
const arr = ["a", "b", "c", "d"];
const setTimeoutPromise = util.promisify(setTimeout);
// Asynchronously wait a very short amount of time and then print our value
async function waitAndPrint(s: string): Promise<void> {
await setTimeoutPromise(Math.random() * 100);
console.log(s);
@mipearson
mipearson / buildkite_which_queue.md
Created December 11, 2018 05:11
Grabbing a list of which Buildkite pipelines use which agent queues

Run this query via the graph explorer:

{
  organization(slug: "your-org-slug") {
    pipelines(first: 500) {
      edges {
        node {
 name
@mipearson
mipearson / cookie_helper.rb
Last active July 26, 2018 04:06
Persist a paypal developer sandbox login over multiple cucumber & capybara & selenium runs.