Last active
December 29, 2015 07:29
-
-
Save jberger/7636217 to your computer and use it in GitHub Desktop.
Mojo::IOLoop::ForkCall and libreoffice to convert a doc file to pdf nonblocking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojo::Base -strict; | |
use Mojo::UserAgent; | |
use Mojo::IOLoop::ForkCall; | |
my $ua = Mojo::UserAgent->new; | |
my $fc = Mojo::IOLoop::ForkCall->new; | |
my $r = Mojo::IOLoop->recurring(1 => sub { say 'Converting ...' }); | |
my $delay = Mojo::IOLoop->delay; | |
$delay->steps( | |
sub { | |
my $delay = shift; | |
$ua->get('http://crypto.stanford.edu/DRM2002/darknet5.doc' => $delay->begin); | |
}, | |
sub { | |
my ($delay, $tx) = @_; | |
my $name = 'darknet5.doc'; | |
$tx->res->content->asset->move_to($name); | |
$fc->run(sub{ `libreoffice --headless --convert-to pdf $name` } => $delay->begin); | |
}, | |
sub { | |
my ($delay, $err, $result) = @_; | |
Mojo::IOLoop->remove($r); | |
print "Conversion result: $result"; | |
} | |
); | |
$delay->wait unless Mojo::IOLoop->is_running; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment