RSS

Tag Archives: mojolicious

perlbrew and mojolicious

I’m not a fan of the guys over at mojo but it’s probably the better of the Perl micro-webframeworks out there. So I was curious if mojolicious was going to work with perlbrew.

The first thing I did was install perlbrew. There are several ways to do it. I decided upon the first option:

curl -Lk http://xrl.us/perlbrewinstall | bash

What I do not like about the above command is that the code is assumed to be good and safe. It would have been a little more helpful of the code were downloaded from CPAN.

Once the module was installed. I was directed to add a line to my .bash_profile and then restart my terminal session. Easy enough.

NOTE: I did not recall what the base version was so I edited the .bash_profile file again and commented out the line that I was instructed to include. Then I opened a new terminal session and executed the command:

perl -v

My default/host perl version was 5.12.3. And I wanted to install perl 5.16.0 the latest and current version of perl:

perlbrew install 5.16.0

Easy enough! At this point there was a message on the console that suggested a tail command that I could use to monitor the build. That was easy too. In the end it took about an hour or so and I had a working Perl 5.16.0. (feel the perlbrew doc for the interesting commands)

As a last step I wanted to see what was going to happen when I installed mojolicious, could it be installed in userspace, and which version was it doing to use. So I installed mojo:

curl -L cpanmin.us | perl - Mojolicious

I omitted the ‘sudo’ that the mojo guys recommended and it installed fine. But now the proof needed to be in the pudding. I created a hello.pl file:

use Mojolicious::Lite;
get '/' => {text => 'Hello World! ' . $] };
app->start;

Notice that I added the $] to the message. This is going to append the Perl version number to the end of the hello world string. The good news is that when I ran the application:

morbo hello.pl

and launched my browser, I received a message that told me I was using Perl version 5.16.0. Perlbrew was a success and so was Mojo.

 
10 Comments

Posted by on 2012/05/26 in Tools, web

 

Tags: , ,

In defense of dynamic languages

There are a good many truths and there are a better set of likelihoods. Given the current state of dynamic languages today they are less performance than static and functional languages, however, it is also true that dynamic languages are more productive than static and functional languages. (I am not talking about savants)

Don’t optimize your code at the first stage. First make it right, then (if necessary) make it fast (while keeping it right). –erlang programming rules

It is likely that regardless of the size of your project, the size or makeup of your team, or the breakthrough that you think the project represents… that your project is going to have average results at best. The Google’s, FaceBooks and Twitters of the world are extreme edge cases. As proof, look at the iPhone app store. There are over 600,000 apps and only a very small fraction of those apps have the following that Angry Birds does.

So before you go off in a corner reinventing the wheel in your favorite language consider this. WHat is going to be your return on investment? I cannot blame you for learning a new language or tool that would enhance your marketability or even just for hobby sake. But if your intent is to make some money and maybe a little independence they you really need to consider your ROI. And if you’re making money then rewriting your killer app in whatever killer fast programming language is available (and popular) will make make plenty of sense.

This is why I’m hot on python and python’s django, tornadoweb, flask; perl and perl’s mojolicious; ruby and ruby’s sinatra and rails; redis, sqlite, zeromq.

PS: While I’m not a fan of erlang, partly because of what it represents, I really like it’s Programming Rules and Conventions(PRC). By comparison python’s PEP-8 is amateurish. The PRC starts off with ideas like the one quoted above and giving you ideas on how best to approach the problem. This is like python’s PEP-20 but again it’s like signing your name with a crayon instead of a fountain pen.

 
Leave a comment

Posted by on 2012/05/03 in architecture, ProgLang

 

Tags: , , , , , , , , , , , ,

Your Next Web Application Framework?

Suppose you are the person who has to make the decision as to what language and framework your startup is going to use to deploy it’s application.  What would you choose? There are so many interesting and qualified frameworks that are already powering a good portion of the internet.

(You don’t have to know the language and framework but enough to argue what makes it idea.)

What would it be?

For example: I read an article several years ago that strongly recommended Erlang. At the time it was a great idea. The author suggested that using Erlang would attract smart people and keep the actual number of respondents to something manageable. Since then I implemented several applications that in hindsight: (a) impossible to attract new talent. (b) the more time that passes the more fragile the app gets because my detail recollection is fading (c) and it lacks common tools that would make allow generalized apps to give access to “operators” instead of programmers.

Other examples include: perl, ruby, python… mojolicious, rails and sinatra, flask, tornadoweb, and django.

I have my ideas… what are yours?

 
Leave a comment

Posted by on 2012/04/12 in architecture, future, web

 

Tags: , , , , , , , , , , ,

RestMQ routing table for PerlMQ

[Update 2011-12-11] After several months of preparation to build this app I’m regretfully abandoning the effort. a) the mojo guys; b) since I’ve decided to concentrate on Python… RestMQ will have to do because there is no sense in rewriting the code for TornadoWeb. Good luck to us all.

As the project continues this article is going to convert the routing table from the RestMQ app to the PerlMQ version. The RestMQ snippet for the routing table associates the URL to a particular event handler in a list form. The Mojolicious version does not. In the Mojo version the URL and the handler’s signature are declared at once.

Here is the RestMQ routing table:

(r"/", IndexHandler),
(r"/q/(.*)", RestQueueHandler),
(r"/c/(.*)", CometQueueHandler),
(r"/p/(.*)", PolicyQueueHandler),
(r"/j/(.*)", JobQueueInfoHandler),
(r"/stats/(.*)", StatusHandler),
(r"/queue", QueueHandler),
(r"/control/(.*)", QueueControlHandler),
(r"/ws/(.*)", WebSocketQueueHandler),

Converting this to the perl version is going to be pretty easy. Most of the handlers have support for get/post and some support delete. These are the rest commands and the actual route or call of the individual function methods are handled by the frameworks. As to whether the list version is better than the inline version is to be debated.

In my opinion the list version is better because it’s concise. The code is self documented right here. However, Mojolicious has an interesting feature is that it can export the routing table as part of the command line interface. This allows the user to verify the work before forcing the programmer to re-review the code from scratch.

The Mojolicious analog of the route table above is as follows:

# ----------------> (r"/", IndexHandler),
# http://localhost/?[queue=...]&[callback=...]
get '/' => sub { ... }
# http://localhost/
# queue=
# [msg=...]
# [value=...]
post '/' => sub { ... }

# ----------------> (r"/q/(.*)", RestQueueHandler),
# http://localhost/?[callback=...]
get '/q' => sub { ... }
# http://localhost/<queue>?[callback=...]
# [msg=...]
# [value=...]
post '/q/:queue => sub { ... }

# http://localhost/<queue>?[callback=...]
delete '/q/:queue' => sub { ... } 

# ----------------> (r"/queue", QueueHandler),
# http://localhost/queue
get '/queue' => sub { ... }

# http://localhost/queue
# [msg=...]
# [body=...]
post '/queue' => sub { ... }

# ----------------> (r"/c/(.*)", CometQueueHandler),
get '/c/:queue' => sub { ... }

# ----------------> (r"/p/(.*)", PolicyQueueHandler),
# http://localhost/p/<queue>
get '/p/:queue' => sub { ... }

# http://localhost/p/<queue>
# [policy=...]
# [callback=...]
post '/p/:queue' => sub { ... }

# ----------------> (r"/j/(.*)", JobQueueInfoHandler),
# http://localhost/j/<queue>
get '/j/:queue' => sub { ... }

# ----------------> (r"/stats/(.*)", StatusHandler),
# http://localhost/stats/<queue>
get '/stats/:queue' => sub { ... }

# ----------------> (r"/control/(.*)", QueueControlHandler),
# http://localhost/control/[queue]
get '/control/:queue' => sub { ... }

# http://localhost/control/<queue>
# [status=(start|stop) ]
post '/control/:queue' => sub { ... }

# ----------------> (r"/ws/(.*)", WebSocketQueueHandler),
# I'm not sure how this is going to translate yet
#get '' => sub { ... }
#post '' => sub { ... }
#delete '' => sub { ... }

PS: One thing that is missing from this project so far… is the actual layout of the folders and and sort of installation program. For the moment I have deferred that step because I’m still trying to decide whether or not to integrate this into the Mojo project (If they will take me).

 
Leave a comment

Posted by on 2011/12/07 in ProgLang, web

 

Tags: , , , , ,

Reverse Engineering RestMQ

RestMQ is no big secret. The source is available online and there is some doc too. The project I’m referring to was written in python using the Cyclone framework which depends on Twisted. I like both of those tools but they feel wrong for a project like this. It seems to me that the dependencies are too deep.

That’s when I requested and was granted an account from the CPAN team. My intention is to build a version of RestMQ in perl using the Mojolicious framework. I originally thought to call it ‘RestMQ-pl’ and then I thought of ‘PerlMQ’ and I’m also lingering on ‘MojoMQ’. The last one makes sense if I create a separate core API than can be integrated into the Mojo base code. (when I learn it)

The RestMQ code and doc is on par with just about every other open source project out there but there are some limits. One thing that is missing for me is enough code/doc that I can use as a requirements doc in order to implement the code in any language or any platform. So I am forced to install it and reverse engineer the behavior and the data.

The other item that I’m struggling with is that this article is getting longer and longer. It’s hard to edit, get feedback and move on to the next idea… and quite possibly abandon the article if there is no interest. So for the purpose of this article I’m going to address installing RestMQ, Cyclone and Twisted on an OpenBSD 5.0 system which I have been using for general purpose development for the last few months.

I tried to install RestMQ and it’s dependencies on an OpenBSD 5.0 system. My first attempt was a failure because the easy_install script reported that it was missing epoll.h which I’m pretty certain is a Linux kernel file and therefore is not going to be available on OpenBSD.

My next stop is the Twisted website to see if they have a proper build script. That too failed because while Twisted was available in source form there were still too many dependencies that I did not want to chase after… and while I really do want some “full stack awareness” at this moment it’s off target for the mission. Get ‘er installed.

I started my second attempt using an OpenBSD package:

# OpenBSD 5.0
pkg_add http://ftp.openbsd.org/pub/OpenBSD/5.0/packages/i386/py-twisted-web-11.0.0.tgz
easy_install cyclone
wget https://github.com/gleicon/restmq.git
cd restmq
python ./setup.py install

This was not going to be enough. A couple of the scripts expect the bash shell to be in a different place. There are a couple ways to solve this and for an OpenBSD system the following is a severe cheat. One should really correct the code. Possibly change the static to use “env”. But in the meantime that too would be off mission. So this sym link will have to do for the moment.

# need bash in the right place
ln -s /usr/local/bin/bash /bin/bash

Let’s see what happens next. From this point you should be able to follow the RestMQ instructions for starting and testing RestMQ… and it worked. Now for some reverse engineering.

(The next article will roughly address the route table.)
 
Leave a comment

Posted by on 2011/12/07 in web

 

Tags: , , , ,

Mojolicious or TornadoWeb?

It depends on what you are really trying to accomplish.  In a current project I’m using TornadoWeb to get the transaction through the ZeroMQ broker to a worker. (standard request/response broker implementation).

Since I’m using TornadoWeb I needed to implement about 50 lines of glue code and I needed to hijack TornadoWeb’s IOLoop instance with the IOLoop from ZeroMQ. And since the work is only a few dozen lines of code … it’s all up for debate.

In retrospect I think I should have gone with Perl+Mojolicious+beanstalk. It would have completely eliminated my own broker .. another 100 lines of code. Then there was the direct access to redis in tornadoweb.

MEH! While I’m pragmatic that if I did not have to wtote an extra 150 lines of code then I would nto have to debug them either.

This race between the two platforms is so close! (on any given Sunday)

 
Leave a comment

Posted by on 2011/11/26 in web

 

Tags: ,

Django-App-Engine is not Django

I’m not going to give these guys any more press than they deserve.  I tweeted that “django-app-engine development halted”. As I tweeted the message I thought they were referring to the DjangoProject, however, I was wrong! Seems the Django project is still going strong, I don’t know who these other guys are or what their endgame is…

Anyway, Django is a strong platform. I do not use it much these days because I’m focusing on TornadoWeb and Mojolicious but it is rock solid. It’s admin tools can bootstrap just about app you might consider building.

 
Leave a comment

Posted by on 2011/11/24 in web

 

Tags: , ,

AnyEvent and memory leaks

Recently twitter kraih posted a gist link that demonstrated a simple memory leak. I say it’s simple now because I’ve spent enough time analyzing it. To say that it’s a beginner’s error is a bit arrogant but such as it is the bug points out a bigger issue when using AnyEvent (whether inside a Mojolicious app or not) it’s own has potential to  cause memory leaks.

In the example:

# Very common leak
my $foo;
$foo = sub {
 my ($i, $j) = @_;
 return if $i >= $j;
 say $i++;
 $foo->($i, $j);
};
$foo->(1, 10);

The bug here is caused because $foo is declared and set outside the scope of the virtual function. Then within the function the code access the variable again. That’s where the variable reference count is incremented. One way to correct this function looks something like:

# Very common leak
my $foo;
$foo = sub {
 my ($foofoo, $i, $j) = @_;
 return if $i >= $j;
 say $i++;
 $foo->($foofoo, $i, $j);
 undef $foofoo;  #this line is optional
};
$foo->($foo, 1, 10);

The corrected example is nothing special but it’s not pretty (so maybe aesthetics should be #5 or #6 on the list).

Where this is a problem for AnyEvent is that it uses a virtual callback function in just about every API. This means that if the function does not push the data you need into the callstack of the callback that you have to get it by triggering access to the variable though the scope.

my $retval = undef;
$ctx->do_somthing_cool(1, 2, 3, cb => sub { $retval = 'done'; }).recv;

The above code is going to leak memory for the same reason that the first example gives. Now there is some good and bad news. The bad news is that there is additional work to do. The good news is that it’s pretty standard and not that big of a deal. Just more aesthetics.

my $retval = undef;
$retval = $ctx->do_somthing_cool(1, 2, 3, 
                    cb => sub { my $myretval = 'done'; 
                                return $myretval}).recv;
 In AnyEvent, recv() returns the value in the “return” function inside the callback. One place I have not experimented with yet is what happens when you have more than one function. For example:
my $retval2 = undef;
my $retval2 = undef;
$ctx->do_somthing_cool(1, 2, 3, 
 cb => sub { my $myretval = 'done'; 
              return $myretval}); 
$ctx->do_somthing_cool(1, 2, 3, 
 cb => sub { my $myretval = 'really'; 
              return $myretval}); 
[$retval1, $retval2] = $cv->recv()

In this example I do not know what ->recv() is going to return and if it does return everything; what order will the results be in.

The most important thing is making sure that you do not increase the number of references unnecessarily. Voila.  I’m not a beginner any more.  *sniff*

 
2 Comments

Posted by on 2011/11/21 in web

 

Tags: , ,

Memory Leaks? Really?

I don’t know anything about the specifics on this one… but when the project leader makes a statement like:

I have yet to see one non-trivial #perl web application that doesn’t leak memory, wish we had cycle detection like #python does since 2000.

Ya gotta wonder whether or not this is a good idea? Is the framework the problem or the language?

When I did a search for “memory leak mojolicious” there was mention of some problems going back to version 1.56… but I’m so disgusted with the state of things that I hope he/they make a correction or statement addressing the issues… I also hope it’s not sarcasm.

I cannot wait to update this post!!!

[update #1] “Take this little example, how would you explain what’s going on to a #perl beginner? gist.github.com/1381643″ [ref: twitter: kraih]

[update #2] “Perl’s memory management is well-hidden, but is based on reference counts and garbage collection. It also has mortal variables, whose lifetimes are limited to the current context. It is possible to free(1) the memory(2) assigned to variables (including arrays) explicitly, by undef-ing the only reference to them.” (ref: http://bit.ly/tH2XJt)

[update #3] since perl uses a reference counter in order to determine what memory to release, this is not much different than Java and a host of other memory managers. It’s easy to cause a leak by having a circular linked list reference and then dropping all of the userspace references to that list. That can easily happen within a function that declares some local memory for a list of some kind and then exits without cleaning up. This is a perfect justification for functional programming style. “no side effects”

 
Leave a comment

Posted by on 2011/11/21 in web

 

Tags: , ,

Getting Mojolicious and AnyEvent::Beanstalkd to work

I have been having a hard time getting Mojolicious and AnyEvent::Beanstalkd to integrate. Given the commonality of AnyEvent one would think that it would a) work off the shelf; and b) there would plenty of documentation or examples. Sadly neither are true and I had to test several permutations, give up, and then try one last method (which worked).

It basically comes down to this. $cv->recv() does not work inside a Mojolicious. It causes a recursion error that AnyEvent just barely explains. The first alternative, that worked, was a matter of nesting all of the callback functions and as long as the stack is shallow that would work but it’s not pretty and it’s certainly unmaintainable.

In the end the code was replaced with $txn->cb(sub { $_[0]->recv }); and then the beanstalkd commands were flattened; and everything seems to be working nicely now. There was no explanation as to why this worked.

After looking at the AnyEvent code I think the code was able to “falsely” detect the $cv->revc() as a recursive call where the other format is undetectable.  I’m not certain whether this is good or bad but it works.

 . . .
 my $txn = AnyEvent::condvar;
 $txn->begin;
 $client->use('restmq', sub {$txn->end;});
 $txn->begin;
 $client->watch($guid, sub {$txn->end;});
 $txn->cb(sub { $_[0]->recv });

 $txn = AnyEvent::condvar;
 $txn->begin;
 my $job2 = $client->put( $msg, sub {$txn->end;});
 $txn->begin;
 $client->reserve( sub { my $job = shift;
   my $decode = $job->decode;
   my $uuid = (@$decode)[0][0]->{'uuid'};
   my $retval = sprintf "data> %s, %s\n", $job->data, $uuid;
   $client->delete($job->id);
   $client->ignore($guid);
   $client->quit;
   $self->render(text => $retval);
   $txn->end;
 });
 $txn->cb(sub { $_[0]->recv });
 $self->render_later;

The complete code is here. I’ll be posting my scratch code shortly and I’ll be updating and commenting the code too.

 
Leave a comment

Posted by on 2011/11/18 in web

 

Tags: , , ,

 
One Page Docs

Creating a library one page at a time.

One Page Bugs

Reducing the friction of writing and fixing bugs or features.

Follow

Get every new post delivered to your Inbox.

Join 223 other followers