Skip to content

Commit

Permalink
Delete lock before dying
Browse files Browse the repository at this point in the history
  • Loading branch information
dklawren committed Dec 12, 2024
1 parent c139ba4 commit 7d944ee
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions extensions/BMO/bin/export_bmo_etl.pl
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,7 @@
send_data($table_name, \@users, $count) if @users;
}

# Delete lock from bmo_etl_locked
Bugzilla->dbh_main->do('DELETE FROM bmo_etl_locked');
delete_lock();

# Functions

Expand All @@ -546,7 +545,10 @@ sub get_cache {

# First uncompress the JSON and then decode it back to Perl data
my $data;
gunzip \$gzipped_data => \$data or die "gunzip failed: $GunzipError\n";
unless (gunzip \$gzipped_data => \$data) {
delete_lock();
die "gunzip failed: $GunzipError\n";
}
return decode_json($data);
}

Expand All @@ -560,7 +562,10 @@ sub store_cache {

# Compress the JSON to save space in the DB
my $gzipped_data;
gzip \$data => \$gzipped_data or die "gzip failed: $GzipError\n";
unless (gzip \$data => \$gzipped_data) {
delete_lock();
die "gzip failed: $GzipError\n";
}

# We need to use the main DB for write operations
my $main_dbh = Bugzilla->dbh_main;
Expand Down Expand Up @@ -607,7 +612,10 @@ sub send_data {

my $fh = path($filename)->open('>>');
print $fh encode_json($big_query) . "\n";
close $fh || die "Could not close $filename: $!\n";
unless(close $fh) {
delete_lock();
die "Could not close $filename: $!\n";
}

return;
}
Expand All @@ -631,7 +639,8 @@ sub send_data {

my $res = $ua->request($request);
if (!$res->is_success) {
die 'Google Big Query insert failure: ' . $res->content;
delete_lock();
die 'Google Big Query insert failure: ' . $res->content . "\n";
}
}

Expand Down Expand Up @@ -661,7 +670,8 @@ sub _get_access_token {
my $res = $ua->request($request);

if (!$res->is_success) {
die 'Google access token failure: ' . $res->content;
delete_lock();
'Google access token failure: ' . $res->content;
}

my $result = decode_json($res->decoded_content);
Expand All @@ -671,6 +681,8 @@ sub _get_access_token {
return $access_token;
}

# If a previous process is performing an export to BigQuery, then
# we must check the lock table and exit if true.
sub check_and_set_lock {
return if $test; # No need if just dumping test files

Expand All @@ -682,6 +694,11 @@ sub check_and_set_lock {
$dbh_main->do('INSERT INTO bmo_etl_locked VALUES (?)', undef, 'locked');
}

# Delete lock from bmo_etl_locked
sub delete_lock {
Bugzilla->dbh_main->do('DELETE FROM bmo_etl_locked');
}

sub check_for_duplicates {
return if $test; # no need if just dumping test files

Expand Down Expand Up @@ -711,7 +728,8 @@ sub check_for_duplicates {

my $res = $ua->request($request);
if (!$res->is_success) {
die 'Google Big Query query failure: ' . $res->content;
delete_lock();
die 'Google Big Query query failure: ' . $res->content . "\n";
}

my $result = decode_json($res->content);
Expand All @@ -720,6 +738,7 @@ sub check_for_duplicates {

# Do not export if we have any rows with this snapshot date.
if ($row_count) {
delete_lock();
die "Duplicate data found for snapshot date $snapshot_date\n";
}
}
Expand Down

0 comments on commit 7d944ee

Please sign in to comment.