Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A reimport task got suddenly quite useful over the weekend #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/Cavil.pm
Original file line number Diff line number Diff line change
@@ -130,7 +130,8 @@ sub startup {
state $pkgs = Cavil::Model::Packages->new(
checkout_dir => $config->{checkout_dir},
minion => $self->minion,
pg => shift->pg
pg => shift->pg,
log => $self->log
);
}
);
64 changes: 61 additions & 3 deletions lib/Cavil/Model/Packages.pm
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ use Mojo::Base -base;
use Mojo::File 'path';
use Mojo::Util 'dumper';

has [qw(checkout_dir minion pg)];
has [qw(checkout_dir minion pg log)];

sub add {
my ($self, %args) = @_;
@@ -228,8 +228,8 @@ sub reindex {
$db->delete('bot_reports', {package => $id});

my $unpacked = path(
$self->{checkout_dir}, $pkg->{name},
$pkg->{checkout_dir}, '.postprocessed.json'
$self->checkout_dir, $pkg->{name},
$pkg->{checkout_dir}, '.postprocessed.json'
);
-f $unpacked ? $self->index($id, $priority) : $self->unpack($id, $priority);

@@ -301,4 +301,62 @@ sub _enqueue {
);
}

sub cleanup {
my ($self, $id) = @_;

my $db = $self->pg->db;
return
unless my $pkg
= $db->select('bot_packages', ['name', 'checkout_dir'], {id => $id})->hash;

my $tx = $db->begin;
$db->query('delete from bot_reports where package = ?', $id);
$db->query('delete from emails where package = ?', $id);
$db->query('delete from urls where package = ?', $id);
$db->query('delete from pattern_matches where package = ?', $id);
$db->query('delete from matched_files where package = ?', $id);
$tx->commit;

my $dir = path($self->checkout_dir, $pkg->{name}, $pkg->{checkout_dir});
return unless -d $dir;

$self->log->info("[$id] Remove $pkg->{name}/$pkg->{checkout_dir}");
$dir->remove_tree;
}

sub reimport {
my ($self, $id) = @_;
my $db = $self->pg->db;

my $pkg = $self->find($id);
my $source = $db->select('bot_sources', '*', {id => $pkg->{source}})->hash;

my $priority;
for my $opkg (
@{
$db->select('bot_packages', 'id,priority',
{name => $pkg->{name}, id => {'!=', $pkg->{id}}, state => 'new'})
->hashes
}
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my $results = $db->select(
  'bot_packages',
  ['id', 'priority'],
  {name => $pkg->{name}, id => {'!=', $pkg->{id}}, state => 'new'}
)->hashes;
for my $opkg (@$results) {

{
$priority = 2;
}
$priority = $pkg->{priority} if $pkg->{state} eq 'new';
my $job = $self->obs_import(
$pkg->{id},
{
api => $source->{api_url},
project => $source->{project},
pkg => $source->{package},
srcpkg => $pkg->{name},
srcmd5 => $source->{srcmd5},
verifymd5 => $pkg->{checkout_dir},
priority => $priority // 1
},
9
);
return $job;
}

1;
21 changes: 1 addition & 20 deletions lib/Cavil/Task/Cleanup.pm
Original file line number Diff line number Diff line change
@@ -49,26 +49,7 @@ sub _cleanup_batch {
my $log = $app->log;
my $db = $app->pg->db;

for my $id (@ids) {
next
unless my $pkg
= $db->select('bot_packages', ['name', 'checkout_dir'], {id => $id})
->hash;

my $dir
= path($app->config->{checkout_dir}, $pkg->{name}, $pkg->{checkout_dir});
next unless -d $dir;

$log->info("[$id] Remove $pkg->{name}/$pkg->{checkout_dir}");
my $tx = $db->begin;
$db->query('delete from bot_reports where package = ?', $id);
$db->query('delete from emails where package = ?', $id);
$db->query('delete from urls where package = ?', $id);
$db->query('delete from pattern_matches where package = ?', $id);
$db->query('delete from matched_files where package = ?', $id);
$tx->commit;
$dir->remove_tree;
}
map { $app->packages->cleanup($_) } @ids;
}

sub _obsolete {
14 changes: 14 additions & 0 deletions lib/Cavil/Task/Import.pm
Original file line number Diff line number Diff line change
@@ -22,6 +22,19 @@ use Mojo::File 'path';
sub register {
my ($self, $app) = @_;
$app->minion->add_task(obs_import => \&_obs);
$app->minion->add_task(reimport => \&_reimport);
}

sub _reimport {
my ($job, $id) = @_;

my $app = $job->app;
my $log = $app->log;

$app->packages->cleanup($id);
$app->pg->db->update('bot_packages', {indexed => undef}, {id => $id});
$app->pg->db->delete('bot_reports', {package => $id});
$app->packages->reimport($id);
}

sub _obs {
@@ -33,6 +46,7 @@ sub _obs {
my $checkout_dir = $app->config->{checkout_dir};
my ($srcpkg, $verifymd5, $api, $project, $pkg, $srcmd5, $priority)
= @{$data}{qw(srcpkg verifymd5 api project pkg srcmd5 priority)};
die "Missing args" unless $srcpkg && $verifymd5;
my $dir = path($checkout_dir, $srcpkg, $verifymd5);

my $obs = $app->obs;