| 1 | 
## ------------------------------------------------------------------------- | 
| 2 | 
##  $Id: Jobs.pm,v 1.4 2003/04/18 16:04:03 joko Exp $ | 
| 3 | 
## ------------------------------------------------------------------------- | 
| 4 | 
##  $Log: Jobs.pm,v $ | 
| 5 | 
##  Revision 1.4  2003/04/18 16:04:03  joko | 
| 6 | 
##  new api method: getTaskGroups - returns job groups (just a hash) from some configuration slot | 
| 7 | 
## | 
| 8 | 
##  Revision 1.3  2003/04/04 17:29:49  joko | 
| 9 | 
##  updated signatures: session-based service | 
| 10 | 
##  enabled method protection | 
| 11 | 
## | 
| 12 | 
##  Revision 1.2  2003/03/28 03:07:41  joko | 
| 13 | 
##  minor fix: 'run_cmd' required here | 
| 14 | 
## | 
| 15 | 
##  Revision 1.1  2003/03/27 15:39:55  joko | 
| 16 | 
##  initial commit, jobs -core and -api | 
| 17 | 
## | 
| 18 | 
## ------------------------------------------------------------------------- | 
| 19 | 
 | 
| 20 | 
 | 
| 21 | 
package OEF::YAA::Jobs; | 
| 22 | 
 | 
| 23 | 
use strict; | 
| 24 | 
use warnings; | 
| 25 | 
 | 
| 26 | 
use base qw( DesignPattern::Object ); | 
| 27 | 
use base qw( OEF::API::Abstract ); | 
| 28 | 
 | 
| 29 | 
 | 
| 30 | 
use Data::Dumper; | 
| 31 | 
#use Data::Mungle::Transform::Deep qw( merge_to expand ); | 
| 32 | 
use shortcuts qw( run_cmd now ); | 
| 33 | 
 | 
| 34 | 
# get logger instance | 
| 35 | 
my $logger = Log::Dispatch::Config->instance; | 
| 36 | 
 | 
| 37 | 
 | 
| 38 | 
#my $bizWorks = $main::bizWorks; | 
| 39 | 
my $bizProcess = $main::bizProcess; | 
| 40 | 
#my $process = $main::bizProcess; | 
| 41 | 
 | 
| 42 | 
#print Dumper($bizProcess); | 
| 43 | 
#push @{$process->{app}->{use_databases}}, 'control'; | 
| 44 | 
#my $boot = $main::boot; | 
| 45 | 
#$boot->_bootDatabases(); | 
| 46 | 
 | 
| 47 | 
#our ($logger, $bizWorks, $bizProcess, $process, $boot); | 
| 48 | 
 | 
| 49 | 
  # ============================== | 
| 50 | 
  # api-method-declarations | 
| 51 | 
  # ============================== | 
| 52 | 
  # remote method declaration | 
| 53 | 
 | 
| 54 | 
  sub _api_init { | 
| 55 | 
    my $self = shift; | 
| 56 | 
    #return; | 
| 57 | 
    my $procs = [ | 
| 58 | 
      { | 
| 59 | 
        name => "getTaskList", | 
| 60 | 
        version => "0.03", | 
| 61 | 
        hidden => 0, | 
| 62 | 
        code => \&getTaskList, | 
| 63 | 
        signature => [ | 
| 64 | 
          'struct struct',             # w/o session | 
| 65 | 
          'struct struct struct',    # session initialized (a struct containing a key 'RPCSESSID' will get propagated!) | 
| 66 | 
        ], | 
| 67 | 
        help => "", | 
| 68 | 
        protected => 1, | 
| 69 | 
      }, | 
| 70 | 
      { | 
| 71 | 
        name => "getTaskInfo", | 
| 72 | 
        version => "0.03", | 
| 73 | 
        hidden => 0, | 
| 74 | 
        code => \&getTaskInfo, | 
| 75 | 
        signature => [ 'struct string', 'struct string struct' ], | 
| 76 | 
        help => "", | 
| 77 | 
        protected => 1, | 
| 78 | 
      }, | 
| 79 | 
      { | 
| 80 | 
        name => "runTask", | 
| 81 | 
        version => "0.03", | 
| 82 | 
        hidden => 0, | 
| 83 | 
        code => \&runTask, | 
| 84 | 
        #signature => [ 'string string', 'string string struct' ], | 
| 85 | 
        signature => [ 'struct string', 'struct string struct' ], | 
| 86 | 
        help => "", | 
| 87 | 
        protected => 1, | 
| 88 | 
      }, | 
| 89 | 
      { | 
| 90 | 
        name => "getTaskGroups", | 
| 91 | 
        version => "0.03", | 
| 92 | 
        hidden => 0, | 
| 93 | 
        code => \&getTaskGroups, | 
| 94 | 
        signature => [ 'struct', 'struct struct' ], | 
| 95 | 
        help => "", | 
| 96 | 
        protected => 0, | 
| 97 | 
      }, | 
| 98 | 
    ]; | 
| 99 | 
    $self->register($procs); | 
| 100 | 
  }   | 
| 101 | 
 | 
| 102 | 
 | 
| 103 | 
  # ============================== | 
| 104 | 
  # methods | 
| 105 | 
 | 
| 106 | 
 | 
| 107 | 
  sub getTaskList { | 
| 108 | 
    my $srv = shift; | 
| 109 | 
    my $options = shift; | 
| 110 | 
     | 
| 111 | 
    #print Dumper($options); | 
| 112 | 
     | 
| 113 | 
    # XPath query expression (to be calculated here) | 
| 114 | 
    my $xpq = ''; | 
| 115 | 
 | 
| 116 | 
    #print Dumper($bizProcess); | 
| 117 | 
    my $file = $bizProcess->{app}->{storage}->{control}->{locator}->{files}->{jobs}; | 
| 118 | 
     | 
| 119 | 
    if ($options->{filter}) { | 
| 120 | 
     | 
| 121 | 
      my $crit_key = $options->{filter}; | 
| 122 | 
      $crit_key ||= ''; | 
| 123 | 
   | 
| 124 | 
      #$logger->info( __PACKAGE__ . "->getTaskList( crit_key|filter=$crit_key )" ); | 
| 125 | 
   | 
| 126 | 
      # build xpath query database | 
| 127 | 
        my $xpq_db = { | 
| 128 | 
          'all-jobs-unfiltered' => '*/*', | 
| 129 | 
        }; | 
| 130 | 
        my $crit_db = { | 
| 131 | 
          'all-jobs' => '/*', | 
| 132 | 
          'to-backend' => '@source="backend" and (@action="import" or @action="load")', | 
| 133 | 
          'to-frontend' => '@target="frontend"', | 
| 134 | 
        }; | 
| 135 | 
        $crit_db->{'about-import'} = "not ($crit_db->{'to-backend'} or $crit_db->{'to-frontend'})"; | 
| 136 | 
   | 
| 137 | 
      # select xpath query by criteria | 
| 138 | 
        #my $xpq = ''; | 
| 139 | 
        if ($crit_key) { | 
| 140 | 
          if ($xpq_db->{$crit_key}) { | 
| 141 | 
            $xpq = $xpq_db->{$crit_key}; | 
| 142 | 
          } else { | 
| 143 | 
            my $crit = $crit_db->{$crit_key}; | 
| 144 | 
            if ($crit) { | 
| 145 | 
              $xpq = "*/target[*/*[$crit]]"; | 
| 146 | 
            } | 
| 147 | 
          } | 
| 148 | 
      } | 
| 149 | 
       | 
| 150 | 
    } elsif (my $query = $options->{query}) { | 
| 151 | 
      #$logger->info( __PACKAGE__ . "->getTaskList( query=$query )" ); | 
| 152 | 
      $xpq = $query; | 
| 153 | 
      #$xpq = '*/target[@name="dummy" or @name="' . $query . '"]'; | 
| 154 | 
      $xpq = '*/target[@name="dummy" or contains(@name, "' . $query . '")]'; | 
| 155 | 
    } | 
| 156 | 
 | 
| 157 | 
    #$xpq = '*/target[@name="transfer process dev to frontend"]'; | 
| 158 | 
    #$xpq = '/*[@name="transfer process dev to frontend"]'; | 
| 159 | 
    #my $crit = '/*[@name="transfer process dev to frontend"]'; | 
| 160 | 
    #my $crit = '@name="transfer process dev to frontend"'; | 
| 161 | 
    #$xpq = "*/target[*/*[$crit]]"; | 
| 162 | 
    #$xpq = "*/target[*/*[\@source=\"expekt\"]]"; | 
| 163 | 
    #$xpq = "*/target[*/*[\@target=\"dev\" or \@name=\"dummy\"]]"; | 
| 164 | 
     | 
| 165 | 
    # issue the query | 
| 166 | 
      $logger->info( __PACKAGE__ . "->getTaskList( xpq=$xpq )" ); | 
| 167 | 
      my $mdbe = DesignPattern::Object->fromPackage('Data::Storage::Handler::XML', filename => $file ); | 
| 168 | 
      $mdbe->sendQuery($xpq); | 
| 169 | 
      $mdbe->circumflex('result');   | 
| 170 | 
      my $tasks = $mdbe->toSimpleTree(); | 
| 171 | 
 | 
| 172 | 
    #print Dumper($tasks); | 
| 173 | 
 | 
| 174 | 
    return $tasks; | 
| 175 | 
  } | 
| 176 | 
 | 
| 177 | 
 | 
| 178 | 
  sub _getTaskByName { | 
| 179 | 
    #my $self = shift; | 
| 180 | 
    my $name = shift; | 
| 181 | 
 | 
| 182 | 
    #print "getTaskByName!!!!!", "\n"; | 
| 183 | 
 | 
| 184 | 
    $logger->info( __PACKAGE__ . "->getTaskByName( name $name )" ); | 
| 185 | 
 | 
| 186 | 
    my $file = $bizProcess->{app}->{storage}->{control}->{locator}->{files}->{jobs}; | 
| 187 | 
 | 
| 188 | 
    # issue the query | 
| 189 | 
      my $xpq = "*/target[\@name=\"$name\"]"; | 
| 190 | 
      my $mdbe = DesignPattern::Object->fromPackage('Data::Storage::Handler::XML', filename => $file ); | 
| 191 | 
      $mdbe->sendQuery($xpq); | 
| 192 | 
      my $task = $mdbe->toSimpleTree(); | 
| 193 | 
    return $task; | 
| 194 | 
 | 
| 195 | 
  } | 
| 196 | 
 | 
| 197 | 
  sub getTaskInfo { | 
| 198 | 
    my $self = shift; | 
| 199 | 
    my $crit = shift; | 
| 200 | 
    $crit ||= ''; | 
| 201 | 
 | 
| 202 | 
    $logger->info( __PACKAGE__ . "->getTaskInfo( crit=$crit )" ); | 
| 203 | 
 | 
| 204 | 
    my $task = _getTaskByName($crit); | 
| 205 | 
 | 
| 206 | 
    my $jobname = $task->{name}; | 
| 207 | 
 | 
| 208 | 
=pod | 
| 209 | 
    my $status = { | 
| 210 | 
      metadata => {  | 
| 211 | 
        description => $task->{description}, | 
| 212 | 
      }, | 
| 213 | 
      jobstatus => { | 
| 214 | 
        running => $self->{_pcontrol}->{$jobname}->{running}, | 
| 215 | 
        locked => $self->{_pcontrol}->{$jobname}->{locked}, | 
| 216 | 
      }, | 
| 217 | 
    }; | 
| 218 | 
=cut | 
| 219 | 
 | 
| 220 | 
    # enrich pure task metadata with job status information | 
| 221 | 
    $task->{status} = { | 
| 222 | 
      running => $self->{_pcontrol}->{$jobname}->{running}, | 
| 223 | 
      locked => $self->{_pcontrol}->{$jobname}->{locked}, | 
| 224 | 
    }; | 
| 225 | 
 | 
| 226 | 
    return $task; | 
| 227 | 
  } | 
| 228 | 
 | 
| 229 | 
 | 
| 230 | 
  sub runTask { | 
| 231 | 
    my $self = shift; | 
| 232 | 
    my $crit = shift; | 
| 233 | 
    $crit ||= ''; | 
| 234 | 
 | 
| 235 | 
    $logger->info( __PACKAGE__ . "->runTask( crit $crit )" ); | 
| 236 | 
 | 
| 237 | 
    #print "getTask - 1", "\n"; | 
| 238 | 
    my $task = _getTaskByName($crit); | 
| 239 | 
    #print "getTask - 1.1", "\n"; | 
| 240 | 
 | 
| 241 | 
    #print Dumper($task); | 
| 242 | 
    #print Dumper($self); | 
| 243 | 
 | 
| 244 | 
    # build command | 
| 245 | 
    my $rap = "rap.pl"; | 
| 246 | 
    #my $rap = 'C:\home\amo\develop\netfrag.org\nfo\perl\scripts\shortcuts\rap.pl'; | 
| 247 | 
    my $jobname = $task->{name}; | 
| 248 | 
    my $command = "$rap $jobname"; | 
| 249 | 
 | 
| 250 | 
    # run command | 
| 251 | 
    # FIXME: what about asynchronous job execution? | 
| 252 | 
    $self->{_pcontrol}->{$jobname}->{locked} = 1; | 
| 253 | 
    $self->{_pcontrol}->{$jobname}->{running} = 1; | 
| 254 | 
 | 
| 255 | 
    # new of 2003-05-11: USE_PATH and async | 
| 256 | 
    #run_cmd($command, "Executed from " . __PACKAGE__ . " at " . now(), { USE_PATH => 1 }); | 
| 257 | 
    run_cmd($command, "Executed from " . __PACKAGE__ . " at " . now(), { USE_PATH => 1, async => 1 }); | 
| 258 | 
 | 
| 259 | 
=pod | 
| 260 | 
        #system($command); | 
| 261 | 
        my $base = $bizProcess->{app}->{config}->{paths}->{base}; | 
| 262 | 
        $command = "$base/bin/$command"; | 
| 263 | 
        print "command: $command", "\n"; | 
| 264 | 
        my $in; my $out; my $err; | 
| 265 | 
        #run $command, "", \$out, \$err or print "Could not execute $command.", "\n"; | 
| 266 | 
        #my $result = run $command, '>&' or print "Could not execute $command.", "\n"; | 
| 267 | 
        my $result = run $command, '2>&1' or print "Could not execute $command.", "\n"; | 
| 268 | 
        #print Dumper($in, $out, $err); | 
| 269 | 
        print "result: $result", "\n"; | 
| 270 | 
=cut | 
| 271 | 
 | 
| 272 | 
    # FIXME: How to determine when execution has finished? | 
| 273 | 
    # Implement some dot.pid-mechanism? | 
| 274 | 
    # Possible solution: | 
| 275 | 
    # a) Spawn a helper process monitoring the long-running one. | 
| 276 | 
    #     This one notifies the system when execution has finished and terminates itself afterwards. | 
| 277 | 
    # b) Have a dedicated process monitoring service running which gets notified of process startup and shutdown. | 
| 278 | 
    #$self->{_pcontrol}->{$jobname}->{locked} = 0; | 
| 279 | 
    #$self->{_pcontrol}->{$jobname}->{running} = 0; | 
| 280 | 
 | 
| 281 | 
    return 1; | 
| 282 | 
    #return $task; | 
| 283 | 
     | 
| 284 | 
  } | 
| 285 | 
 | 
| 286 | 
 | 
| 287 | 
  sub getTaskGroups { | 
| 288 | 
    my $self = shift; | 
| 289 | 
 | 
| 290 | 
    $logger->info( __PACKAGE__ . "->getTaskGroups()" ); | 
| 291 | 
     | 
| 292 | 
    # emulate a row based result | 
| 293 | 
    my $jobgroups = $bizProcess->{app}->{config}->{jobs}->{groups}; | 
| 294 | 
     | 
| 295 | 
    return $jobgroups; | 
| 296 | 
     | 
| 297 | 
  } | 
| 298 | 
 | 
| 299 | 
 | 
| 300 | 
1; | 
| 301 | 
__END__ |