Skip to content

Instantly share code, notes, and snippets.

View reidmorrison's full-sized avatar

Reid Morrison reidmorrison

View GitHub Profile
@reidmorrison
reidmorrison / h265_reencode.rb
Created September 20, 2019 18:29
Re-Encode local video files using the H.265 compression method
#
# Re-encode video files with the more efficient h.265 file format.
#
# The original file is appended with the extension `.original` once successfully converted.
# The new file has the .MOV extension.
#
require 'pathname'
EXTENSIONS = "m2ts,flv,mov,wmv,avi,mp4,mpg,m4a,3gp,3g2,mj2"
@reidmorrison
reidmorrison / sftp.rb
Last active February 9, 2017 20:25
Uploading files via SFTP
require 'pty'
require 'expect'
require 'semantic_logger'
# Sftp that works on both Ruby and JRuby.
#
# Example Usage:
# Sftp.upload(
# user: 'friend',
# password: 'secure',
@reidmorrison
reidmorrison / autoload_patch.rb
Created August 14, 2015 13:56
JRuby startup under load causes invalid LoadError - no such file to load
# Fixes "LoadError" "no such file to load" on JRuby 1.7
# Place at the top of application.rb
# Override Kernel#autoload and Module#autoload to #require when Web Server or Worker
# so that classes are all loaded in highly concurrent environments
# Also forces the code to be loaded up front instead of on demand in production
begin
# Since Rails has not yet been loaded we have to use the env vars
rails_env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
if (['production', 'release', 'hotfix'].include?(rails_env))
@reidmorrison
reidmorrison / my_app
Created January 27, 2015 18:49
Puma Redhat/CentOS startup shell script
#!/bin/bash
# puma - Generic Red Hat Startup script for Puma
# chkconfig: 35 95 05
# description: Generic Puma Startup Script
# processname: puma
# config: /etc/'filename'.conf
# pidfile: /var/run/puma/'filename'.pid
@reidmorrison
reidmorrison / active_record_connection_adapters_jdbc_adapter.rb
Created November 13, 2014 20:06
Return a single row at a time from ActiveRecord without having to load them all into memory first
if defined?(Java)
class ActiveRecord::ConnectionAdapters::JdbcAdapter
# Returns each row as a hash
# Example:
# Inquiry.connection.exec_query_each('select * from inquiries') do |row|
# ap row
# end
def exec_query_each(sql, name = 'SQL', binds = [], &block)
log(sql, name) { @connection.exec_query_each(sql, binds, &block) }
end
@reidmorrison
reidmorrison / upsert.rb
Last active August 29, 2016 16:40
ActiveRecord upsert implementation
# Perform an upsert.
#
# Insert a new document using the supplied data hash
# If the insert fails due to a duplicate record then the `update` values
# are applied.
#
# Example:
#
# # Values to insert
# insert = {
@reidmorrison
reidmorrison / gist:e5e6b0bf01d6837624d4
Created June 29, 2014 16:59
Active Record V4 connection pool performance patch
require 'thread_safe'
require 'semantic_logger'
require 'gene_pool'
# Require original code since it contains more than just this class
require 'active_record/connection_adapters/abstract/connection_pool'
module ActiveRecord
# Not Used. Connection are no longer timed out
# A warning is logged when the connection time is exceeded
class ConnectionTimeoutError < ConnectionNotEstablished
@reidmorrison
reidmorrison / redis_connection_pool.rb
Last active August 29, 2015 14:01
Implement a connection pool for Redis
# Implement a Redis Connection Pool
#
# Convenience methods
# Instead of having to write:
# redis_pool.perform {|redis| redis.get('key')}
# The following can be used
# redis_pool.get('key')
#
# This applies to all Redis methods except quit. RedisPool#quit calls quit
# on all instances of Redis already in the pool
@reidmorrison
reidmorrison / collection.rb
Last active September 28, 2015 21:19
Mongo Reconnect logic for Connection Failures. Deprecated, moved to: https://github.com/reidmorrison/mongo_ha
#
# NOTE: This gist is now deprecated and has been moved into a gem: https://github.com/reidmorrison/mongo_ha
#
require 'mongo/collection'
module Mongo
class Collection
alias_method :find_one_original, :find_one
def find_one(*args)