Bunny Consumer

No, not something that eats bunnies. I wanted to know if I could easily run a RabbitMQ consumer that just sat there listening. It seems incorrect to have a loop sat there just doing nothing all the time but I guess it the connection is open then it should just sit there, if not it can stop and have forty winks.


source 'https://rubygems.org'

gem 'bunny'
gem 'pry'
gem 'pry-coolline'


require 'rubygems'
# ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', __dir__)
require 'bundler/setup'

conn = Bunny.new
# open a channel
ch = conn.create_channel

# declare a queue
q = ch.queue('test1')

q.subscribe do |_, _, payload|
  Thread.new do
    puts "payload #{payload}"
    puts "finished sleeping"

while conn.status == :open do
  # nothing is happening here

As expected, when an event occurs the subscribe block is called and the payload is send to STDOUT. Wait five seconds and finish. Had I not added Thread.new consumer would have waited 5 seconds and order is not quaranteed.

Robs-iMac:consumer roblacey$ ruby gimmick.rb 
payload 2
payload 1
payload 4
payload 3
payload 5
finished sleeping
finished sleeping
finished sleeping
finished sleeping
finished sleeping

Interesting to note that when using manual_ack. Events are not automatically re-read from the queue until a new connection is made if the job is not ack’ed.

q.subscribe(manual_ack: true) do |delivery_info, _, payload|
  Thread.new do
    puts "payload #{payload} - Redelivered #{delivery_info.redelivered?}\n"
    puts "finished sleeping #{payload}\n"
    puts payload.class
    ch.acknowledge(delivery_info.delivery_tag, false) unless payload == '1' && !delivery_info.redelivered?

while conn.status == :open do
  # nothing is happening here

Guard / RSpec error messages

Some warnings and errors appears today, or at least I only just noticed them.

  1. Firstly I forget to load guard without bundle exec and this makes me a bad person.
  2. Apparently I might have more than one version of rspec-expectations and rb-notify installed and I should probably clean them up.

So this might be the case, oh actually. I see the problem. I have a project that has two branches, master and upgrade/rails51. I’m using RVM but each branch has a different .ruby-gemset because of course Rails 5.1 is an upgrade and will have multiple dependencies that are all upgraded too. I must remember to switch RVM environments when flipping branches.

Robs-iMac:core roblacey$ guard
Warning: you have a Gemfile, but you're not using bundler or RUBYGEMS_GEMDEPS
09:36:26 - INFO - Guard here! It looks like your project has a Gemfile, yet you are running
> [#] `guard` outside of Bundler. If this is your intent, feel free to ignore this
> [#] message. Otherwise, consider using `bundle exec guard` to ensure your
> [#] dependencies are loaded correctly.
> [#] (You can run `guard` with --no-bundler-warning to get rid of this message.)
WARN: Unresolved specs during Gem::Specification.reset:
      rb-inotify (~> 0.9, >= 0.9.7)
      rspec-expectations (~> 3.8.0)
WARN: Clearing out unresolved specs.

Converting PDFs to Greyscale

Needed to remove the colour from PDFs that were too big to print our our crappy printer.

find . -iname '*.pdf' -exec sh -c 'gs  -sOutputFile=grey/{} -sDEVICE=pdfwrite  -sColorConversionStrategy=Gray  -dProcessColorModel=/DeviceGray  -dCompatibilityLevel=1.4  -dNOPAUSE  -dBATCH {}' \;

Greyscale will print much better, although the colour versions look amazing.

dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib

I keep getting this, first in Ruby, so had to re-install Ruby 2.3.3. Now node.

Robs-iMac:something roblacey$ node -v
dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib
  Referenced from: /usr/local/bin/node
  Reason: image not found
Abort trap: 6

I must have installed something that played havoc with this lib.

Robs-iMac:something roblacey$ brew reinstall node
==> Reinstalling node 
==> Downloading https://homebrew.bintray.com/bottles/node-11.6.0.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring node-11.6.0.mojave.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
==> Summary
   /usr/local/Cellar/node/11.6.0: 3,938 files, 46.6MB
Robs-iMac:something roblacey$ node -v

Elixir Day #Something (because I started again) - Collection Types

So what are the Collection Types?

# List
[ 1, 2, 3 ]
# Tuple
{ 1, 2, 3 }
# Keyword List
[ one: 1, two: 2, three: 3 ]
# Map
{ one: 1, two: 2, three: 3 }

My first question was why Keyword List and Map. They have their differences.


Speed of read/write access and Keyword Lists ordered, Maps not.

RVM and GPG 2.1.17 bug

Oh how annoying, just trying to deploy a quick fix for a client.

DEBUG[f3c03d7b] 	Found old RVM 1.29.4 - updating.
DEBUG[f3c03d7b] 	Downloading https://get.rvm.io
DEBUG[f3c03d7b] 	[32mDownloading https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer.asc[0m
DEBUG[f3c03d7b] 	[0m
DEBUG[f3c03d7b] 	[32mVerifying /home/rails/.rvm/archives/rvm-installer.asc[0m
DEBUG[f3c03d7b] 	[0m
DEBUG[f3c03d7b] 	gpg: 
DEBUG[f3c03d7b] 	Signature made Sun 30 Dec 2018 10:44:46 UTC using RSA key ID 39499BDB
DEBUG[f3c03d7b] 	gpg: 
DEBUG[f3c03d7b] 	Can't check signature: public key not found
DEBUG[f3c03d7b] 	[31mWarning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found. Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).
DEBUG[f3c03d7b] 	GPG signature verification failed for '/home/rails/.rvm/archives/rvm-installer' - 'https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer.asc'! Try to install GPG v2 and then fetch the public key:
DEBUG[f3c03d7b] 	    gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
DEBUG[f3c03d7b] 	or if it fails:
DEBUG[f3c03d7b] 	    command curl -sSL https://rvm.io/mpapis.asc | gpg --import -
DEBUG[f3c03d7b] 	the key can be compared with:
DEBUG[f3c03d7b] 	    https://rvm.io/mpapis.asc
DEBUG[f3c03d7b] 	    https://keybase.io/mpapis
DEBUG[f3c03d7b] 	NOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgrade or upgrade to newer version (if available) or use the second method described above.
DEBUG[f3c03d7b] 	[0m
DEBUG[f3c03d7b] 	[0m
DEBUG[f3c03d7b] 	/home/rails/.rvm/scripts/functions/cli: line 238: return: _ret: numeric argument required

Seems there is a bug in GPG when fetching remote keys. Solution for this Ubuntu server was to just install gnupg2 and install the key again.

rails@fashion1-002:~$ sudo apt-get install gnupg2
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  gnupg-agent libassuan0 libksba8 libpth20 pinentry-gtk2
Suggested packages:
  gnupg-doc xloadimage pinentry-doc
The following NEW packages will be installed
  gnupg-agent gnupg2 libassuan0 libksba8 libpth20 pinentry-gtk2
0 to upgrade, 6 to newly install, 0 to remove and 177 not to upgrade.
Need to get 1,622 kB of archives.
After this operation, 4,115 kB of additional disk space will be used.
Do you want to continue [Y/n]? Y
Get:1 http://archive.ubuntu.com/ubuntu/ precise/main libassuan0 i386 2.0.2-1ubuntu1 [34.2 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ precise-updates/main libksba8 i386 1.2.0-2ubuntu0.2 [107 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ precise/main libpth20 i386 2.0.7-16ubuntu3 [50.1 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ precise/main pinentry-gtk2 i386 0.8.1-1ubuntu1 [51.4 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ precise-updates/main gnupg-agent i386 2.0.17-2ubuntu2.12.04.6 [298 kB]
Get:6 http://archive.ubuntu.com/ubuntu/ precise-updates/main gnupg2 i386 2.0.17-2ubuntu2.12.04.6 [1,082 kB]
Fetched 1,622 kB in 0s (9,323 kB/s)
Selecting previously unselected package libassuan0.
(Reading database ... 101031 files and directories currently installed.)
Unpacking libassuan0 (from .../libassuan0_2.0.2-1ubuntu1_i386.deb) ...
Selecting previously unselected package libksba8.
Unpacking libksba8 (from .../libksba8_1.2.0-2ubuntu0.2_i386.deb) ...
Selecting previously unselected package libpth20.
Unpacking libpth20 (from .../libpth20_2.0.7-16ubuntu3_i386.deb) ...
Selecting previously unselected package pinentry-gtk2.
Unpacking pinentry-gtk2 (from .../pinentry-gtk2_0.8.1-1ubuntu1_i386.deb) ...
Selecting previously unselected package gnupg-agent.
Unpacking gnupg-agent (from .../gnupg-agent_2.0.17-2ubuntu2.12.04.6_i386.deb) ...
Selecting previously unselected package gnupg2.
Unpacking gnupg2 (from .../gnupg2_2.0.17-2ubuntu2.12.04.6_i386.deb) ...
Processing triggers for man-db ...
Setting up libassuan0 (2.0.2-1ubuntu1) ...
Setting up libksba8 (1.2.0-2ubuntu0.2) ...
Setting up libpth20 (2.0.7-16ubuntu3) ...
Setting up pinentry-gtk2 (0.8.1-1ubuntu1) ...
Setting up gnupg-agent (2.0.17-2ubuntu2.12.04.6) ...
Setting up gnupg2 (2.0.17-2ubuntu2.12.04.6) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
rails@fashion1-002:~$ gpg2 --recv-keys 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: requesting key 39499BDB from hkp server keys.gnupg.net
gpg: key 39499BDB: public key "Piotr Kuczynski <piotr.kuczynski@gmail.com>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

ActiveSupport.halt_callback_chains_on_return_false = false

Finally getting to upgrading to Rails 5.1 and there are lots of cases where we use before_* callbacks to block behaviour, most often in before_destroy to prevent a record being deleted when some conditions are met / not met. Previously Rails 5.1 allowed you to just return false in a callback to halt the callback chain. Now you must use throw(:abort).

The behaviour change was turned off by default in Rails 5.0

ActiveSupport.halt_callback_chains_on_return_false = true

So you can implement the change sooner by changing that setting in an initializer.

class Thing < ApplicationRecord
  before_create do

  before_destroy do

I thought initially this would result in having to cope with exceptions raised from throwing that abort rather than say checking if object.destroy returned false. However, it seems it’s just a clarification of behaviour to avoid accidental blocking.

2.3.0 :006 > Thing.create.persisted?
   (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
 => false 
2.3.0 :007 > reload!
 => true 
2.3.0 :008 > Thing.create.persisted?
   (0.1ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `things` (`created_at`, `updated_at`) VALUES ('2019-01-17 08:38:24', '2019-01-17 08:38:24')
   (0.5ms)  COMMIT
 => true 
2.3.0 :009 > Thing.last.destroy
  Thing Load (0.3ms)  SELECT  `things`.* FROM `things` ORDER BY `things`.`id` DESC LIMIT 1
   (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
 => false 
2.3.0 :010 > reload!
 => true 
2.3.0 :011 > Thing.create.persisted?
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `things` (`created_at`, `updated_at`) VALUES ('2019-01-17 08:39:29', '2019-01-17 08:39:29')
   (21.0ms)  COMMIT
 => true 
2.3.0 :010 > reload!
2.3.0 :014 > Thing.last.destroy
  Thing Load (0.1ms)  SELECT  `things`.* FROM `things` ORDER BY `things`.`id` DESC LIMIT 1
   (0.1ms)  BEGIN
  SQL (0.2ms)  DELETE FROM `things` WHERE `things`.`id` = 2
   (0.6ms)  COMMIT
 => #<Thing id: 2, created_at: "2019-01-17 08:39:29", updated_at: "2019-01-17 08:39:29"> 

Hopefully this won’t be too painful an exercise.

Elixir - Day 1

Well I installed it, and that’s good enough for me for today.

Erlang/OTP 21 [erts-10.1.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.7.4) - press Ctrl+C to exit (type h() ENTER for help)

At least my specs pass

When in doubt always write just any test. If it passes then you’re good to do into production with your fully tested to hell code.

require 'spec_helper'

RSpec.describe 'so much specs' do 
  it 'represents the level of care I put into my code' do
    expect { some bullshit }.to raise_error

More ImageMagick examples

I needed to quickly just crop an image to a specific width and then pad the image into a square with a white background.

convert logo.jpg -gravity west -extent 480x logo-cropped.jpg
convert -background white -gravity center logo-cropped.jpg -resize 470x470 -extent 470x470 result.jpg