Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 3 files changed
  • 0 comments
  • 1 contributor
76 lib/LIMS2/Report.pm
@@ -17,11 +17,70 @@ use Scalar::Util qw( blessed );
17 17 use Fcntl qw( :DEFAULT :flock );
18 18 use LIMS2::Exception::System;
19 19 use LIMS2::Exception::Validation;
  20 +use Path::Class;
  21 +use Const::Fast;
  22 +use DateTime;
  23 +use DateTime::Duration;
  24 +
  25 +const my $WORK_DIR => dir( $ENV{LIMS2_REPORT_DIR} || '/var/tmp/lims2-reports' );
  26 +const my $CACHE_FILE_TTL => DateTime::Duration->new( days => 3 );
  27 +
  28 +sub clean_cache {
  29 + my ( $model, $verbose ) = @_;
  30 +
  31 + INFO( "Deleting expired entries from cached_reports table" );
  32 + $model->schema->resultset('CachedReport')->search_rs(
  33 + {
  34 + expires => { '<=', \'current_timestamp' }
  35 + }
  36 + )->delete;
  37 +
  38 + my $cutoff = DateTime->now() - $CACHE_FILE_TTL;
  39 + INFO( "Deleting cache files older than $cutoff" );
  40 + for my $d ( grep { $_->is_dir } $WORK_DIR->children ) {
  41 + if ( $d->stat->mtime <= $cutoff->epoch ) {
  42 + $d->rmtree($verbose);
  43 + }
  44 + }
  45 +
  46 + return;
  47 +}
  48 +
  49 +sub get_report_status {
  50 + my $report_id = shift;
  51 +
  52 + my $work_dir = $WORK_DIR->subdir( $report_id );
  53 +
  54 + unless ( $work_dir->stat and -d _ ) {
  55 + return 'NOT_FOUND';
  56 + }
  57 +
  58 + if ( $work_dir->file( 'done' )->stat ) {
  59 + return 'DONE';
  60 + }
  61 +
  62 + if ( $work_dir->file( 'failed' )->stat ) {
  63 + return 'FAILED';
  64 + }
  65 +
  66 + return 'PENDING';
  67 +}
  68 +
  69 +sub read_report_from_disk {
  70 + my ( $report_id ) = @_;
  71 +
  72 + my $dir = $WORK_DIR->subdir( $report_id );
  73 +
  74 + my $report_fh = $dir->file( 'report.csv' )->openr;
  75 + my $report_name = $dir->file( 'name' )->slurp;
  76 +
  77 + return ( $report_name, $report_fh );
  78 +}
20 79
21 80 sub _cached_report_ok {
22   - my ( $work_dir, $cache_entry ) = @_;
  81 + my ( $cache_entry ) = @_;
23 82
24   - my $report_dir = $work_dir->subdir( $cache_entry->id );
  83 + my $report_dir = $WORK_DIR->subdir( $cache_entry->id );
25 84 if ( $report_dir->stat && ! $report_dir->file('fail')->stat ) {
26 85 return 1;
27 86 }
@@ -38,7 +97,8 @@ sub cached_report {
38 97 # Take an exclusive lock to avoid race between interrogating table
39 98 # and creating cache row. This ensures we don't set off concurrent
40 99 # cache refreshes for the same report.
41   - my $lock_file = $args{output_dir}->file('lims2.cache.lock');
  100 + $WORK_DIR->mkpath;
  101 + my $lock_file = $WORK_DIR->file('lims2.cache.lock');
42 102 my $lock_fh = $lock_file->open( O_RDWR|O_CREAT, oct(644) )
43 103 or LIMS2::Exception::System->throw( "open $lock_file failed: $!" );
44 104 local $SIG{ALRM} = sub { LIMS2::Exception::System->throw( "Timeout waiting for lock on $lock_file" ) };
@@ -48,7 +108,7 @@ sub cached_report {
48 108 alarm(0);
49 109
50 110 if ( my $in_cache = $generator->cached_report ) {
51   - if ( _cached_report_ok( $args{output_dir}, $in_cache ) ) {
  111 + if ( _cached_report_ok( $in_cache ) ) {
52 112 return $in_cache->id;
53 113 }
54 114 }
@@ -56,7 +116,7 @@ sub cached_report {
56 116 my $cache_entry = $generator->init_cached_report( generate_report_id() );
57 117 $lock_fh->close(); # End of critical code: release the lock
58 118
59   - my $work_dir = init_work_dir( $args{output_dir}, $cache_entry->id );
  119 + my $work_dir = init_work_dir( $cache_entry->id );
60 120 run_in_background( $generator, $work_dir, $cache_entry );
61 121
62 122 return $cache_entry->id;
@@ -85,7 +145,7 @@ sub generate_report {
85 145
86 146 INFO( "Generating $args{report} report $report_id" );
87 147
88   - my $work_dir = init_work_dir( $args{output_dir}, $report_id );
  148 + my $work_dir = init_work_dir( $report_id );
89 149
90 150 if ( $args{async} ) {
91 151 run_in_background( $generator, $work_dir );
@@ -191,9 +251,9 @@ sub load_generator_class {
191 251 }
192 252
193 253 sub init_work_dir {
194   - my ( $output_dir, $report_id ) = @_;
  254 + my ( $report_id ) = @_;
195 255
196   - my $work_dir = dir( $output_dir )->subdir( $report_id );
  256 + my $work_dir = $WORK_DIR->subdir( $report_id );
197 257 $work_dir->mkpath;
198 258
199 259 return $work_dir;
25 lib/LIMS2/WebApp/Controller/API/Report.pm
... ... @@ -1,17 +1,11 @@
1 1 package LIMS2::WebApp::Controller::API::Report;
2 2 use Moose;
3 3 use MooseX::Types::Path::Class;
  4 +use LIMS2::Report;
4 5 use namespace::autoclean;
5 6
6 7 BEGIN {extends 'LIMS2::Catalyst::Controller::REST'; }
7 8
8   -has report_dir => (
9   - is => 'ro',
10   - isa => 'Path::Class::Dir',
11   - coerce => 1,
12   - required => 1
13   -);
14   -
15 9 =head1 NAME
16 10
17 11 LIMS2::WebApp::Controller::API::Report - Catalyst Controller
@@ -32,21 +26,8 @@ sub report_ready_GET {
32 26
33 27 $c->assert_user_roles( 'read' );
34 28
35   - my $work_dir = $self->report_dir->subdir( $report_id );
36   -
37   - unless ( $work_dir->stat and -d _ ) {
38   - return $self->status_ok( $c, entity => { status => 'NOT_FOUND' } );
39   - }
40   -
41   - if ( $work_dir->file( 'done' )->stat ) {
42   - return $self->status_ok( $c, entity => { status => 'DONE' } );
43   - }
44   -
45   - if ( $work_dir->file( 'failed' )->stat ) {
46   - return $self->status_ok( $c, entity => { status => 'FAILED' } );
47   - }
48   -
49   - return $self->status_ok( $c, entity => { status => 'PENDING' } );
  29 + my $status = LIMS2::Report::get_report_status( $report_id );
  30 + return $self->status_ok( $c, entity => { status => $status } );
50 31 }
51 32
52 33 =head1 AUTHOR
25 lib/LIMS2/WebApp/Controller/User/Report.pm
@@ -8,13 +8,6 @@ use namespace::autoclean;
8 8
9 9 BEGIN {extends 'Catalyst::Controller'; }
10 10
11   -has report_dir => (
12   - is => 'ro',
13   - isa => 'Path::Class::Dir',
14   - coerce => 1,
15   - required => 1
16   -);
17   -
18 11 =head1 NAME
19 12
20 13 LIMS2::WebApp::Controller::User::Report - Catalyst Controller
@@ -45,7 +38,6 @@ sub cached_async_report :Path( '/user/report/cache' ) :Args(1) {
45 38 model => $c->model( 'Golgi' ),
46 39 report => $report,
47 40 params => $params,
48   - output_dir => $self->report_dir
49 41 );
50 42
51 43 $c->stash(
@@ -75,7 +67,6 @@ sub sync_report :Path( '/user/report/sync' ) :Args(1) {
75 67 model => $c->model( 'Golgi' ),
76 68 report => $report,
77 69 params => $params,
78   - output_dir => $self->report_dir,
79 70 async => 0
80 71 );
81 72
@@ -106,7 +97,6 @@ sub async_report :Path( '/user/report/async' ) :Args(1) {
106 97 model => $c->model('Golgi'),
107 98 report => $report,
108 99 params => $params,
109   - output_dir => $self->report_dir,
110 100 async => 1
111 101 );
112 102
@@ -130,7 +120,7 @@ sub download_report :Path( '/user/report/download' ) :Args(1) {
130 120
131 121 $c->assert_user_roles( 'read' );
132 122
133   - my ( $report_name, $report_fh ) = $self->_read_report_from_disk( $report_id );
  123 + my ( $report_name, $report_fh ) = LIMS2::Report::read_report_from_disk( $report_id );
134 124
135 125 $c->response->status( 200 );
136 126 $c->response->content_type( 'text/csv' );
@@ -150,7 +140,7 @@ sub view_report :Path( '/user/report/view' ) :Args(1) {
150 140
151 141 $c->assert_user_roles( 'read' );
152 142
153   - my ( $report_name, $report_fh ) = $self->_read_report_from_disk( $report_id );
  143 + my ( $report_name, $report_fh ) = LIMS2::Report::read_report_from_disk( $report_id );
154 144
155 145 my $pageset = LIMS2::WebApp::Pageset->new(
156 146 {
@@ -202,17 +192,6 @@ sub _count_rows {
202 192 return $count - 1;
203 193 }
204 194
205   -sub _read_report_from_disk {
206   - my ( $self, $report_id ) = @_;
207   -
208   - my $dir = $self->report_dir->subdir( $report_id );
209   -
210   - my $report_fh = $dir->file( 'report.csv' )->openr;
211   - my $report_name = $dir->file( 'name' )->slurp;
212   -
213   - return ( $report_name, $report_fh );
214   -}
215   -
216 195 =head1 AUTHOR
217 196
218 197 Ray Miller

No commit comments for this range

Something went wrong with that request. Please try again.