Data-Downloader

 view release on metacpan or  search on metacpan

lib/Data/Downloader/Repository.pm  view on Meta::CPAN


=item download_all

Parameters  :

    fake (boolean) -- fake the download.

Download all known files associated with this repository.

=cut

sub download_all {
    my $self = shift;
    my %args = @_; # TODO validate (fake =>1)
    for my $file (@{ $self->files }) { # TODO only where not downloaded?
        DEBUG "downloading file : ".$file->filename;
        $file->download(%args);
        for my $datum (@{ $file->metadata }) {
            TRACE "    " . $datum->name . " == " . $datum->value;
        }
    }
}

=item cache

Get a cache object for this repository.  See Data::Downloader::Cache.

=cut

sub cache {
    my $self = shift;
    my $strategy = $self->cache_strategy or return;
    my $cache_class = "Data::Downloader::Cache::$strategy";
    eval "use $cache_class";
    LOGDIE "error using $cache_class : $@" if $@;
    return $cache_class->new(repository => $self);
}

sub _initialize_stats {
    my $self = shift;
    return if $self->stat_info;
    unless ($self->stat_info) {
        $self->stat_info({last_stat_update => undef, last_fsck => undef, repository => $self->id});
        $_->save for $self->stat_info;
    }
}

=item update_stats

Update the stats for this repository, e.g. the atimes, and any
aggregate stats.  Won't update the stats before a specified
interval has elapsed.

Parameters :

    interval -- a Datetime::Duration object or "0" to force an update.
    defaults to one hour.

=cut

sub update_stats {
    my $self = shift;
    my $args = validate(@_, { interval => 0 });
    my $duration = $args->{duration};
    if (!defined($duration)) {
        $duration = DateTime::Duration->new(hours => 1);
    }
    $self->_initialize_stats; # only if necessary
    return if $duration &&
        $self->stat_info->last_stat_update &&
        ($self->stat_info->last_stat_update->add_duration($duration)) > DateTime->now();

    # Also set an advisory lock; only one process should do this.
    my ($lockfile,$lock);
    unless ($self->db->database eq ':memory:') {
        $lockfile = $self->db->database.".dado_stats_lock";
    }

    if ($lockfile) {
        open $lock, ">$lockfile" or do {
            ERROR "cannot write to $lockfile";
            return;
        };
        flock($lock, LOCK_EX) or return;
    }
    DEBUG "updating stats ($$)";
    my $files = Data::Downloader::File::Manager->get_files([on_disk => 1 ]);
    for my $file (@$files) {
        $file->load(speculative => 1) or next;
        my $stat = stat($file->storage_path) or next;
        $file->atime( DateTime->from_epoch(epoch => $stat->atime) );
        $file->save(changes_only => 1) or do {
            ERROR $file->error;
            return;
        };
    }
    $self->stat_info->last_stat_update(DateTime->now());
    $self->stat_info->save or do {
        ERROR $self->stat_info->error;
        return;
    };
    if ($lockfile) {
        flock ($lock, LOCK_UN) or LOGWARN "cannot unlock $lockfile";
    }
}

=item dump_stats

Print statistics about this repository to STDOUT.

=cut

sub dump_stats {
    my $self = shift;
    my $args = validate(@_, {yaml => 0});
    my %stats;

    @stats{qw/known_files/} = $self->db->simple->select(
        'file',
        [ 'count(1)', ],
        { repository => $self->id }



( run in 0.645 second using v1.01-cache-2.11-cpan-39bf76dae61 )