Zeus console in Emacs

Zeus is quite a useful tool when working with larger rails applications, or other larger ruby applications for that matter. Especially as it reduces the time to open a interactive environment for the project and makes the whole development process much more fluid. Combining it with Emacs power to send pieces of code directly from a buffer to be executed in a ruby process this is quite a nice way to explore and work. To quickly get access to a Zeus console simply drop the following in your Emacs configuration.

(add-to-list 'inf-ruby-implementations
             `("zeus console" . "zeus console"))

Maybe my suggestion to add this to inf-ruby will be added, but even if not it’s simple enough to get a console with this via C-u M-x inf-ruby.

Hacking on rails using emacs

Rails contributor?

Apparently I committed to rails before, I’m pretty sure it was a doc update, but none the less this is something I actually like to do more. So first step get a working setup for running rails tests locally and getting a reasonably productive environment setup. Since I’m mostly using emacs these days it would be nice if everything would actually work out of this editor/operating system.

rake test

The easiest way to run the tests after cloning down rails and bundling is to use rake.

$ bundle exec rake test

in the root rails folder will run all the available tests, this is great but when working on something also not what I want most of the time. But running it in a subfolder like activesupport will actually do the right thing and run only this subset of tests

$ cd activesupport 
$ bundle exec rake test

To make this easier I’m using textmate.el to execute most commands while inside a project from the determined project-root. Meaning if I switch to a file I set the working directory to the current root automatically.

(defun coder/default-project-dir ()
  (if (textmate-project-root)
      (setq default-directory (textmate-project-root))))

(add-hook 'find-file-hook 'coder/default-project-dir)

Adding .emacs-project files to all the subdirectories will now make sure that when I run a rake command it will be run in the correct directory. Combining this with rake.el allows to run test via a simple M-x rake and selecting test, M-x rake-rerun will now rerun the given tests with a simple command.

This works great while working in quick running test suites but running the whole suite on every change can become inconvenient quickly, this is solved by 2 more enhancements to the flow

running a single test file

Rake allows for passing a file name to limit the run to a single test file

$ bundle exec rake test TEST="test/abstract/callbacks_test.rb"

for example will run only this file of the actionpack testsuite for example. Make sure to run with bundle exec, rake.el does not detect this when in a subfolder, but C-u M-x rake will let you modify the command as needed.

running a single test

Should a file have a lot of tests it can be a good idea to only run a single one, this is possible by passing an option to the minitest testrunner.

$ bundle exec rake test TEST="test/abstract/callbacks_test.rb" TESTOPTS="--name=test_around_action_works"

TESTOPTS will be passed through to minitest which will only run this one test.

integration in emacs

All this works on the command line, as well as using rake.el, personally I use rake.el and have rake rerun bound to C-c r to allow me to quickly run the tests I’m currently working on, this executes in compilation-mode, which allows emacs to parse out file urls and makes them clickable in case of errors.

Next up to make it even better would be to figure out the test name I’m currently in, this works quite simply for files which use the

class TestClass < Minitest::Testcase
  def test_foo
    assert_true doing_the_foo
  end
end

syntax, but is much harder with files

class TestClass < Minitest::Testcase
  test "doing the foo" do
    assert_true doing_the_foo
  end
end

So for now I stick with doing it by hand.

Some F# fun

I’m making my way through the The book of F# and been playing with implementing the RPN calculator

personally I found it quite interesting to see my choices during this small exercise, especially the use of Option types to handle error cases comes quite naturally. Another interesting thing I found was that, similar to how I would write the same thing in Lisp, it is very obvious to define functions inside other functions to hide implementation details I would like to give a name to but don’t want to share with the rest of the program.

How to remember circuit breaker configuration with Emacs

Some things are just not easy to remember and looking them up, saving a bookmark to wherever, is just tedious. So how about saving a bookmark and at the same time automating the calculation? Well this is why I started a file which hopefully grows with all those little formulas, using the lisp docstrong to provide a note and a link to why this is relevant.

First thing, when configuring a circuit breaker we need to set a size for how many parallel requests we want to accept, luckily Netflix has a nice formula for this, and even more lucky it is documented on the yammer tenacity repository.

(defun hysterix/thread-pool-size (p99 reqs)
  "calculate and insert the thread pool size for a hysterix
circuit based on the p99 and the requests per secound see
https://github.com/yammer/tenacity#configuration-equations for
more details"
  (interactive "nP99 in ms:
nRequests per sec: ")
  (let ((thread-pool-size (ceiling (* (/ (float p99) 1000.0) reqs))))
    (insert (format "%d" thread-pool-size))))

All I need to do now when configuring a circuit, hit M-x hysterix/thread-pool-size and insert the 99th percentile for request latency I can easily get from the service monitoring, as well as the requests per second. If ever in doubt I can use C-h f hysterix/thread-pool-size to get the documentation.

Creating Pull Requests quickly from Emacs

A lot of code I work on lives in GitHub, both work and personal project normally make their way there quickly so maintaining it means I work with the GitHub interface quite a lot as well. To make my live a little easier I decided to create a quick Emacs function to drop me in the matching interface and create or visit the pull request I’m currently working on. Luckily most of the pieces where already out there. All that was needed was grabbing the correct URL and branch from the current repository.

Switching git users in Emacs based on repository

Using Github internally for work is really nice, but especially when working on multiple projects, some on public and some on internal Github it can also be quite hard to always remember to use the correct username and email for the commits. Because I tend to forget such things I decided to create a little elisp function to make sure the correct user is set when I start magit. Right now I only have to switch between 2 users so it is managable, but I think this is easily extended to more if the need arises (god I hope not).

Learning some emacs

I have been a very faithful Vim user for the last couple years, but recently I ran into more and more things which made me want to know more OS like shortcuts. On Mac OS that means that using Emacs is pretty close, most of the basic text navigation just works in most other text windows, and Emacs is a powerful editor in itself as well. The advantage is that I can more or less have my whole setup inside emacs, it can act as my terminal, my editor, as well as my interface to other servers via SSH and the emacs built in trampmode. When editing some ruby I realized that I’ve been relying on CTags quite a lot, and I need to have a quick way to generate and update them in emacs as well, which let me to write my first few lines in ELisp in years.

(defun create-tags ()
  "Create tags file for current project defined by the textmate-project-root"
  (interactive)
  (let ((root (textmate-project-root))
        (tags-file (concat (textmate-project-root) "TAGS")))
    (shell-command (concat "ctags -e -R -f " tags-file " " root))
    (visit-tags-table tags-file)))

This small function allows me to run a simple

M-x create-tags

to update the CTags for the current project, aka the closest folder with containing .git directory, a Makefile, Rakefile etc.. So far the experience has been really nice, but I’m still catching up speed wise to my Vim days.

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.

« Older posts