Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Mahemoff mahemoff

View GitHub Profile
@mahemoff
mahemoff / elasticsearch.rake
Created Sep 19, 2012 — forked from jarosan/elasticsearch.rake
Elasticsearch reindex task
View elasticsearch.rake
# Run with: rake environment elasticsearch:reindex
# Begins by creating the index using tire:import command. This will create the "official" index name, e.g. "person" each time.
# Then we rename it to, e.g. "person20121001" and alias "person" to it.
namespace :elasticsearch do
desc "re-index elasticsearch"
task :reindex => :environment do
klasses = [Place, Person, Caption]
@mahemoff
mahemoff / README.md
Last active Dec 29, 2021
Vim Terminal Mode - A short introduction
View README.md

Vim has a Terminal Mode!

Since v8.1 (May 2018), Vim has shipped with a built-in terminal. See https://vimhelp.org/terminal.txt.html or type :help terminal for more info.

Why use this? Mainly because it saves you jumping to a separate terminal window. You can also use Vim commands to manipulate a shell session and easily transfer clipboard content between the terminal and files you're working on.

Key Bindings

@mahemoff
mahemoff / README.md
Last active Sep 25, 2021
MySQL thread-safe multi-database switching in Rails 5
View README.md

The code here is a tech demo showing how to switch between databases in Rails 5, in a thread-safe manner.

BACKGROUND: HOW NOT TO CHANGE DATABASES

The simple way to switch databases is:

ActiveRecord::Base.establish_connection :user_shard1
puts User.find(1) # this is executed on the database keyed on "user_shard1" in database.yml
ActiveRecord::Base.establish_connection :development
@mahemoff
mahemoff / uniq.rb
Created Sep 12, 2014
Ensure Sidekiq jobs are unique (if the queue contains several jobs with same class and args, this will delete all but one of them)
View uniq.rb
def self.uniq! queue_name
seen_already = Set.new
Sidekiq::Queue.new(queue_name).each { |job|
key = { job.klass => job.args }
key.in?(seen_already) ? job.delete : seen_already << key
}
end
@mahemoff
mahemoff / fixhistory.coffee
Created Jan 10, 2012
"Prevent" popstate firing on hash change
View fixhistory.coffee
currentPathname = null
window.onpopstate = (ev) ->
return if document.location.pathname == currentPathname
currentPathname = document.location.pathname
#... (now handle a real back-button or page load event)
@mahemoff
mahemoff / Ansible Disk Check
Last active May 5, 2021
Show disk space and warn about disk full in Ansible
View Ansible Disk Check
* Shows a message while asserting like:
ok: [host] => {
"msg": "disk usage 4.2B of total 20.0GB (21.0%) (should be within limit 90.0%)"
}
* Note this only looks at first mount point on current node
* Fails if disk is near-full
* Last step pushes to a push-based monitoring service, which will alert us if it doesn't get there after some time
* Need to setup a variable `disk_limit`, which is the max acceptable usage ratio, e.g. set it to 0.8 if you want to keep disks within 80% of max size
View video-client.html
<!-- This is a Node+WebSocket powered demo to sync videos
across different browsers. This file is the client,
the other one is the Node server. Powered by Node and
http://github.com/miksago/node-websocket-server -->
<style>
.inactive { display: none; }
.active { display: block; }
</style>
<script>
@mahemoff
mahemoff / gist:07c0c3427aa3eb3c0abc07f76fc68279
Created Sep 15, 2018
Show disk space and warn about disk full in Ansible
View gist:07c0c3427aa3eb3c0abc07f76fc68279
- name: show disk space
debug: msg="{{ ((mount.size_total - mount.size_available) / 1000000000) | round(1,'common') }}GB of {{ (mount.size_total / 1000000000)|round(
1, 'common') }}GB ({{ (100 * ( (mount.size_total - mount.size_available) / mount.size_available)) | round(1, 'common')}}%)"
vars:
mount: "{{ ansible_mounts | first }}"
tags: disk
- name: e
@mahemoff
mahemoff / websockets.md
Last active Nov 17, 2020
Persistent Connection Services - Pricing
View websockets.md

Here I'm interested in a use case of subscribers keeping a permanent connection open (24/7), but with negligible amount of messages. So I'm only concerned about price per connection, not price per message.

AWS API Gateway

https://aws.amazon.com/api-gateway/pricing/

$0.25 per million connection minutes

That's $1 per 4 million connection minutes.