]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/offline/offline-execute.pl
batch of scripts for handling the processing of offline content
[Evergreen.git] / Open-ILS / src / offline / offline-execute.pl
1 #!/usr/bin/perl
2 use strict; use warnings;
3
4 # --------------------------------------------------------------------
5 # Loads the offline script files for a given org, sorts and runs the 
6 # scripts, and returns the exception list
7 # --------------------------------------------------------------------
8
9 our $U;
10 our %config;
11 our $cgi;
12 our $base_dir;
13 our $logger;
14 my @data;
15 require 'offline-lib.pl';
16
17 my $org = $cgi->param('org');
18 my $resp = &process_data( &sort_data( &collect_data() ) );
19 &archive_files();
20 handle_success("Scripts for org $org processed successfully <br/>" . JSON->perl2JSON($resp) );
21
22
23
24 # --------------------------------------------------------------------
25 # Collects all of the script logs into an in-memory structure that
26 # can be sorted, etc.
27 # --------------------------------------------------------------------
28 sub collect_data {
29
30         handle_error("Org is not defined") unless $org;
31         my $dir = get_pending_dir($org);
32         handle_error("Batch from org $org is already in process") if (-e "$dir/lock");
33
34         # Lock the pending directory
35         system(("touch",  "$dir/lock")) == 0 or handle_error("Unable to create lock file");
36
37         # Load the data from the files
38         my $file;
39         my @data;
40
41         while( ($file = <$dir/*.log>) ) {
42                 $logger->debug("offline: Loading script file $file");
43                 open(F, $file) or handle_error("Unable to open script file $file");
44                 push(@data, <F>);
45         }
46
47         return \@data;
48 }
49
50
51 # --------------------------------------------------------------------
52 # Sorts the commands
53 # --------------------------------------------------------------------
54 sub sort_data {
55         my $data = shift;
56         $logger->debug("offline: Sorting data");
57         return $data;
58 }
59
60
61 # --------------------------------------------------------------------
62 # Runs the commands and returns the list of errors
63 # --------------------------------------------------------------------
64 sub process_data {
65         my $data = shift;
66         my $resp = [];
67         for my $d (@$data) {
68                 $logger->activity("offline: Executing command $d");
69         }
70         return $resp;
71 }
72
73 # --------------------------------------------------------------------
74 # Moves the script files from the pending directory to the archive dir
75 # --------------------------------------------------------------------
76 sub archive_files {
77         my $archivedir = create_archive_dir($org);
78         my $pendingdir = get_pending_dir($org);
79         my $err = "Error moving offline logs from $pendingdir to $archivedir";
80         my @files = <$pendingdir/*.log>;
81
82         system( ("rm", "$pendingdir/lock") ) == 0 or handle_error($err);
83         system( ("mv", "@files", "$archivedir") ) == 0 or handle_error($err);
84         system( ("rmdir", "$pendingdir") ) == 0 or handle_error($err);
85         $logger->debug("offline: Archiving files to $archivedir");
86 }
87
88