Skip to content

Instantly share code, notes, and snippets.

@s1037989
Last active March 30, 2021 11:02
Show Gist options
  • Save s1037989/461f4543ce097fbfe822884d2fe66c82 to your computer and use it in GitHub Desktop.
Save s1037989/461f4543ce097fbfe822884d2fe66c82 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
use Mojo::File qw(path);
use Mojo::IOLoop;
use Mojo::JSON qw(false true);
use Mojo::Log;
my $tmp = path('/dev/shm/v')->make_path;
get '/reset/:build' => {build => ''} => sub ($c) {
my $build = $c->param('build');
$build = $build ? "build.$build" : 'building';
return $c->reply->not_found unless -e $tmp->child($build);
$tmp->child($build)->remove;
$c->log->info("reset $build");
$c->render(json => {ok => true});
};
get '/build/:build' => sub ($c) {
my $build = $c->param('build');
return $c->reply->not_found if -e $tmp->child('building');
return $c->reply->not_found if -e $tmp->child("build.$build");
$tmp->child('building')->spurt($build);
Mojo::IOLoop->subprocess->run_p(sub {
sleep $build;
my $date = qx(date);
my $err = ($? >> 8);
die $err if $err;
chomp($date);
return $date;
})->then(sub (@results) {
$c->log->info(@results);
$tmp->child('building')->move_to($tmp->child("build.$build"))->spurt(@results);
})->catch(sub ($err) {
chomp($err);
$c->log->error($err);
$tmp->child('building')->move_to($tmp->child("building.$build.err"))->spurt($err);
});
$c->render(json => {started => $tmp->child('building')->stat->mtime, build => $build});
};
get '/status/:build' => {build => ''} => sub ($c) {
my $building = $tmp->child('building');
my %building = (started => $building->stat->mtime, build => $building->slurp) if -e $building;
my $last = $tmp->list->grep(qr/build\.(\d+)/)->sort(sub{$a->stat->mtime <=> $b->stat->mtime})->last;
my %last = (build => ($last->basename =~ /build\.(\d+)/), started => $last->stat->mtime, results => $last->slurp) if -e $last;
my $b = $c->param('build');
my $build = $tmp->child("build.$b") if $b;
my %build = (started => $build->stat->mtime, build => $b, results => $build->slurp) if -e $build;
$c->render(json => {building => \%building, last => \%last, build => \%build});
};
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>Welcome to the Mojolicious real-time web framework!</h1>
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
@s1037989
Copy link
Author

@s1037989
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment