Sending TCP keepalives in Ruby

When you make a simple TCP connection to a remote server (like telnet), your client won't normally notice when the connection is unexpectly severed on the remote side. E.g. if someone would disconnect a network cable from the server you're connected to, no client would notice. It would simply look like nothing is being sent.

You can detect remote connection loss by configuring your client socket to send TCP keepalive signals after some period of inactivity. If those signals are not acknowledged by the other side, your client will terminat...

Check SSL certificates

Installing SSL certificates usually implies additionally using intermediate certificates. If one of them is missing, some SSL client implementations might fail with failures such as

curl

~ curl -v https://host-to-check
curl: (60) SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

OpenSSL

~ openssl s_client -connect host-to-check:443
...
verify error:num=20:unable to get local issuer certificate
...
v...

grosser/parallel

Utility methods to distribute each or map over multiple threads or processes.

How to repair a corrupt PDF

If you have issues with PDFs, fix them like this: pdftk <corrupted_file>.pdf output <fixed_file>.pdf

Background

I had an issue where an included PDF would not show up in a document created with xelatex. This is the relevant line of LaTeX code:

  \AddToShipoutPicture*{ \includegraphics[width=21cm]{/home/dominik/code/ihero/public/system/stationery/original/stationery.pdf} }

The included PDF is a stationery for invoices which users can upload themselves. It did work until someone updated their stationery with a nearly-iden...

Fix "subprocess installed post-removal script returned error exit status ..." when installing/removing/updating a package with apt

If you get an error like:

subprocess installed post-removal script returned error exit status 78

when installing/removing/updating a package with apt you should check the postinst, postrm, prerm, ... script in /var/lib/dpkg/info/.

For example in my case I had a problem when removing varnish:

Removing varnish (4.0.3-2~trusty) ...
dpkg: error processing package varnish (--remove):
 subprocess installed post-removal script returned error exit status 78
Errors were encountered while processing:
 varnish

So I checked ...

Show or hide a jQuery element given a condition

If you have jQuery code like this:

if (condition) {
  $element.show();
} else {
  $element.hide();
}

... you can shorten this to:

$element.toggle(condition);

Test downstream bandwidth of Internet connection

You want to test your 1GE or 10GE internet uplink? We needed to ensure we have full 10GE to the backbone for a customer project.

Using netcat

To test whether we can achieve the bandwidth internally, you can use netcat and dd like this:

On your first server: nc -v -l 55333 > /dev/null
On your second server: dd if=/dev/zero bs=1024K count=5000 | nc -v $remote_ip 55333

You should see some output like this:

user@xxx:~ % dd if=/dev/zero bs=1024K count=5000 | nc -v removed 55333
Connection to 91.250.95.249 55333 port [...

How to remove properties of ActiveRecord scopes

When dealing with AR scopes, you can remove conditions, order, etc by using the unscope method.

It is available on Rails 4+.


Examples

Consider an exemplary User class as follows. For the examples below, we will use a scope that applies all its constraints.

class User < ActiveRecord::Base
  scope :active, -> { where(locked: false) }
  scope :admins, -> { where(role: 'admin') }
  scope :ordered, -> { order(:name) }
end

users = User.active.admins.ordered

^
SELECT "users".* FROM "users" WHERE "use...

Detect the current jQuery version

You can say this in Javascript:

$.fn.jquery
=> "1.11.1"

Find your Thunderbird passwords

You cannot find your account passwords in the Account Settings – that'd be too easy. Here is where you find them:

Preferences > Security > Passwords > Saved Passwords… > Show Passwords

A case for Redactor

Redactor is yet another WYSIWYG editor. It definitely has its weak points, but I want to point out that it has clear strengths, too.

Pro

  • Simple and beautiful interface.
  • Outstandingly organized source code. Have never seen a JS library that was this structured.
  • Clear, comprehensive and searchable API documentation. Filled with code examples.
  • Easily customizable: specify toolbar buttons, pass various callbacks, etc.
  • Features a collection of great [plugins](ht...

How to disable Rails raising errors on pending migrations in development

Rails 4 introduced raising an error on pending migrations. This is most annoying when you are crafting a migration but need to play with your application to figure out how to do it.

To disable this behavior, just set the corresponding config option to false:

# in config/environments/development.rb

# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = false # was :page_load

Web Fonts Performance // Speaker Deck

Web fonts are great. They are also be really bad for front-end performance because they block rendering. You may have experienced this on a slow cellular network. Staring at a blank page is no fun, especially when the content has already loaded.

This talk explores why browser have placed fonts on the critical path, and how we can work around this while still delivering a good user experience. It also takes a look at what the future will bring to web font performance: preloading hints, the font-display property, and HTTP/2.

Heads up: Ruby implicitly converts a hash to keyword arguments

When a method has keyword arguments, Ruby offers implicit conversion of a Hash argument into keyword arguments. This conversion is performed by calling to_hash on the last argument to that method, before assigning optional arguments. If to_hash returns an instance of Hash, the hash is taken as keyword arguments to that method.

Iss...

Enhanced error messages when hash keys are missing

Hash#fetch is a great way to ensure that a hash key is present. The error message when a key is missing though is rather useless if you don't have immediate access to the object and want to debug why keys are missing, e.g. in the parsed JSON response of an external API. If you'd like a more detailed error message, you can do a Hash#decent_fetch (with the attached code).

some_hash.fetch('missing_key')
# => KeyError: key not found: "missing_key"
 
some_hash.decent_fetch('missing_key')
# => KeyError: Key "missing_key" not found in {"id...

ExceptionNotification gem will only show application backtrace starting on Rails 4

Starting with Rails 4.0, when you get an exception reported via the ExceptionNotification gem, you will only see a very short backtrace with all backtrace lines from gems or ruby libraries missing.

This happens, because the ExceptionNotification gem uses Rails' default backtrace cleaner. To get a full backtrace in exception emails, you can remove the comment from this line in config/initializers/backtrace_silencers.rb:

Rails.backtrace_cleaner.remove_silencers!

Note that this will break the "Application Trace" functionality o...

OR-ing query conditions on Rails 4 and 3.2

Rails 5 will introduce ActiveRecord::Relation#or. On Rails 4 and 3.2 you can use the activerecord_any_of gem which seems to be free of ugly hacks and nicely does what you need.

Use it like this:

User.where.any_of(name: 'Alice', gender: 'female')

^
SELECT "users".* FROM "users" WHERE (("users"."name" = 'Alice' OR "users"."gender" = 'female'))

To group conditions, wrap them in hashes:

User.where.any_of({ name: 'Alice', gender: 'female' }, { name: 'Bob' }, { name: 'Charl...

pgAdmin has a "graphical EXPLAIN" feature

When working with PostgreSQL, you can use pgAdmin as a GUI.
While you can do most things just like on an SQL console, you can use it to display EXPLAIN results in a more human-readable way.


(image from the Postgres manual)

  1. Open up pgAdmin, connect to your server
  2. Pick a database from the left pane
  3. Click the "SQL" icon in the toolbar, or press Ctrl+E to open the query tool.
  4. Paste any queries that you'd like to explain.
  5. Go to "Query" → "Explain analyze", or ...

Rarely say yes to feature requests

A fantastic guide for a dilemma facing any web-based product.

Here’s a simple set of Yes/No questions that you can quickly answer before you add another item to your product roadmap.

Saying yes to a feature request – whether it’s a to an existing customer, a product enquiry, a teammate, or a manager – is immediately rewarding. It’s an unspoken transaction where you barter long term product focus in exchange for short term satisfaction. Buying short term joy for the cost of long term pain is the human condition.

  1. Does it fit your ...

Terminal escape sequences – the new XSS for Linux sysadmins

Article shows how to make a script that fakes one kind of content when printed with cat, but uses different code when executed:

$ printf '#!/bin/bash\necho doing something evil!\nexit\n\033[2Aecho doing something very nice!\n' > backdoor.sh

$ chmod +x backdoor.sh

$ cat backdoor.sh
#!/bin/bash
echo doing something very nice!

$ ./backdoor.sh
doing something evil!

Regain unused disk space from OpenStack instances

This is how you regain disk space from OpenStack instances if you are using kvm and qcow.

If your instance used up all configured disk space once the disk file remains big. You can end up in a situation where for example the instance use only 20GB disk space but the disk file on the server has 100GB (or even more).

To resize the disk file do the following:

  1. Check storage on the instance:

    vm $ df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vda1        99G   19G   75G  21% /
    udev            2.0G   12K  2.0...
    

httpclient: A Ruby HTTP client for serious business

While debugging an intricate issue with failed HTTP requests I have come to appreciate the more advanced features of the httpclient Rubygem.

The gem is much more than a lightweight wrapper around Ruby's net/http. In particular:

  • A single HTTPClient instance can re-use persistent connections across threads in a thread-safe way.
  • Has a custom and configurable SSL certificate store (which you probably want to disable by default...

Error installing gem with native extension (collect2: error: ld returned 1 exit status)

If you have problems installing a gem and get a error collect2: error: ld returned 1 exit status it's due to missing development headers of a library (ld is the linker).

For example, with this output:

$ gem install json
Building native extensions.  This could take a while...
ERROR:  Error installing json:
       ERROR: Failed to build gem native extension.

   /home/foobar/.rvm/rubies/ruby-2.2.3/bin/ruby -r ./siteconf20150915-3539-1i9layj.rb extconf.rb
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling generator.c...