Exclusive cronjobs with flock and whenever
I had a very frequent cronjob that in rare cases could be relatively slow. To avoid multiple instances of this cronjob running in parallel, I decided to use flock
to ensure that only one instance could run at a time.
flock
works by acquiring a lock to a file, and if it can do so running a command. In order not to wait but simply give up when the file is locked, you can add -n
:
flock /tmp/my.task.lock -n -c "bin/my-long-running-job"
Using whenever, and since this was a rake task, the follo...
MutationObserver
MutationObserver
provides developers a way to react to changes in a DOM. Any: insertion, deletion, attribute change – anything.
Quickstart: https://davidwalsh.name/mutationobserver-api
Relevant caniuse.com search: http://caniuse.com/#feat=mutationobserver
ne, the nice editor
Command line text editor with syntax highlighting, menus (F1
), etc.
This might be a worthy replacement for nano
if you don't want to use vim
.
How to fix: RubyMine occasionally no longer accepts keyboard input
From time to time, RubyMine suddenly did not accept any keyboard input and felt crashed, while mouse interaction was still possible. This apparently happens to all IntelliJ IDEs, especially on Ubuntu 14.04.
I've managed to fix it by having a shell script that exports XMODIFIERS=""
when launching RubyMine, like this:
#!/bin/sh
XMODIFIERS= /home/arne/rubymine/bin/rubymine.sh
It has been working reliably for me ever since, at least until RubyMine 8.
An alternate solution suggested on the [Jetbrains issue tracker](https://youtrack....
CSS Fontstack: An overview of web/web safe font support
Web safe fonts are fonts that are pre-installed by many operating systems. While not all systems have the same fonts installed, you can use a web safe font stack to choose several fonts that look similar, and are installed on the various systems that you want to support. If you want to use fonts other than ones pre-installed, as of CSS3, you can use Web Fonts.
If you need to install any of the widely-supported fonts listed there, you'll probably find them at www.fontpalace.com.
Case Study: Analyzing Web Font Performance
Table of contents of the linked article:
What are Web Fonts?
- Advantages of Web Fonts
- Disadvantages of Web Fonts
- Fallback Fonts
- CSS3 @font Declaration Example
- Fallback Font Example
- Render Blocking and Critical Rendering Path
- FOIT
Optimizing Web Font Delivery Further
- Prioritize Based On Browser Support
- Choose Only Styles You Need
- Character Sets
- Host Fonts Locally or Prefetch
- Store in LocalStorage with Base64 Encoding
- Another Method
Web Font Pe...
IFrame Resizer
A JS library that allows you to embed an iframe that automatically shrinks or expands to match its content.
(Untried.)
get haproxy stats/informations via socat
You can configure a stat socket for haproxy in the global section of the configuration file:
global
daemon
maxconn 999
user foobar
stats socket /var/run/haproxy.stat # this is the line you want to configure
You need socat
to query data from this socket.
After installing socat and reconfiguring haproxy you can use this socket to query data from it:
-
show informations like haproxy version, PID, current connections, session rates, tasks, etc..
echo "show info" | socat unix-connect:/var/run/haproxy.stat stdio
...
what to do if nova's iptables rules are missing
After restarting an OpenStack host you may encouter problems with missing iptables rules (we're on an quite old release of OpenStack currently. Maybe this is fixed in newer releases). The nova chains appear in the iptables -L
output but they're empty. NAT is working fine. The reason is, that the NAT chains are configured by nova-network
while the filter rules are managed by nova-compute
. I didn't manage to find the cause of this behaivour yet, but I think it has something to do with the start order of the nova services. (When `nova-net...
ping with timestamps
Use this snippet by Achu from Ask Ubuntu:
ping hostname.tld | while read pong; do echo "$(date): $pong"; done
This gives you lines like:
Wed Nov 4 10:32:31 CET 2015: 64 bytes from 1.2.3.4: icmp_seq=298 ttl=61 time=0.673 ms
Wed Nov 4 10:32:32 CET 2015: 64 bytes from 1.2.3.4: icmp_seq=299 ttl=61 time=0.616 ms
Wed Nov 4 10:32:33 CET 2015: 64 bytes from 1.2.3.4: icmp_seq=300 ttl=61 time=1.04 ms
How to deal with "invalid %-encoding" error in application for malformed uri
Lead by a discussion of this issue, I built in a middleware which answers those requests with [400] bad request
rather than raising an ArgumentError
.
I put it into app/util
and configured application.rb
like that:
# catches 'invalid %-encoding' error
require "#{Rails.root}/app/util/exception_app"
config.middleware.insert_before Rack::Runtime, ExceptionApp::Middleware
Note: Rails 4.2+ raises an ActionController::BadRequest
error instead of an ArgumentError
.
Gemspecs must not list the same gem as both runtime and development dependency
When you're developing a gem, never list the same dependency as both runtime and development dependency in your .gemspec
.
So don't do this:
spec.add_dependency 'activesupport'
spec.add_development_dependency 'activesupport', '~> 2.3'
If you do this, your gemspec will not validate and modern versions of Bundler will silently ignore it. This leads to errors like:
Could not find your-gem-0.1.2 in any of the sources
What to do instead
If you want to freeze a different version of a dependency for your t...
Reverse-proxying web applications with nginx
While you can use Apache as a reverse proxy, it tries to be too smart. Try nginx instead, it's much simpler to set up.
After struggling with Apache for quite a while, since I simply could not make it pass through the Digest Authentication of my target host (that I proxied to), I switched to nginx. Here is what I did.
-
Have nginx
sudo apt-get install nginx
-
Define your nginx config, e.g. at
/etc/nginx/conf.d/reverse-proxy.conf
:server { listen 127.0.0.1; location /...
Ruby: How to update all values of a Hash
There are many solutions, but a very concise one is this:
hash.merge!(hash) do |key, old_value, new_value|
# Return something
end
The block of merge!
will be called whenever a key in the argument hash already exists in the base hash. Since hash
is updated with itself, each key will conflict and thus allow you to modify the value of each key to whatever you like (by returning old_value
you'd get the behavior of Rails' reverse_merge!
, by re...
Latency Numbers Every Programmer Should Know
A list of common computer I/O actions and how long they take.
Visual comparison chart: http://i.imgur.com/k0t1e.png
PostgreSQL: Expanded display and other command line features
One useful postgres command I stumbled upon recently was \x
. It gives you an expanded display which allows you to actually read the results of your select * from
queries. The link below describes a few more useful techniques and commands.
AngularJS: How to force Content-Type on GET and DELETE requests
While you usually do not need a Content-Type
on GET request (which have a blank body), an external API may still force you to send one.
Angular's $http
service will strip that header when the request data (body) is blank. [1] This is possibly a misconception of RFC2616.
Here is how to send GET requests with a Content-Type
header in Angular.
Example
Consider this request:
$http({ me...
Shrine - A file upload toolkit
Now that CarrierWave is no longer maintained, Shrine might be worth a look.
Thunderbird: How to compose an HTML e-mail (when plain-text messages are your default)
Nobody needs HTML e-mails. However, you occasionally might have to write an HTML message for some weird reason. Here is how:
- Hold the Shift-Key while clicking on "Write", "Reply", "Reply All", or "Forward".
That's it. :)
Designing a landing page that sells
A great walkthrough through several design iteration of the same landing page.
Remove Rubygems deprecation warnings
Rubygems can produce lots of deprecation warnings, but sometimes, you cannot fix them. To have a tidy terminal with output that matters, add this to the top of your Gemfile
and enjoy silence:
Deprecate.skip = true if defined?(Deprecate.skip)
Gem::Deprecate.skip = true if defined?(Gem::Deprecate.skip)
# all gems go here ...
Count number of existing objects in Ruby
Sometimes you want to know exactly how many objects exist within your running Ruby process. Here is how:
stats = {}
ObjectSpace.each_object {|o| stats[o.class].nil? ? stats[o.class] = 0 : stats[o.class] += 1 }; stats
=> {String=>192038, Array=>67690, Time=>2667, Gem::Specification=>2665, Regexp=>491, Gem::Requirement=>16323, Gem::StubSpecification=>2665, ...}
Maybe you want to sort it like this:
stats.sort_by {|k,v| v }
Configure RSpec to raise an error when stubbing a non-existing method
You can configure RSpec 3.3+ to raise an error when attempting to stub or mock a non-existing method. We strongly recommend to do this as non-verified stubs are a footgun.
You can enable this behavior by adding the following to your spec_helper.rb
:
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
This will also replace stub_existing
from our rspec_candy.
Ruby will get a safe navigation operator: '.?'
This is basically Ruby-native syntax for andand
.