Tag: ruby (page 1 of 2)

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.

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
.

Setting up Couchbase views with Rake

If you are like me, you like your code and at best everything else about your
app to be versioned. This makes setting up a new development environment quick,
and even more important reproducible. So one thing which always bothered me was
setting up external code outside of the application to generate Couchbase views,
since those views are build using JavaScript, and by default live in the
web interface, far away from my application code. If you want to know more about
them watch the Couchbase 103 – Views and
Map-Reduce
by the way.

Since those JavaScript functions are code which is closely related to my app, I
like to keep it inside as well, and provide a task to actually set them up for
the given environments. The following quick gist provides a rake task to do
this, adding all views in config/couchbase_views to Couchbase.

This works great together with dotenv to provide the connection details via
environment variables, but can also depend on any other way to
set up the database connection easily.

Safari Readinglist to Readability

Ok so I got a Kindle Fire and since there is no way to sync my Safari
Readinglist over I decided it is time to move to something “portable”. Luckily
it is quite easy to get a list of all the current Safari Bookmarks, which
includes everything on the Safari Readinglist, the basics are in a Gist by andphe

So all there is left to do is use the
readit gem to get everything into
Readability, using oauth to authenticate first. Overall this went pretty well
and quickly, so for anybody interested you can find the scripts on
github.

Enjoy

README Driven Development: Testing libraries from a user perspective

What really is outside in testing when writing a library? I have seen different
approaches to this problem like cucumber, rspec or bacon, but one step that
seems to be missing in a lot of cases is testing what the user sees first, the
README. On Github the README file has really become the first thing any
developer coming in touch with a library looks at, so it should be the getting
started guide, and something that can make or break the experience a developer
has. But many developers including myself seem to face a problem so keeping the
documentation working and updated. This got me thinking what I really want in
there are code samples, and those code samples should work copy and paste.

To achieve this the README has to be part of my build process, so I started
experimenting with including a runner for code inside as a Rake task

The Idea really is nothing new, but I have been struggling to find a well
integrated solution. This is probably the simplest solution I could come up
with. I’m currently driving the development of a library idea for my talk at
takeoffconf, and it allows me to really focus on
the concept of the code from a user perspective writing the README first and
building the working code from there on.

Currently the solution is not robust so any help is greatly appreciated, even
though for now it works.

Daemons and output in ruby

Backgrounding a process in ruby is easy, just add

Process.daemon

This works great but by doing so you will actually lose any output since daemon
will redirect stdout and stderr to /dev/null by default. Depending on how you
handle logging this might actually not be a great idea, I personally prefer
redirecting the output of a daemon process myself so I can add logging easily.

This is easily solve so by passing both parameters for daemon, first to either
do or don’t change the working dir to / and the second to not redirect stdout
and stderr.

Process.daemon true, true

Just a nice thing which is quickly overlooked.

Using zeus without rails

At the last rubyshift the question came up if
zeus could be used without rails, and how to do
it. At the time I kind of expected it to be but wasn’t sure if there are any
complexities which I didn’t see at the moment because I never tried it. So
trying it out it actually works allmost as expected, the only thing needed is a
modification to the custom_plan.rb file which defines how the preloading
actually works. So just as a quick example I
setup a repository which
defines 2 tasks

$ zeus rake
$ zeus foo

This should get everybody started, as the tasks are pretty obvious and easily
modified for your needs.

Rubyshift May 2013, fast rails with zeus

At the last rubyshift I presented a quick
introduction to zeus as a way to manage rails
startup, so as a developer you don’t have to wait for 10+ sec just to execute a
simple rake command.

I’ll welcome any feedback.

Ruby snippet: figure out what has been loaded already

While trying to figure out why some piece of code was running inside the tests
but not in production I needed to figure out which files where actually loaded
at which point in the code. Using a combination of pry
and the LOADED_FEATURES global ie.

Enjoy

Older posts