261
248
# Method: _loadModules
263
# Load dinamically the modules which lays on a directory given a
264
# prefix. This could be: 'Watcher' or 'Dispatcher'.
250
# Load installed watchers or dispatchers.
268
# prefix - String the prefix could 'Watcher' or 'Dispatcher'
254
# type - can be 'watcher' or 'dispatcher'
272
my ($self, $prefix) = @_;
274
my ($prefixPath, $registeredField);
275
if ( $prefix eq 'Watcher' ) {
276
$prefixPath = WATCHERS_DIR;
277
$registeredField = 'registeredEvents';
278
} elsif ( $prefix eq 'Dispatcher' ) {
279
$prefixPath = DISPATCHERS_DIR;
280
$registeredField = 'registeredDispatchers';
285
opendir ( my $dir, $prefixPath );
287
while ( defined ( my $file = readdir ( $dir ))) {
288
unless ( -e "$prefixPath/$file" ) {
289
if ( -l "$prefixPath/$file" ) {
290
EBox::info("Unlinking broken link $prefixPath/$file");
291
unlink ( "$prefixPath/$file" )
292
or throw EBox::Exceptions::Internal("Cannot unlink $prefixPath/$file");
258
my ($self, $type) = @_;
260
my $events = EBox::Global->getInstance(1)->modInstance('events');
261
my $model = $type eq 'watcher' ? $events->model('ConfigureWatchers') : $events->model('ConfigureDispatchers');
263
foreach my $id (@{$model->enabledRows()}) {
264
my $row = $model->row($id);
265
my $className = $row->valueByName($type);
267
eval "use $className";
269
EBox::error("Error loading $type class: $className $@");
296
next unless ( $file =~ m/.*\.pm/g );
297
my ($className) = ($file =~ m/(.*)\.pm/);
298
$className = 'EBox::Event::' . $prefix . '::' . $className;
300
# The class may not be included
301
if (not defined ($self->{$registeredField}->{$className})) {
302
eval qq{require "$prefixPath/$file"};
304
EBox::warn("Error loading class: $className $@");
307
EBox::info("$className loaded from $registeredField");
308
if ($prefix eq 'Watcher') {
309
if ($className->isa('EBox::Event::Watcher::Base') and
310
(not ($className eq 'EBox::Event::Watcher::Base')) ) {
311
$instance = $className->new();
312
$self->{$registeredField}->{$className} = {
313
instance => $instance,
317
EBox::info("Class $className not derived from EBox::Event::Watcher::Base");
320
if ($className->isa('EBox::Event::Dispatcher::Abstract') and
321
(not ($className eq 'EBox::Event::Dispatcher::Abstract')) ) {
322
$instance = $className->new();
323
$self->{$registeredField}->{$className} = $instance;
325
EBox::info("Class $className not derived from EBox::Event::Dispatcher::Abstract");
272
my $instance = $className->new();
273
if ($type eq 'watcher') {
274
$self->{watchers}->{$className} = { instance => $instance, deadOut => 0 };
329
# Check its last modification time in order to reload
331
my $statFile = stat ("$prefixPath/$file");
333
if ( $prefix eq 'Watcher' ) {
334
$lastScan = $self->{lastWatcherScan};
336
$lastScan = $self->{lastDispatcherScan};
338
if ( $statFile->mtime() > $lastScan ) {
339
EBox::info("$className reloaded from $registeredField");
340
$self->_deleteFromINC($className);
341
eval qq{require "$prefixPath/$file";};
343
EBox::warn("Error loading class: $className");
346
$instance = $className->new();
347
if ( $prefix eq 'Watcher' ) {
348
my $registeredEvent = $self->{$registeredField}->{$className};
349
$registeredEvent->{instance} = $instance;
350
# If the period has plummered to be lower than
351
# current dead out, set the new period
352
if ( $registeredEvent->{deadOut} > $registeredEvent->{instance}->period() ) {
353
$registeredEvent->{deadOut} = $registeredEvent->{instance}->period();
355
} elsif ($prefix eq 'Dispatcher') {
356
$self->{$registeredField}->{$className} = $instance;
364
foreach my $className ( keys (%{$self->{$registeredField}}) ){
365
my ($fileName) = $className =~ m/.*::(.*)$/g;
367
unless ( -e "$prefixPath/$fileName" ) {
368
EBox::info("$className deleted from $registeredField");
369
$self->_deleteFromINC($className);
370
if ( -l "$prefixPath/$fileName" ) {
371
# Delete broken links
372
EBox::info("Unlinking broken link $prefixPath/$fileName");
373
unlink( "$prefixPath/$fileName" )
374
or throw EBox::Exceptions::Internal("Cannot unlink $prefixPath/$fileName");
378
# unless ( -f ( readlink ( "$prefixPath/$fileName" ))) {
379
# EBox::info("$className deleted from $registeredField since the link is broken");
380
# $self->_deleteFromINC($className);
381
# # Remove broken links
386
if ($prefix eq 'Watcher') {
387
$self->{lastWatcherScan} = time();
389
$self->{lastDispatcherScan} = time();
394
# Method: _deleteFromINC
396
# Delete a class from the loaded modules
400
# className - String the class name in :: format
404
my ($self, $className) = @_;
406
my $pathName = $className;
407
$pathName =~ s/::/\//g;
408
delete $INC{$pathName};
276
$self->{dispatchers}->{$className} = $instance;
411
281
# Method: _dispatchEventByDispatcher
507
my ($year, $mon, $mday, $hour, $min, $sec) = split /[\s\-:]/, $storedEvent->{lasttimestamp};
369
my ($year, $mon, $mday, $hour, $min, $sec) = split /[\s\-:]/, $storedEvent->{lastTimestamp};
510
my $storedTimestamp = timelocal($sec,$min,$hour,$mday,$mon,$year);
372
my $storedTimestamp = timelocal($sec,$min,$hour,$mday,$mon,$year);
512
374
if (($storedTimestamp + EVENT_FOLDING_INTERVAL) > $event->timestamp()) {
513
375
# Last event of the same type happened before last