Saturday, October 4, 2014

Managing server-side ssh commands

When running a long-running command over ssh, sometimes I need that command to be aware of the client connection and to exit with SIGINT on disconnect.

http://stackoverflow.com/questions/3235180/starting-a-process-over-ssh-using-bash-and-then-killing-it-on-sigint

Stdin and stdout are the best ways to detect an active connection. (Other alternatives require port forwarding, or log trolling.)

This is the best way I have found to do this. You want something on the server side that attempts to read stdin and then kills the process group when that fails, but you also want a stdin on the client side that blocks until the server side process is done and will not leave lingering processes like <(sleep infinity) might.


Thursday, August 7, 2014

Rails ajax render template

When working Rails Ajax queries, I commonly need to update more than one section of a page. For instance, a submit action might update a data table, but may also need to show some notifications in #messages. I've collected a few techniques that I've found posted in various forums and blogs into this solution that I use.

One important thing to note is that Rails is much friendlier with form submissions with remote: true than it is with $.ajax() calls. ActiveRecord models tie gracefully with the form_for tag, but sometimes link_to tags are used with anchors (links).

The first thing to use is the remote: true form option which sets the html data-remote attribute. Secondly, set a custom data-update-target attribute which specifies which div to replace with the body of the rendered ajax layout.



The shared messages template is used by both the main application layout and the ajax layout to provide updated notices on ajax actions.



Here is the ajax layout template rendered for the response:


And here is the javascript that extracts the #messages div replacing the notices, and replacing the data-update-target div:



Finally, in the controller, set the ajax layout for the action.



Sunday, January 12, 2014

Android Model-View-Presenter

There are a lot of conflicting recommendations around using the MVP pattern with Android activities. The two suggested approaches make different tradeoffs, and while one of the approaches looks good on the surface, it misses the point.

The first school of thought is that the view is the layout and the activity is the presenter. This approach is convenient because the view only needs to subclass a layout like RelativeLayout and the presenter already receives some of the interesting events such as onCreate, onResume, etc.

The second approach is cleaner from the presenter perspective. The goal of the MVP pattern is to separate the view logic from the view itself. Therefore, the presenter should be divorced from any kind of view specific hierarchy, such as extending Activity. This goal exists to make the presenter more testable, and it is worth the message forwarding from the activity (now the view) to the presenter.

It is fairly trivial to create a common AbstractPresenter and AbstractViewActivity that forward the most common events to the presenter: onCreate, onResume, onPause, onDestroy, onCreateOptionsMenu. Now it becomes much simpler to define the Presenter.View interface that extends your common view interface and provides the view specific methods such as setSelectedTab, enableMenuActions, or whatever interfaces you may need.

Also, by making the activity the view, the most trivial logic can be left in the view itself since only the most interesting logic needs to be tested in the presenter. For example, when handling a menu item action, a confirmation dialog may need to be shown. The real presenter logic takes place in the ok/positive button listener, and there is no need for the presenter to handle the logic of confirming the action with the user. It is actually the logic that taken when the ok button is selected that is the subject under test, so the ok button listener delegates this action to the presenter.onSomeInterestingAction.

Sunday, December 8, 2013

Inverting the git index

I wanted to find a way to invert the git index (swap the staged and unstaged files in the index). There were some answers on stackoverflow but the best I found initially required an interactive rebase to reorder the commits.

http://stackoverflow.com/questions/14628830/git-invert-staging-area/20458127#20458127

Then I found another answer about how to use tags to invert commits. This is ultimately what I'm using now by putting both answers together:

Wednesday, December 4, 2013

Android multi-user sdcard access

I haven't found the documented way to see the sdcard content for different users on a multi-user android  nexus7 using adb  (suppose I need to see some debug logs). However I did find this:

cd /storage/sdcard0
ls ..

What is listed is not actually the content of  /storage. There are listings for user 0 (root) and user 10 (the other user). You can confirm this with ps, you'll see processes prefixed with u0_ and u10_.

So:
ls ../0 lists the tablet owner's files
ls ../10 lists the other user's files

You can also copy files from there.

cp ../10/Download/somefile .

Note: Even tab autocompletion works too.

Thursday, November 21, 2013

Try/finally to do post-super actions with return

A common pattern for overriding a super method requires keeping the return result of the super method in a temporary variable:


But this temporary variable can be avoid using try/finally: