From 65305cfde3e6d9d19d24fdb9768f4819e45fcdd1 Mon Sep 17 00:00:00 2001 From: Marco Pessotto Date: Wed, 31 Jan 2024 09:31:45 +0100 Subject: [PATCH] Improve workflow --- lib/AmuseWikiFarm/Controller/BookCovers.pm | 2 ++ lib/AmuseWikiFarm/Schema/Result/Bookcover.pm | 28 ++++++++++++++----- lib/AmuseWikiFarm/Schema/Result/Job.pm | 8 +++--- .../Schema/ResultSet/Bookcover.pm | 6 +--- root/src/bookcovers/edit.tt | 17 ++++++++++- root/src/bookcovers/listing.tt | 2 ++ 6 files changed, 46 insertions(+), 17 deletions(-) diff --git a/lib/AmuseWikiFarm/Controller/BookCovers.pm b/lib/AmuseWikiFarm/Controller/BookCovers.pm index ac32b898..81bbd71b 100644 --- a/lib/AmuseWikiFarm/Controller/BookCovers.pm +++ b/lib/AmuseWikiFarm/Controller/BookCovers.pm @@ -98,6 +98,8 @@ sub edit :Chained('find') :PathPart('edit') :Args(0) { my %params = %{$c->request->body_params}; # post request if (%params) { + # initialize again. This will refresh the template + $bc->initialize; my $tokens = $bc->parse_template; my $wd = $bc->working_dir; # should always be fine. diff --git a/lib/AmuseWikiFarm/Schema/Result/Bookcover.pm b/lib/AmuseWikiFarm/Schema/Result/Bookcover.pm index a070ea27..29b0ab65 100644 --- a/lib/AmuseWikiFarm/Schema/Result/Bookcover.pm +++ b/lib/AmuseWikiFarm/Schema/Result/Bookcover.pm @@ -605,23 +605,30 @@ sub produce_pdf { my ($self, $logger) = @_; $logger ||= sub {}; my $tex = $self->write_tex_file; + my $pdf = "$tex"; + $pdf =~ s/\.tex/.pdf/; + $self->update({ + compiled => undef, + zip_path => undef, + pdf_path => undef, + }); + if (-f $pdf) { + log_info { "Removing $pdf" }; + unlink $pdf or die $!; + } + $self->convert_images_to_cmyk($logger); + # this should happen only in the jobber, where we fork. But in # case, return to the original directory. - $self->convert_images_to_cmyk($logger); my $cwd = getcwd; my $wd = $self->working_dir; chdir $wd or die "Cannot chdir into $wd"; - my ($in, $out, $err); my @run = ("xelatex", '-interaction=nonstopmode', $tex->basename); my $ok = run \@run, \$in, \$out, \$err; chdir $cwd or die "Cannot chdir back into $cwd"; # log_info { "Compilation: $out $err" }; - if ($ok) { - my $pdf = "$tex"; - $pdf =~ s/\.tex/.pdf/; - $self->compiled(); - $self->pdf_path($pdf); + if ($ok and -f $pdf) { my $zipdir = Archive::Zip->new; if ($zipdir->addTree("$wd", "bookcover-" . $wd->basename) == Archive::Zip::AZ_OK) { my $zipfile = $wd->parent->child("bookcover-" . $wd->basename . ".zip"); @@ -655,6 +662,13 @@ sub username { return; } +sub initialize { + my $self = shift; + $self->create_working_dir; + $self->populate_tokens; + return $self->discard_changes; +} + before delete => sub { my $self = shift; if (my $wd = $self->working_dir) { diff --git a/lib/AmuseWikiFarm/Schema/Result/Job.pm b/lib/AmuseWikiFarm/Schema/Result/Job.pm index 488d19d5..06ee38b6 100644 --- a/lib/AmuseWikiFarm/Schema/Result/Job.pm +++ b/lib/AmuseWikiFarm/Schema/Result/Job.pm @@ -995,14 +995,14 @@ sub dispatch_job_build_bookcover { if (my $bc = $self->site->bookcovers->find($self->job_data->{id})) { $logger->("Producing cover for " . $self->job_data->{id} . "\n"); my $res = $bc->produce_pdf($logger); - Dlog_info { "Result is $_" } $res; + # Dlog_info { "Result is $_" } $res; if ($res->{success}) { - return sprintf("/bookcovers/bc/%i/download/bc-%i.pdf", $bc->bookcover_id, $bc->bookcover_id); + $logger->("SUCCESS!\n"); } else { - log_info { "Failure: $res->{stderr} $res->{stdout}" }; - $logger->("Failure compiling the cover!\n"); + $logger->("FAILURE compiling the cover! Please contact your admin\n"); } + return sprintf("/bookcovers/bc/%i/edit", $bc->bookcover_id); } else { $logger->("Cover data not found!"); diff --git a/lib/AmuseWikiFarm/Schema/ResultSet/Bookcover.pm b/lib/AmuseWikiFarm/Schema/ResultSet/Bookcover.pm index 3b015674..a31ec406 100644 --- a/lib/AmuseWikiFarm/Schema/ResultSet/Bookcover.pm +++ b/lib/AmuseWikiFarm/Schema/ResultSet/Bookcover.pm @@ -11,11 +11,7 @@ use DateTime; sub create_and_initalize { my ($self, $values) = @_; - my $bc = $self->create($values); - $bc->create_working_dir; - $bc->populate_tokens; - $bc->discard_changes; - return $bc; + return $self->create($values)->initialize; } sub expired { diff --git a/root/src/bookcovers/edit.tt b/root/src/bookcovers/edit.tt index 8114fd0d..b6cd49cb 100644 --- a/root/src/bookcovers/edit.tt +++ b/root/src/bookcovers/edit.tt @@ -1,7 +1,22 @@ [% INCLUDE 'include/breadcrumbs.tt' %]
+ [% loc('ID') %] [% loc('Created') %] [% loc('Title') %] [% loc('User') %] @@ -28,6 +29,7 @@ [% FOREACH bc IN bookcovers %] + [% bc.bookcover_id | html %] [% bc.created | html %] [% bc.title | html %]