Problem starting Sidekiq in development

If you ever get this error:

can't link outside actor context

Followed by something like:

    /Library/Ruby/Gems/2.0.0/gems/celluloid-0.16.0/lib/celluloid.rb:176:in `new_link'
    /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/lib/sidekiq/launcher.rb:21:in `initialize'
    /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/lib/sidekiq/cli.rb:81:in `new'
    /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/lib/sidekiq/cli.rb:81:in `run'
    /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/bin/sidekiq:8:in `<top (required)>'
    /Library/Ruby/Gems/2.0.0/bin/sidekiq:23:in `load'
    /Library/Ruby/Gems/2.0.0/bin/sidekiq:23:in `<main>'

It's mostly likely somehow related with either ZenTest or another testing framework. 

In my case, i was adding the ZenTest gem both in the development and test groups of my Gemfile. Moving it away from the development group solved the problem.

AES encryption in Ruby and Decryption in Java

This one is precious, as it took me a long time to figure out. As a side-note, Java apparently only supports 128bit AES.

Here's the Ruby code:

def encrypt(string, pwd)
    salt = OpenSSL::Random.random_bytes(16)

    # prepare cipher for encryption
    e = OpenSSL::Cipher.new('AES-128-CBC')
    e.encrypt

    # next, generate a PKCS5-based string for your key + initialization vector
    key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(pwd, salt, 1024, e.key_len+e.iv_len)
    key = key_iv[0, e.key_len]
    iv  = key_iv[e.key_len, e.iv_len]

    # now set the key and iv for the encrypting cipher
    e.key = key
    e.iv  = iv

    # encrypt the data!
    encrypted = '' << e.update(string) << e.final
    [encrypted, iv, salt].map {|v| ::Base64.strict_encode64(v)}.join("--")   
 end

And the Java part:

public static String decrypt(String encrypted, String pwd) throws Exception {

        String[] parts = encrypted.split("--");
        if (parts.length != 3) return null;

        byte[] encryptedData = Base64.decodeBase64(parts[0]);
        byte[] iv = Base64.decodeBase64(parts[1]);
        byte[] salt = Base64.decodeBase64(parts[2]);

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(pwd.toCharArray(), salt, 1024, 128);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKey aesKey = new SecretKeySpec(tmp.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv));

        byte[] result = cipher.doFinal(encryptedData);
        return new String(result, "UTF-8");
}

Kill process by fuzzy name

Sometimes you want to kill a process but you either don’t know the exact name of the executable file, or you know part of the command arguments. killall is not your friend in those situations. Here’s a quick script to grep on part of a process name or arguments and kill it.


Full script (put it in a path included folder and change it’s permissions to executable +x):

#!/bin/bash
ps aux | grep $1 | grep -v grep | awk '{print $2}' | xargs kill -9

Another awesome video

Take a peak inside the most awesome BASE jumping boogie ever created. The 3rd annual AZ BASE Boogie, hosted by Matt Frohlich of anitgravityBASE and also Apex BASE. Witness a sample of what a huge group of awesome jumpers can get themselves into over a big party weekend in the winter desert. Thanks to Matt and all the local jumpers, all the participants and the guys who contributed footage for this video. Absolute success!!! 

from Vimeo