diff options
author | M Somerville <matthew-github@dracos.co.uk> | 2020-08-07 20:02:17 +0100 |
---|---|---|
committer | M Somerville <matthew-github@dracos.co.uk> | 2020-08-11 14:00:46 +0100 |
commit | e461de75b26e74c0d8c154a1a17d6019c2be30dd (patch) | |
tree | 51c70d22931a7d34cddcf909d850d0af3397c48e /perllib/FixMyStreet/Reporting.pm | |
parent | b4d322bf40f88dbab1717e7620178b3641ecd3fa (diff) |
Offline process for CSV generation.
Include a status page, the option for access token requests to use this
system, and a script for manual generation.
Diffstat (limited to 'perllib/FixMyStreet/Reporting.pm')
-rw-r--r-- | perllib/FixMyStreet/Reporting.pm | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/Reporting.pm b/perllib/FixMyStreet/Reporting.pm index 7ee9fc41a..08cdf1544 100644 --- a/perllib/FixMyStreet/Reporting.pm +++ b/perllib/FixMyStreet/Reporting.pm @@ -2,6 +2,7 @@ package FixMyStreet::Reporting; use DateTime; use Moo; +use Path::Tiny; use Text::CSV; use Types::Standard qw(ArrayRef CodeRef Enum HashRef InstanceOf Int Maybe Str); use FixMyStreet::DB; @@ -305,6 +306,46 @@ sub generate_csv { # Output code +sub cache_dir { + my $self = shift; + + my $cfg = FixMyStreet->config('PHOTO_STORAGE_OPTIONS'); + my $dir = $cfg ? $cfg->{UPLOAD_DIR} : FixMyStreet->config('UPLOAD_DIR'); + $dir = path($dir, "dashboard_csv")->absolute(FixMyStreet->path_to()); + my $subdir = $self->user ? $self->user->id : 0; + $dir = $dir->child($subdir); + $dir->mkpath; + $dir; +} + +sub kick_off_process { + my $self = shift; + + return $self->_process if FixMyStreet->test_mode; + + my $pid = fork; + unless ($pid) { + unless (fork) { + # eval so that it will definitely exit cleanly. Otherwise, an + # exception would turn this grandchild into a zombie app process + eval { $self->_process }; + exit 0; + } + exit 0; + } + waitpid($pid, 0); +} + +sub _process { + my $self = shift; + my $out = path($self->cache_dir, $self->filename . '.csv'); + my $file = path($out . '-part'); + if (!$file->exists) { + $self->generate_csv($file->openw_utf8); + $file->move($out); + } +} + # Outputs relevant CSV HTTP headers, and then streams the CSV sub generate_csv_http { my ($self, $c) = @_; |