1 |
## ------------------------------------------------------------------------- |
2 |
## $Id: Jobs.pm,v 1.1 2003/03/27 15:39:55 joko Exp $ |
3 |
## ------------------------------------------------------------------------- |
4 |
## $Log: Jobs.pm,v $ |
5 |
## Revision 1.1 2003/03/27 15:39:55 joko |
6 |
## initial commit, jobs -core and -api |
7 |
## |
8 |
## ------------------------------------------------------------------------- |
9 |
|
10 |
|
11 |
package OEF::YAA::Jobs; |
12 |
|
13 |
use strict; |
14 |
use warnings; |
15 |
|
16 |
use base qw( DesignPattern::Object ); |
17 |
use base qw( OEF::API::Abstract ); |
18 |
|
19 |
|
20 |
#use Data::Dumper; |
21 |
#use Data::Mungle::Transform::Deep qw( merge_to expand ); |
22 |
use shortcuts qw( run_cmd ); |
23 |
|
24 |
# get logger instance |
25 |
my $logger = Log::Dispatch::Config->instance; |
26 |
|
27 |
|
28 |
#my $bizWorks = $main::bizWorks; |
29 |
my $bizProcess = $main::bizProcess; |
30 |
#my $process = $main::bizProcess; |
31 |
|
32 |
#print Dumper($bizProcess); |
33 |
#push @{$process->{app}->{use_databases}}, 'control'; |
34 |
#my $boot = $main::boot; |
35 |
#$boot->_bootDatabases(); |
36 |
|
37 |
#our ($logger, $bizWorks, $bizProcess, $process, $boot); |
38 |
|
39 |
# ============================== |
40 |
# api-method-declarations |
41 |
# ============================== |
42 |
# remote method declaration |
43 |
|
44 |
sub _api_init { |
45 |
my $self = shift; |
46 |
#return; |
47 |
my $procs = [ |
48 |
{ |
49 |
name => "getTaskList", |
50 |
version => "0.02", |
51 |
hidden => 0, |
52 |
code => \&getTaskList, |
53 |
signature => [ 'struct string' ], |
54 |
help => "", |
55 |
}, |
56 |
{ |
57 |
name => "getTaskInfo", |
58 |
version => "0.02", |
59 |
hidden => 0, |
60 |
code => \&getTaskInfo, |
61 |
signature => [ 'struct string' ], |
62 |
help => "", |
63 |
}, |
64 |
{ |
65 |
name => "runTask", |
66 |
version => "0.02", |
67 |
hidden => 0, |
68 |
code => \&runTask, |
69 |
signature => [ 'string string' ], |
70 |
help => "", |
71 |
}, |
72 |
]; |
73 |
$self->register($procs); |
74 |
} |
75 |
|
76 |
|
77 |
# ============================== |
78 |
# methods |
79 |
|
80 |
|
81 |
sub getTaskList { |
82 |
my $srv = shift; |
83 |
my $crit_key = shift; |
84 |
$crit_key ||= ''; |
85 |
|
86 |
$logger->info( __PACKAGE__ . "->getTaskList( crit_key $crit_key )" ); |
87 |
|
88 |
#print Dumper($bizProcess); |
89 |
|
90 |
my $file = $bizProcess->{app}->{storage}->{control}->{locator}->{files}->{jobs}; |
91 |
|
92 |
# build xpath query database |
93 |
my $xpq_db = { |
94 |
'all-jobs-unfiltered' => '*/*', |
95 |
}; |
96 |
my $crit_db = { |
97 |
'all-jobs' => '/*', |
98 |
'to-backend' => '@source="backend" and (@action="import" or @action="load")', |
99 |
'to-frontend' => '@target="frontend"', |
100 |
}; |
101 |
$crit_db->{'about-import'} = "not ($crit_db->{'to-backend'} or $crit_db->{'to-frontend'})"; |
102 |
|
103 |
# select xpath query by criteria |
104 |
my $xpq = ''; |
105 |
if ($crit_key) { |
106 |
if ($xpq_db->{$crit_key}) { |
107 |
$xpq = $xpq_db->{$crit_key}; |
108 |
} else { |
109 |
my $crit = $crit_db->{$crit_key}; |
110 |
if ($crit) { |
111 |
$xpq = "*/target[*/*[$crit]]"; |
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
# issue the query |
117 |
my $mdbe = DesignPattern::Object->fromPackage('Data::Storage::Handler::XML', filename => $file ); |
118 |
$mdbe->sendQuery($xpq); |
119 |
$mdbe->circumflex('result'); |
120 |
my $tasks = $mdbe->toSimpleTree(); |
121 |
|
122 |
#print Dumper($tasks); |
123 |
|
124 |
return $tasks; |
125 |
} |
126 |
|
127 |
|
128 |
sub _getTaskByName { |
129 |
#my $self = shift; |
130 |
my $name = shift; |
131 |
|
132 |
#print "getTaskByName!!!!!", "\n"; |
133 |
|
134 |
$logger->info( __PACKAGE__ . "->getTaskByName( name $name )" ); |
135 |
|
136 |
my $file = $bizProcess->{app}->{storage}->{control}->{locator}->{files}->{jobs}; |
137 |
|
138 |
# issue the query |
139 |
my $xpq = "*/target[\@name=\"$name\"]"; |
140 |
my $mdbe = DesignPattern::Object->fromPackage('Data::Storage::Handler::XML', filename => $file ); |
141 |
$mdbe->sendQuery($xpq); |
142 |
my $task = $mdbe->toSimpleTree(); |
143 |
return $task; |
144 |
|
145 |
} |
146 |
|
147 |
sub getTaskInfo { |
148 |
my $self = shift; |
149 |
my $crit = shift; |
150 |
$crit ||= ''; |
151 |
|
152 |
$logger->info( __PACKAGE__ . "->getTaskInfo( crit $crit )" ); |
153 |
|
154 |
my $task = _getTaskByName($crit); |
155 |
#print Dumper($tasks); |
156 |
return $task; |
157 |
|
158 |
my $jobname = $task->{name}; |
159 |
|
160 |
my $status = { |
161 |
metadata => { |
162 |
description => $task->{description}, |
163 |
}, |
164 |
jobstatus => { |
165 |
running => $self->{_pcontrol}->{$jobname}->{running}, |
166 |
locked => $self->{_pcontrol}->{$jobname}->{locked}, |
167 |
}, |
168 |
}; |
169 |
|
170 |
return $status; |
171 |
} |
172 |
|
173 |
|
174 |
sub runTask { |
175 |
my $self = shift; |
176 |
my $crit = shift; |
177 |
$crit ||= ''; |
178 |
|
179 |
$logger->info( __PACKAGE__ . "->runTask( crit $crit )" ); |
180 |
|
181 |
#print "getTask - 1", "\n"; |
182 |
my $task = _getTaskByName($crit); |
183 |
#print "getTask - 1.1", "\n"; |
184 |
|
185 |
#print Dumper($task); |
186 |
|
187 |
# build command |
188 |
my $jobname = $task->{name}; |
189 |
my $command = "rap.pl $jobname"; |
190 |
|
191 |
# run |
192 |
# FIXME: what about asynchronous job execution? |
193 |
$self->{_pcontrol}->{$jobname}->{locked} = 1; |
194 |
$self->{_pcontrol}->{$jobname}->{running} = 1; |
195 |
|
196 |
run_cmd($command); |
197 |
|
198 |
=pod |
199 |
#system($command); |
200 |
my $base = $bizProcess->{app}->{config}->{paths}->{base}; |
201 |
$command = "$base/bin/$command"; |
202 |
print "command: $command", "\n"; |
203 |
my $in; my $out; my $err; |
204 |
#run $command, "", \$out, \$err or print "Could not execute $command.", "\n"; |
205 |
#my $result = run $command, '>&' or print "Could not execute $command.", "\n"; |
206 |
my $result = run $command, '2>&1' or print "Could not execute $command.", "\n"; |
207 |
#print Dumper($in, $out, $err); |
208 |
print "result: $result", "\n"; |
209 |
=cut |
210 |
|
211 |
$self->{_pcontrol}->{$jobname}->{locked} = 0; |
212 |
$self->{_pcontrol}->{$jobname}->{running} = 0; |
213 |
|
214 |
return 1; |
215 |
} |
216 |
|
217 |
|
218 |
|
219 |
|
220 |
1; |
221 |
__END__ |