source: A2P/a2p/A2P/Converter.pm @ 3

Last change on this file since 3 was 3, checked in by guillaume, 16 years ago
  • AUTHORS: Ajout des différents contributeurs
  • COPYING: Ajout de la licence GPL v3
  • a2p: Préparation des sources pour leur publication sous GPL
  • Property svn:keywords set to Id
File size: 5.7 KB
Line 
1#
2# Copyright (c) 2004-2007 - Consultas, PKG.fr
3#
4# This file is part of A2P.
5#
6# A2P is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# A2P is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with A2P; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19#
20# $Id: Converter.pm 3 2007-10-18 16:20:19Z guillaume $
21#
22
23package A2P::Converter;
24
25# Derived class from Thread.pm
26use base qw(A2P::Thread AFPDS::afpds2tex);
27
28use strict ;
29use integer ;
30use Fcntl ':flock';
31use A2P::Globals ;
32use A2P::Syslog ;
33use AFPDS::afpds2tex ;
34use A2P::Com qw( IsCom GetCom comJOB comREQ );
35use A2P::Tools qw( ShortID ) ;
36
37BEGIN {
38    our $VERSION = sprintf "%s", q$Rev: 717 $ =~ /(\d[0-9.]+)\s+/ ;
39}
40our $VERSION ;
41
42my $LCK ;
43my $self ;
44
45################################################################################
46##             Converter code                                                 ##
47################################################################################
48sub Do {
49       $self = shift ;
50    my $ref  = shift ;
51
52    my $Ret = 0 ;
53
54    my @Job = &IsCom( comJOB , $$ref );
55
56    if ( @Job == 2 ) {
57        my ( $Job , $file ) = @Job ;
58
59        # Keep internal stats
60        &UPSTAT('CONV_REQ');
61
62    #--------------------------------------
63    # It's a file to convert by default
64    #--------------------------------------
65        my $startpos = 0 ;
66        # Check if we were ask to start an AFP file at a given position
67        ( $file, $startpos ) = ( $1, $2 ) if ( $file =~ /^(.*):(\d+)$/ );
68
69        &Debug( "Requested to convert '$file' for Job $Job".
70            ( $startpos ? ", beginning at pos $startpos" : "" ) );
71
72        # Get folder without trailing '/'
73        my ( $jobfolder ) = $SHMDIR =~ m|(.*[^/]+)/?$| ;
74        my ( $base ) = $file =~ m|([^/]+)$| ; # Get file basename
75        $jobfolder .= "/$Job-$base" ;
76
77        # Found a free folder to compute AFPDS file
78        my ( $newfolder , $tries ) = ( $jobfolder , 10 );
79        while ( $tries-- ) {
80            $! = 0 ;
81            # Use standard Perl locking as we are working on our filesystem
82            undef $LCK ;
83            open($LCK, '>>', $newfolder.".LCK")
84                or $self->ThisError("Can't open $newfolder.LCK lock file: $!"),
85                   $Ret ++, last ;
86
87            # Try non blocking exclusive lock
88            my $locked = flock($LCK, LOCK_EX | LOCK_NB);
89            &Debug("Can't lock '$newfolder.LCK': $locked, $!") unless $locked ;
90
91            # Don't use this folder name if folder exists or we don't have lock
92            last unless ( ! $locked or -d $newfolder );
93
94            close($LCK);
95
96            $newfolder = $jobfolder . '-' . &ShortID(5) ;
97        }
98        $jobfolder = $newfolder ;
99
100        if ( ! -d $jobfolder ) {
101            &Debug("Starting '$base' conversion in '$jobfolder' for Job $Job");
102
103            # self->{JOB} is necessary to return some informations to JobManager
104            # when conversion thread failed abnormally
105            # (see A2P/Thread.pm, DESTROY sub)
106            $self->{JOB} = $Job ;
107            $Ret = $self->ConvertFile( $jobfolder, $file, $Job, $startpos );
108            $self->{JOB} = "" ;
109
110            &Debug("Conversion of '$base' returned with value $Ret") if $Ret ;
111
112            # Keep internal stats
113            $Ret ? &UPSTAT('BAD_CONV_REQ') : &UPSTAT('GOOD_CONV_REQ') ;
114
115            # Check to handle splitting case
116            if ( !$Ret and
117            defined($self->{AFPDS}) and $self->{AFPDS}->issplitted() ) {
118                # Set splitting event, JobManager handles it as also DONE answer
119                $Ret = $self->{AFPDS}->getEnv ;
120                # Freeing object
121                delete $self->{AFPDS} ;
122            }
123
124            # Now return conversion status
125            $Ret = DONE unless $Ret ;
126            $self->Return( $Job , $Ret );
127            $self->AnswerDone( $Job );
128
129            # LCK file will be purge by JobManager during step 12
130            # This will release the lock on the folder
131
132        } else {
133
134            # Keep internal stats
135            &UPSTAT('BAD_CONV_JOBFOLDER');
136
137            $Ret = 50 ;
138            # Return an error status
139            $self->Return( $Job , $Ret );
140            $self->AnswerDone( $Job );
141        }
142
143    } else {
144
145        # Keep internal stats
146        &UPSTAT('BAD_CONV_REQFORMAT');
147
148        $self->ThisError("Got bad conversion request '$$ref'");
149        $Ret ++ ;
150
151        if (@Job) {
152            # Return exec status even for bad format to not break job processing
153            $self->Return( $Job[0] , $Ret );
154            $self->AnswerDone( $Job[0] );
155        }
156    }
157
158    return $Ret == DONE ? 1 : 0 ;
159}
160
161sub ThreadInit {
162    $self = shift ;
163
164    # Initializes stat headers
165    $self->{STATHEADERS} = [ qw(
166        Date      DayTime   JobID       Jobname     DestId  PageDef
167        FormDef   DocName   PageFormats CopyGroups  Logos   NbPages
168        Type      LockId
169        ) ];
170
171    $self->{HAVE_STATS} = 1 ;
172}
173
174sub END {
175    return unless (defined($self) and defined($self->{JOB}) and $self->{JOB});
176
177    print $self->getName . ": END step: $self->{JOB}\n";
178
179    # Return an error status
180    &Warn("Unexpected abort condition reached on '$self->{JOB}'");
181    $self->Return( $self->{JOB} , 254 );
182    $self->AnswerDone( $self->{JOB} );
183}
184
185&Debug("Module " . __PACKAGE__ . " v$VERSION loaded");
186
1871;
Note: See TracBrowser for help on using the repository browser.