Skip to content

Instantly share code, notes, and snippets.

@mahemoff
mahemoff / timeago.js
Created June 3, 2016 14:31
jQuery timeago supporting multiple locales and weeks instead of months
/**
* Timeago is a jQuery plugin that makes it easy to support automatically
* updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
*
* @name timeago
* @version 1.4.1
* @requires jQuery v1.2.3+
* @author Ryan McGeary
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
*
@mahemoff
mahemoff / fallacies.txt
Last active January 29, 2023 05:03
Avoiding Decision-Making Pitfalls: A Taxonomy of 25 Common Fallacies and How to Recognize Them (by ChatGPT)
Cognitive biases:
Confirmation bias: The tendency to seek out and interpret information in a way that confirms one's existing beliefs. Example: A person who believes that vaccines are dangerous only seeks out and pays attention to information that supports this belief, while ignoring information that contradicts it.
Sunk cost fallacy: The tendency to continue investing resources into a decision because of the resources that have already been invested. Example: A person continues to work on a project they know is failing because they have already invested so much time and money into it.
Availability heuristic: The tendency to overestimate the likelihood of events that are easily brought to mind. Example: A person who recently heard about a plane crash is more likely to believe that plane crashes are more common than car accidents.
Anchoring bias: The tendency to rely too heavily on the first piece of information encountered when making decisions. Example: A person who is looking to buy a used car is more like
@mahemoff
mahemoff / README.md
Last active January 6, 2023 15:56
MySQL thread-safe multi-database switching in Rails 5

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 / names.txt
Created April 5, 2012 22:24
Reserved username list
###
A meta-compilation of:
https://raw.github.com/gist/1453705/d253733a56632a8d2c29321a75c18b627fa4dda8/reserved_usernames.rb
http://blog.postbit.com/reserved-username-list.html
http://www.quora.com/How-do-sites-prevent-vanity-URLs-from-colliding-with-future-features
(Took out some company-specific terms at the top of the first one (e.g. imulus, stacks), added one term so far - 'edits'.)
NOTE: Does not include profanities. You may or may not want to add a list of those.
(See https://www.google.com/search?q=profanity+list, but don't try to apply regexp's here because Scunthorpe - http://en.wikipedia.org/wiki/Scunthorpe_problem)
@mahemoff
mahemoff / certbot
Last active August 21, 2022 05:27
Certbot for Apache one-liner
# Certbot can be a bit tedious, but it's easy to automate a one-liner non-interactive. Substitute your own domain and email below.
sudo service apache2 stop && sudo certbot --apache -d example.com -m admin@example.com --non-interactive --agree-tos && sudo service apache2 start
@mahemoff
mahemoff / unique_domains.rb
Created July 27, 2022 03:54
Unique domains list from ICANN zones files
#!/usr/bin/env ruby
# Based on https://sive.rs/com - extended it to read from gzipped file, support any domain extension, and output to separate file
require 'hashie'
require 'zlib'
class Parser < Hashie::Dash
property :extension, required: 'true' # e.g. "com" for dotcom database
@mahemoff
mahemoff / tuning.md
Last active May 23, 2022 08:48
Tuning MySQL and Apache

A few general tips to deal with slow performance and hanging. Target system is Ubunty 20.04 and MySQL 8.0, but applicable to other configurations.

Relies on some functions in https://github.com/mahemoff/dotfiles

Slow log

MySQL can log slow queries. Enable it in /etc/mysql/mysql.conf.d/mysqld.cnf.

slow_query_log = 1
@mahemoff
mahemoff / elasticsearch.rake
Created September 19, 2012 19:36 — forked from jarosan/elasticsearch.rake
Elasticsearch reindex task
# 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 / uniq.rb
Created September 12, 2014 00:29
Ensure Sidekiq jobs are unique (if the queue contains several jobs with same class and args, this will delete all but one of them)
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