Skip to content

Instantly share code, notes, and snippets.

@jarthod
jarthod / git_cleanup.rb
Last active August 29, 2015 14:13
Ruby script to cleanup large files from your git repository
#!/usr/bin/env ruby
# Ruby shows you the largest objects in your git repo's pack file & offers you to remove them automatically
# Based on http://stubbisms.wordpress.com/2009/07/10/git-script-to-show-largest-pack-objects-and-trim-your-waist-line/ by Antony Stubbs
# Use this to fetch all branches locally first:
# for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
# git branch --track ${branch##*/} $branch
# done
@jarthod
jarthod / deploy.rb
Created September 20, 2015 08:20
Capistrano local assets compile
# Rewrite precompile tasks to do it locally
Rake::Task["deploy:compile_assets"].clear
task :compile_assets => [:set_rails_env] do
# invoke 'deploy:assets:precompile'
invoke 'deploy:assets:precompile_local'
invoke 'deploy:assets:backup_manifest'
end
namespace :assets do
task :precompile_local do
@jarthod
jarthod / application_helper.rb
Last active February 14, 2016 15:21
Rails helper to render time using client's browser locale
# Use this in place of `time_tag`
# Ex: <%= local_time_tag user.created_at %>
def local_time_tag time, opts = {}
opts[:data] = (opts[:data] || {}).merge format: 'local'
opts[:title] ||= time
time_tag(time, opts) + content_tag(:script, raw(<<-JAVASCRIPT))
var nodes = document.querySelectorAll('time[data-format=local]');
if (nodes.length > 0) {
var elem = nodes[nodes.length - 1];
@jarthod
jarthod / process_semaphore.rb
Created April 17, 2019 08:31
Provides a system wide file lock to ensure no more than X process is running at the same time.
#!/usr/bin/env ruby
# Provides a system wide file lock to ensure no more
# than X process is running at the same time.
# Example:
#
# process_semaphore!({
# prefix: "/tmp/my_process",
# limit: 2
@jarthod
jarthod / mongo-hang-monitoring.rb
Created November 8, 2019 08:31
Ruby script which tries to detect when mongo is up but super slow (for example due to IO issue) and stop it to allow fallback to secondary
#!/usr/bin/env ruby
# Tries to detect when mongo is up but super slow (ex: IO issue)
THRESHOLD = 20_000 # ms
def test_mongo tries: 5
out = `echo -e "db.isMaster()\ndb.getReplicationInfo()" | mongo mongodb://localhost/?socketTimeoutMS=#{THRESHOLD} 2>&1`
res = $?
if res != 0 && !out['Connection refused']
@jarthod
jarthod / set_nf_conntrack_max.sh
Last active October 25, 2020 09:58
Fix synology DS414 nf_conntrack: table full issue
sudo cat /proc/sys/net/netfilter/nf_conntrack_max
echo 500000 | sudo tee /proc/sys/net/netfilter/nf_conntrack_max
sudo cat /proc/sys/net/netfilter/nf_conntrack_max
@jarthod
jarthod / mongo_spy.rb
Created April 16, 2021 21:36
Ruby module (rspec) to count and assert number of mongo queries
# Examples:
#
# expect { code }.to change { finds("users") }.by(3)
# expect { code }.to change { updates("contents") }.by(1)
# expect { code }.not_to change { inserts }
#
# MongoSpy.flush
# ..code..
# expect(MongoSpy.queries).to match(
# "find" => { "users" => 1, "contents" => 1 },
@jarthod
jarthod / vat.rb
Last active April 26, 2022 19:35
EU VAT rates in ruby (2020) + online fetcher to verify/update rates.
module VAT
RATES = {
"AT" => 20.0,
"BE" => 21.0,
"BG" => 20.0,
"CY" => 19.0,
"CZ" => 21.0,
"DE" => 19.0,
"DK" => 25.0,
"EE" => 20.0,
@jarthod
jarthod / RestRequest.inc.php
Created July 7, 2022 19:24
Quickly mute or unmute multiple checks in bulk using this small PHP page made by Eric Bouquerel from Bol d'Air (@Boldairdev)
<?php
class RestRequest
{
protected $url;
protected $verb;
protected $requestBody;
protected $requestLength;
protected $apiKey;
protected $username;
@jarthod
jarthod / website-dump.md
Created August 10, 2022 18:36
How to dump a website using wget

Example with wget:

wget --no-parent -rpk --wait=1 --random-wait https://wbsite.com/page

--no-parent prevents going up in the URLs --wait=1 --random-wait slows down request and add jitter to go easy on the server

If it needs a connected session:

--header="Cookie: session=xxxx"