The Fugue

Counterpoint by Hans Fugal

Rails Sessions

Posted by Hans Fugal Thu, 27 Mar 2008 15:05:00 GMT

I was doing some maintenance on my blog, and was devastated to find that Typo was taking 225 megabytes of resident RAM. Yikes! After some creative debug thinking and digging I figured out it was due to sessions. Typo now stores sessions in the database, so my maintenance cron job to delete old sessions didn't clean up old sessions. (Ha! had you going for a second!)

Well I could write a cron job to run a script to clean the sessions out of the db, like:

#!/bin/sh
sqlite3 /path/to/typo/db/production.db 'delete from sessions'

Ok, that's a bit extreme, but you get the idea. But when I deleted the sessions in this manner the memory usage didn't drop at all until I had restarted the server, which seems unnecessary. So instead I changed typo's configuration to use a different session store. I commented out this line in config/environment.rb:

-  config.action_controller.session_store = :active_record_store
+  #config.action_controller.session_store = :active_record_store

Then I restarted the server and fired up a browser. "Huh, that's odd… no sessions in tmp/sessions or /tmp or anywhere I can see. No, they're not in the database…" What I was seeing didn't match up with what all the stuff Google said. The default session store was PStore, aka file system, so they said. But apparently that recently changed in Rails, and now the default is CookieStore. From ActionController::Base documentation:

Sessions are stored in a browser cookie that‘s cryptographically signed, but unencrypted, by default. This prevents the user from tampering with the session but also allows him to see its contents.

Do not put secret information in session!

Well a quick grep -ri session app lib told me that typo wasn't storing anything secret, so I decided that default was alright with me. Now I don't have to set up any session cleanup script at all. Sweet.

Now, don't stop there. You should set your session key and secret while you're hanging out in config/environment.rb. Add the following lines in the same place as the line you commented out above:

config.action_controller.session['session_key'] = 'something unique'
config.action_controller.session['secret'] = 'get this from rake secret'

1 comment |

Typo and the Amazon Sidebar

Posted by Hans Fugal Mon, 29 Oct 2007 16:31:05 GMT

I finally got the typo amazon sidebar working. Nifty thumbnails and riches beyond measure are now mine! Well, at least the thumbnails…

This is what you need to do. Enable the Amazon postprocessor in your preferred text filter (e.g. Markdown), which lets you write urls as <a href="amazon:0743253434">…</a>. (Note: I find that if you don't use double quotes it doesn't work) Then, set up the Amazon sidebar, which is straightforward. Handcrafted URLs don't seem to work, though it's possible a handcrafted URL of the kind generated by the Amazon filter would (e.g. http://www.amazon.com/exec/obidos/ASIN/0743253434/thefug-20).

no comments |

Typo with a Prefix

Posted by Hans Fugal Sun, 28 Oct 2007 04:06:26 GMT

I just set up Typo again, for my wife's blog. She wanted the url http://erin.fugal.net/blog, the important part here is the /blog bit at the end. Rails in general has traditionally been fairly hostile to this prefix thing, but Typo has smoothed things out considerably. I've blogged about this before, but typo has changed in the meantime. Here's what you do:

First, in your Apache2 config change the ProxyPass and ProxyPassReverse lines to have /blog in both places, e.g.

ProxyPass             /blog http://localhost:4907/blog
ProxyPassReverse      /blog http://localhost:4907/blog

Then, tell typo about it:

cd /path/to/my/typo
typo config . prefix-url=/blog
typo restart .

Finally, you may need to flush the cache and/or reactivate the theme of your choice.

Kudos to the typo folks for making this relatively painless. Getting it to work in the "good ol' days" was truly painful.

no comments |

Typo Cache

Posted by Hans Fugal Sat, 27 Oct 2007 04:13:06 GMT

Typo, the blogging software that runs this blog, was getting slower and slower for an unknown reason. Finally I decided I had had enough and begged for help in IRC. sprewell was kind enough to help me track it down to an odd caching problem. Apparently my caches weren't going away like they should, and I had accumulated some 1.7 million files in my tmp/cache directory. Even running find tmp/cache | wc -l took a few minutes. No wonder typo (which was probably actually looking at each file's contents) was having a cow. Apparently this isn't a common problem, but they have struggled with cache problems in the past. So if your typo is feeling sluggish, check for the ballooning cache. If it's there, stop typo and move or delete the offending cache directory, i.e.

typo stop .
mv tmp/cache /tmp
typo start .

It's nice to have my old typo back.

no comments |

Trackback Spam

Posted by Hans Fugal Mon, 03 Jul 2006 17:29:41 GMT

My blog is fairly impervious to comment spam. I've got one or two comments that I had to delete, but I can respond fairly quickly because I get a Jabber notification of comments. I think the ajax comment mechanism does most of the filtering.

I just discovered that I am not impervious to trackback spam. Turns out there was a lot that I didn't even know existed. Luckily, I didn't have a single valid trackback so I could just do this at the rails console:

Trackback.find(:all).map {|t| t.destroy}
Article.find(:all).map {|a| a.allow_pings=false; a.save}

I also changed the default allow trackbacks setting to no. Trackbacks are a neat idea but obviously not one that works.

no comments |

flog

Posted by Hans Fugal Thu, 18 May 2006 15:04:00 GMT

A while back I switched my blog from Blosxom to Typo. Typo is really spiffy but I am not a fan of HTML edit boxes, so I looked for good blog posting software. I didn't find any, and certainly none that was free, and I was really comfortable with writing my posts using Markdown in vim, so I decided to write my own. Thus was born flog, so named because it rhymes with blog and reflects the true effect that my blog has upon the world.

From the README:

Run flog and edit the post template in $EDITOR. When done, save and exit $EDITOR and your post will be submitted.

I finally got around to adding the pieces to make it robust enough for general consumption, and so I am releasing it. You can find it at http://hans.fugal.net/src/flog.

Flog uses the MetaWebLog API, so it should work with a variety of blog software.

no comments |

Typo

Posted by Hans Fugal Sat, 01 Apr 2006 21:47:00 GMT

I am in the process of migrating my blog to Typo. Blosxom has served me well, but I think it's time I joined the ranks of cool blogs and allowed people to comment, trackback, etc. Blosxom can do all that, but it's not very much fun.

Blosxom's strength is its simplicity. When I wanted a simple blog, it was perfect.

Now, getting typo to work was not a walk in the park. I'll walk you through what I had to do.

First, you have to get typo and install it. I hear rumors that version 4 release is imminent, so I grabbed the svn version. Next, make a database and set up config/database.yml. Now you can run script/server -e production and point your browser at http://localhost:3000/ and give it a whirl.

I tried the RSS and atom converters in db/converters but I wasn't satisfied with the results. So I wrote a blosxom converter, which I will contribute. If you can't find it feel free to contact me.

The hard part was getting typo to run under a subdirectory in my mixed apache/lighttpd setup. All fugal.net sites are running through Apache. My rails apps run through lighttpd, with apache's mod_proxy. So, you have to configure apache, lighttpd, and rails each in turn.

Apache Configuration

    ProxyPass               /typo http://127.0.0.1:81/typo
    ProxyPassReverse        /typo http://127.0.0.1:81/typo
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>

Lighttpd Configuration

The normal lighttpd stuff, and then:

$HTTP["url"] =~ "^/typo(/|$)" {
    server.indexfiles = ("dispatch.fcgi")
    server.document-root = "/srv/www/typo/public/"
    server.error-handler-404 = "/dispatch.fcgi"
    server.errorlog = "/srv/www/typo/log/error.log"
    accesslog.filename = "/srv/www/typo/log/access.log"
    fastcgi.server = ( ".fcgi" => (
        "typo" => ( "min-procs" => 1, "max-procs" => 1,
            "socket" => "/tmp/typo.fcgi.socket",
            "bin-path" => "/srv/www/typo/public/dispatch.fcgi",
            "bin-environment" => ( "RAILS_ENV" => "production" ),
            "idle-timeout" => 120
            )
        )
    )
}

You don't need strip-request-uri, because we'll take care of the typo/ prefix in rails.

Rails Config

Add this to config/environment.rb:

    ActionController::AbstractRequest.relative_url_root = "/typo"

Finally, and this was the real stink, you have to help lighttpd find files in public/. Lighttpd gets a request for http://hans.fugal.net/typo/foo.html which is a static file in public/, and so it looks in the document root to find /typo/foo.html, which means it looks for /srv/www/typo/public/typo/foo.html, which doesn't exist. This is easy to fix:

    cd /srv/www/typo/public
    ln -s . typo

et voilá! You should be up and running.

For more reading see http://znark.com/blog/articles/2005/12/11/got-typo-working and http://blog.lighttpd.net/articles/2005/11/23/lighttpd-1-4-8-and-multiple-rails-apps.

Posted in | 3 comments |