Skip to content

Commit

Permalink
Use transactions instead of locks
Browse files Browse the repository at this point in the history
  • Loading branch information
notr1ch committed Jan 2, 2024
1 parent 6cc15ef commit 666bea9
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions cron/wiki_hotness.php
Expand Up @@ -154,28 +154,36 @@
}
}

$db->exec( 'LOCK TABLES wiki_hot WRITE' );
$db->exec( 'DELETE FROM wiki_hot' );
for ( $attempts = 0; $attempts < 3; $attempts++ ) {
$db->beginTransaction();

foreach ( $wiki_hits as $row ) {
$db->exec( 'DELETE FROM wiki_hot' );

$url = mb_substr( $row[ 'url' ], 0, 255 );
$count = $row[ 'hits' ];
$wiki = $row[ 'wiki' ];
$title = mb_substr( $row[ 'title' ], 0, 255 );
$insertSql = 'INSERT INTO wiki_hot (wiki, page, title, hits) VALUES ( :wiki, :url, :title, :count )';
$sth = $db->prepare( $insertSql );

$insertData = [
'wiki' => $wiki,
'url' => $url,
'title' => $title,
'count' => $count
];
foreach ( $wiki_hits as $row ) {

$insertSql = 'INSERT INTO wiki_hot (wiki, page, title, hits) VALUES ( :wiki, :url, :title, :count )';
$db->prepare( $insertSql )->execute( $insertData );
}
$url = mb_substr( $row[ 'url' ], 0, 255 );
$count = $row[ 'hits' ];
$wiki = $row[ 'wiki' ];
$title = mb_substr( $row[ 'title' ], 0, 255 );

$db->exec( 'UNLOCK TABLES' );
$insertData = [
'wiki' => $wiki,
'url' => $url,
'title' => $title,
'count' => $count
];

$sth->execute( $insertData );
}

if ( $db->commit() )
break;

sleep ( 1 );
}

curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PURGE' );
curl_setopt( $ch, CURLOPT_URL, "http://127.0.0.1:6081/" );
Expand Down

0 comments on commit 666bea9

Please sign in to comment.