Setting the process name in Ruby

When running a ruby application by default the process is shown like 1234 ruby ruby test.rb when running ps -o pid,comm,args on a linux box. The command, comm, is the process name according to the application, and args are the arguments passed to the process on startup. Since the arguments are passed as a string this means that a programm can theoretically modify them to change what is displayed. In ruby this means that one can change the value of the $0 variable to change what is displayed by programms like top for example, this does however not change the actuall process name but just changes what is stored in the process agruments. A problem with this arises when the process is forked via a tool like upstart, which means that its arguments like any other process memory is copy-on-write, and therefor a modification of the arguments will cause a copy and modification, instead of changing the underlying string, this in turn means that overwriting $0 will not change the output in top or other tools.
With ruby 2.1 a new API appears in ruby setproctitle which is available to change this, unfortunatly this does not work everywhere and is also not available pre 2.1. Luckily linux offers an API to set the process title via prctl which can easily be leveraged via FFI to change the process title to whatever is needed (as long as it is below 15 characters).

Now 1234 mytitle ruby test.rb can be seen when running ps -o pid,comm,args when calling ProcessCtrl.set_process_name "mytitle" from the test.rb script.

Detect long paths incompatible with OneDrive for Mac


Screen Shot 2015-09-01 at 21.35.28

As a user of OneDrive on MacOS I ran into an issue that OneDrive would hang at the processing files or scanning files stage. After some searching I found that this can be related to having paths with more than 255 characters in the name but how to find those? Well some small bash magic did the trick for me.

cd ~/OneDrive && find `pwd` -name '*' | grep '^.\{255\}'

This will list all the paths longer than 255 characters for the OneDrive directory located in home.

Canned v0.3.3

It has been some time since I wrote about canned and a lot has happend since then. With the current release canned now supports all features on Windows as well as Unix, which has been limited before due to the Windows limitation of certain characters in the filename, which caused complex get parameters to create filenames that are not valid on Windows.
With the new version canned now fully supports multiple response bodies in a single file, allowing you to match on headers, parameters, and body of the request, as well es return different status codes for each of them. This allows now to have a single file bundling up all the post responses for a certain resource, emulating the rejection of certain parameters, or headers while returning the correct status code for the operation.

For more details see the changelog thanks to everybody who contributed to the recent releases, reporting and fixing bugs as well as adding features like proper SOAP XML support to emulate ever more complex APIs.

Moving from Tumblr to WordPress

Recently I moved this blog from Tumblr to WordPress, as I wanted more control over the environment. My setup now is a hosted WordPress instance on heroku, with really nothing special, using the current PHP stack with Apache2 as my server, and ClearDB to provide me a MySQL instance to store data in. Since this basic setup will provide rather dreadful performance, especially on a single free Dyno, I use Amazons CloudFront as my CDN. All this gets me to a quite fast experience. The setup was quick and painless, but migration of the content proved harder than expected. All there is to do to make this work on heroku is using environment variables to get the database setup in PHP.

Importing Tumblr into WordPress

WordPress offers an importer for Tumblr, sadly this importer seems to strip HTML from Tumblr posts which meant that all my carefully embedded content was not imported. Looking around there are multiple ways to solve this, the quickest being Tumblr2Wordpress which allows to create a WordPress compatible XML file from all the Tumblr posts, with the content being HTML. Importing is as simple as hitting import on the new WordPress blog.

One thing which constantly annoys me on the more “casual” web is that people break links all the time, and since I didn’t want to be one of those people I wanted to make sure that all links to my content still work after the migration. Also since I use Disqus for my commenting, and Disqus uses the URL to determine the correct comment stream (afaik) I wanted to make sure this works correctly. Tumblr uses a somewhat weird way to reference posts its URL format is something like: http://tumblr.com/posts/SOME_ID/THE_SLUGIFIED_POST_TITLE what is funny about this is that the last part of the URL does not matter at all, it is just the ID Tumblr cares about, and this ID is also carried over by the import and stored in the post_name attribute for the post. So to faking the first part of the Tumblr URL in WordPress is easy, just make the permalink structure: /post/%postname%. Now if everybody would just link to the ID this would work great, but sadly most of the time the title is going to be included, even though nobody cares about it. The solution to this is simple, just don’t make WordPress care about it either, but display it if needed.

Added to function.php of the theme that is in use, this little snippet will provide a new tag for the permalinks to attach and strip of the slugified title to the to the permalinks, depending on if the imported post used the Tumblr way of URL or if it is a new WordPress post, just don’t name your posts 1234… /post/%postname%/%tumblrslug% will now give you the structure you need.

Easy management with Rake

For the longest time I have been a ruby guy and I just like to use rake to automate almost everything and do the needed command remembering for me. So to make sure I can keep managing my blog I ended up creating a Rakefile with some function to manage my remote instance, and I use dotenv to manage my local variables in the same way as heroku does remotely, through environment variables. For anybody interested, this is what this looks like

And that’s it, the blog is migrated, all the links should still work and I can easily manage the needed tasks, like backup, getting into the database, running a local server all through Rake.

Using Couchbase Lite from RubyMotion

CouchbaseLite has been released for sometime now, so I thought it is time to
give an update for using CouchbaseLite from RubyMotion.

When I ported ToDoLite-iOS to
RubyMotion originally there where some bumps in the road, but it worked over
all. There were for example some problems with RubyMotion not handling lambdas
the way CouchbaseLite needs them, but this has been resolved since then. If you
don’t know what I mean by that, you can be happy and forget all about it, or
read up on
it
.

So what do I need now to get going with CouchbaseLite and iOS?

By now it is possible to go 100% ruby for a RubyMotion project using
CouchbaseLite, which is great. CouchbaseLite has been publicly released, is out
of beta and has already received much love in terms of patches to make it work
even more reliably cross all platforms. In case of RubyMotion this made things
much easier, and the process is by now:

  • Add couchbase-lite via cocoapods
  • Tell rubymotion where to find the header files
  • Use it!

Installing CouchbaseLite via Cocoapods

Cocoapods is an awesome package manager for iOS and
MacOS projects, and it integrates really well with RubyMotion. All there is todo
is add cocoapods and motion-cocoapods to your gemfile

Now you can install any cocoapods by adding them to the Rakefile and running
“bundle exec rake pod:install”

Important side not, make sure to include

as the headers are not going to be found otherwise.

Using Couchbase Lite from RubyMotion

You can now use CouchbaseLite like you would any other Obj-C library from
rubymotion, for example to define a view which grabs all the “lists” in the
database you can write this

For more details checkout the RubyMotion Sample project on
Github
.

Plain text to-do list and Textmate

For my personal to-do list I’m a big fan of todo.txt and have been using it for quite a while, recently I returned to Textmate to manage the list so I decided to revamp my todo.txt bundle a little to now include syntax highlighting for todos with a date, as well as a command to include the date in a task.
It’s also downloadable as a bundle now, if you are one of the 3 people still using Textmate 1.5 and todo.txt check it out ;).

Couchbase views and JSHint

I like to run JSHint to check my work when creating JavaScript for Couchbase views, but by default it shows a warning ‘Missing name in function declaration’. Luckily since JSHint 1.0.0 you can ignore any warning by number so

Gets rid of this.

Debugging Erlang NIF with LLDB

Earlier today I ran across a segfault while trying something with
cberl so I figured it’s time to startup
gdb and see what is going on, but how to actually do that? Luckily
I found a post on the erlang programming
list

with the exact same problem. Since I’m on a Mac I wanted to use
lldb instead so a quick modification to the erl
startup script did the trick.

Now ./rebar skip_deps=true eunit runs erland via lldb and ready to
debug.

Couchbase Lite on the Raspberry Pi using JRuby

After having my Raspberry Pi sitting quietly in the corner for quite some time
I recently heard Dave Starling talk about
running Couchbase Lite on it and just had to
try it out. After all having a small embedded database sounds perfect to the
Pi and I can think of a couple fun projects with it, from dashboards to home
automation, to the intelligent fridge ;). Also with DevoxxUK
coming up, this sounded like a perfect talk subject for a quick 15min session.

So what is Couchbase Lite?

Couchbase Lite is a NoSQL database, for mobile and embedded devices. Similar to
using SQLite you add a library to your project and now you have a database
available to you, but unlike SQLite it provides additional syncing
capabilities, and stores JSON documents instead of Rows. So what does that give
you? Basically it means that you can use JSON APIs directly and store the
results in the database, which makes building such apps much easier. Also
having syncing done for you means that you can just work with your data locally
like you always were, but have it available on every device connected, be it
Android / iOS / Windows Phone and of course the Raspberry Pi, even if you are
offline. This works via the so called
sync_gateway which takes care of
the nitty-gritty details of sync and managing conflicts as well as the
connection to Couchbase to keep the data available on
your severs.

Running
Couchbase Lite + JRuby on MacOS
from Philipp Fehre on Vimeo.

OK let’s build something!

Since there is a full Linux, in my case Raspbian, running on the Pi you can
develop in any language you like, but Couchbase lites portable version is
written in Java, so the way to go for me was using JRuby. JRuby lends itself
quite nicely to trying stuff quickly without the overhead of setting up a big
project, and since it provides great interoperability with
Java
, using any of
the Java libs is very easy.

Couchbase Lite on a Raspberry PI

So what are building?

As shown in the video we will create a small messaging app, which works cross
platform and even if you are offline. The idea is to store all the messages in
the local Couchbase Lite database and have it sync with a server somewhere on
EC2, which in turn all clients subscribe to. The full final code is available
on github
. Just to quickly
review what you need to follow along:

  • JRuby (http://jruby.org)
  • Raspberry Pi running with Raspbian or a Mac running OSX
  • A sync gateway instance, you can get one by signing up to
    CouchbaseCloud

Let’s get started!

Loading in all the dependencies

Couchbase Lite has some dependencies, and we first of all need to make sure all
the needed jars are loaded, for the most part those are plain java jars so
there is no need to do anything platform specific, so simply loading the whole
directory works fine.

There is one natively compiled dependency so which needs some special attention to be loaded
depending which platform you are on. In this case detecting the OS as well as
the CPU platform let’s us get the right jar loaded. I compiled the
couchbase-lite-java-native
in for Raspbian and the linux-arm platform, as well as for MacOS so
the application runs on either and load depending on which platform the app currently runs on.

Compiling for additional platforms is simple as well, just checkout the code
and run $ gradle jar, in case of the Raspberry Pi you might need
to adjust the build.gradle file according to the
crosscompile-build.gradle and include the paths for the compiler
as well as the path to jni.h. Take a look the compiler settings
in this case

And make sure it finds the required JNI header

With this out of the way we can start using Couchbase Lite in our application.

Initialize the application

Using the database from within the application requires some setup during the initialization.

We need to get access to a JavaContext first, which is similar to
an Android Application Context, and is needed to provide a basic information for
the whole application. This contains elements like the directory for data
storage and alike. We also need to make sure to get the java String object from a URL string
used as the location of the Sync Gateway, which we will talk about shortly.
Lastly the manager is used to acquire the actual database instances, and handle
access to them. Getting access to the Database via the manager is done by
simply getting the database with a given name, in case the database is not
present already it will be created for you and stored on disk in the data
directory, which defaults to the current application directory.


Setting up the sync

The ease of sync is one of the big advantages Couchbase Lite has, so let’s set
it up. First we need some backend to sync with. If you just want to try it out
take a look at Couchbase Cloud which
let’s you setup a sync_gateway with just a few clicks, or just run it locally,
by downloading form Couchbase Mobile, soon
sync_gateway will also be available in
homebrew for MacOS. After
this is done just grab the URL and point the app to it.

Now all is left is to create two replications one to pull data from the remote
whenever there is a change, and one to push our own changes up.

And finally we need to handle any changes which get passed along, this is done
by implementing a so called ChangeListener Interface which just
needs to implement one method void change(ChangeEvent event) which
gets called for every change being replicated. I our case we just log the
change and update the UI as needed. Since this class is implementing a
Java Interface we also need to include some annotations for JRuby to handle the
interfacing with Java correctly, mainly annotating the method with the type
signature required.

And with this we are now ready to create and read the data.

Reading and writing data

When writing to couchbase lite we need to create a document first and than save
the data to it.

In this case we create a document in the given database with the provided text.
The type of the document is set to message. The type is no special property but it
is common to set it because it allows us to filter more easily later as there
are more different types of documents stored in the database. Now to read the
data out we could either do so via the document id which is assigned on
creation or by creating a query. Since we want to display all the documents it
makes sense to create a query which reads all the documents and stores them in
an array.

Couchbase Lite allows for more advanced queries using Map Reduce and also for
live updating queries, but for this simple application this approach will be
sufficient.

Displaying the results in the UI

Now that everything is in place we can create a UI using Swing to actually
provide us with the needed interactions. This is rather simple so I’m not going
to walk through all the code, the important part is to setup a listener to
actually create new messages as the user presses send.

And adding rows to the created table as the new data rolls in

Thats it a quick and simple messaging app, runnable on a Raspberry
Pi, MacOSX and basically anywhere there is Java.

Conclusion

Getting up and running with JRuby and Couchbase Lite on the Raspberry Pi is
really simple, the JVM comes pre-installed and JRuby is just a download away,
this opens up the Raspberry Pi to a nice set of applications with the ability
to operate on the data locally without having to worry about the syncing
yourself. I’m already looking forward to some home automation, dashboards and
alike interfacing with my iPhone or Android Tablet, maybe I finally get my
intelligent fridge? ;).

Resources

chruby homebrew and native extensions fail…

I always try to run the latest version of ruby, via chruby and ruby-build.
Recently I ran into a wierd issue, and I’m not really sure how to resolve it,
yet, but managed to work around it. When installing gems with native extensions
via bundler, ruby seems to not find libraries installed via homebrew, it works
fine just running gem install so. Also all extensions installed
seem to stop working between ruby 2.1.0 and 2.1.1. Even though I don’t know
what’s going on, the work around seems to be installing all gems which have
native extensions via gem install GEM and running bundle
install
after the fact. Now everything works again…

« Older posts