Marcus Ramberg Something deeply moving and profound

Mojolicious::Plugin::RenderSteps

This weekend I attended the MojoConf hackathon, which was great fun. I had some interesting talks with the rest of the core team, and I collaborated with Joel Berger on Mojo::PG, an adaptor for the Mojo::IOLoop for Postgres. Joel is almost done with a Pool implementation as well, and we'll probably be on CPAN sometime this week.

I also wrote a simple plugin-helper, which I think greatly simplify working with async controllers in Mojolicious. This what you have to do to setup async actions in mojolicious at the moment:

#!/usr/bin/env perl
use Mojolicious::Lite;

get '/foo' => sub {
  my $self = shift;
  $self->render_later;
  # Concurrent requests
  my $delay=Mojo::IOLoop->delay(
    sub {
      my $delay = shift;
      my $url   = Mojo::URL->new('api.metacpan.org/v0/module/_search');
      $url->query({sort => 'date:desc'});
      $self->ua->get($url->clone->query({q => 'mojo'})  => $delay->begin);
      $self->ua->get($url->clone->query({q => 'mango'}) => $delay->begin);
    },

    # Delayed rendering
    sub {
      my ($delay, $mojo, $mango) = @_;
      $self->stash (
        mojo  => $mojo->res->json('/hits/hits/0/_source/release'),
        mango => $mango->res->json('/hits/hits/0/_source/release')
      );
      $self->render;
    }
  )->catch(sub { shift->render_exception(shift) });
  $delay->wait unless Mojo::IOLoop->is_running;

With the help of my new helper, that can be turned into this:

#!/usr/bin/env perl
use Mojolicious::Lite;

plugin 'RenderSteps';

get '/foo' => sub {
  # Concurrent requests
  my $self=shift;
  $self->render_steps(
    sub {
      my $delay = shift;
      my $url   = Mojo::URL->new('api.metacpan.org/v0/module/_search');
      $url->query({sort => 'date:desc'});
      $self->ua->get($url->clone->query({q => 'mojo'})  => $delay->begin);
      $self->ua->get($url->clone->query({q => 'mango'}) => $delay->begin);
    },
    # Automatic rendering at after last step
    sub {
      my ($delay, $mojo, $mango) = @_;
      $self->stash (
        mojo  => $mojo->res->json('/hits/hits/0/_source/release'),
        mango => $mango->res->json('/hits/hits/0/_source/release')
      );
    }
  );
};

PS. We are looking for someone to host Mojoconf 2015, and we've donated 2000 EUR to get the next host kickstarted. Contact Oslo.pm if you're interested.

Monit notifications in slack #

A quick ruby script that lets you recieve monit notifications in Slack when a process is in trouble on your server.

Improved init.d script for unicorn

I went looking for a init.d script for Unicorn today, and Google seemed to give me various links to this one - It's a nice and simple script, but it has a couple of issues. For one, it's missing the LSB header, and more importantly it is missing status.

This does not make Ansible very happy, as it is using the status to check if the service is running before enforcing state=stopped or state=running. I spent some time tracking this down, so I wanted to share a fixed version with LSB headers and status implemented, in case someone else has the same problem.

Syncthing #

Syncthing is an Open Source Dropbox/btsync replacement. Been looking for something like this for a long time.

Prose.io #

I still prefer to edit my posts in the command line with vim, but prose.io seems like a really nice alternative for when you need to post to Jekyll on the go, from a web browser.