mirror of
https://github.com/pragma-/pbot.git
synced 2025-01-11 12:32:37 +01:00
Add flock semaphore to prevent race conditions
This commit is contained in:
parent
b693f96806
commit
9d4af761ca
@ -5,6 +5,7 @@ use strict;
|
|||||||
|
|
||||||
use Time::HiRes qw/gettimeofday/;
|
use Time::HiRes qw/gettimeofday/;
|
||||||
use Time::Duration qw/duration/;
|
use Time::Duration qw/duration/;
|
||||||
|
use Fcntl qw(:flock);
|
||||||
|
|
||||||
use IRCColors;
|
use IRCColors;
|
||||||
|
|
||||||
@ -25,6 +26,9 @@ if ($channel !~ /^#/) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open my $semaphore, ">", "$CJEOPARDY_DATA-$channel.lock" or die "Couldn't create semaphore lock: $!";
|
||||||
|
flock $semaphore, LOCK_EX;
|
||||||
|
|
||||||
my $ret = open my $fh, "<", "$CJEOPARDY_DATA-$channel";
|
my $ret = open my $fh, "<", "$CJEOPARDY_DATA-$channel";
|
||||||
if (defined $ret) {
|
if (defined $ret) {
|
||||||
my $last_question = <$fh>;
|
my $last_question = <$fh>;
|
||||||
|
@ -5,6 +5,7 @@ use strict;
|
|||||||
|
|
||||||
use Text::Levenshtein qw(fastdistance);
|
use Text::Levenshtein qw(fastdistance);
|
||||||
use Time::HiRes qw(gettimeofday);
|
use Time::HiRes qw(gettimeofday);
|
||||||
|
use Fcntl qw(:flock);
|
||||||
|
|
||||||
use Scorekeeper;
|
use Scorekeeper;
|
||||||
use IRCColors;
|
use IRCColors;
|
||||||
@ -40,6 +41,9 @@ if (not length $lctext) {
|
|||||||
|
|
||||||
my @data;
|
my @data;
|
||||||
|
|
||||||
|
open my $semaphore, ">", "$CJEOPARDY_DATA-$channel.lock" or die "Couldn't create semaphore lock: $!";
|
||||||
|
flock $semaphore, LOCK_EX;
|
||||||
|
|
||||||
my $ret = open my $fh, "<", "$CJEOPARDY_LAST_ANSWER-$channel";
|
my $ret = open my $fh, "<", "$CJEOPARDY_LAST_ANSWER-$channel";
|
||||||
if (defined $ret) {
|
if (defined $ret) {
|
||||||
my $last_nick = <$fh>;
|
my $last_nick = <$fh>;
|
||||||
@ -188,10 +192,10 @@ foreach my $answer (@valid_answers) {
|
|||||||
my %streaks = (
|
my %streaks = (
|
||||||
3 => "$color{orange}$nick$color{green} is on a $color{orange}3$color{green} correct answer streak!",
|
3 => "$color{orange}$nick$color{green} is on a $color{orange}3$color{green} correct answer streak!",
|
||||||
4 => "$color{orange}$nick$color{green} is hot with a $color{orange}4$color{green} correct answer streak!",
|
4 => "$color{orange}$nick$color{green} is hot with a $color{orange}4$color{green} correct answer streak!",
|
||||||
5 => "$color{orange}$nick$color{green} is on fire with an $color{orange}5$color{green} correct answer streak!",
|
5 => "$color{orange}$nick$color{green} is on fire with a $color{orange}5$color{green} correct answer streak!",
|
||||||
6 => "$color{orange}$nick$color{green} is ON FIRE with a $color{orange}6$color{green} correct answer streak!",
|
6 => "$color{orange}$nick$color{green} is ON FIRE with a $color{orange}6$color{green} correct answer streak!",
|
||||||
7 => "$color{orange}$nick$color{green} is DOMINATING with a $color{orange}7$color{green} correct answer streak!",
|
7 => "$color{orange}$nick$color{green} is DOMINATING with a $color{orange}7$color{green} correct answer streak!",
|
||||||
8 => "$color{orange}$nick$color{green} is DOMINATING with a $color{orange}8$color{green} correct answer streak!",
|
8 => "$color{orange}$nick$color{green} is DOMINATING with an $color{orange}8$color{green} correct answer streak!",
|
||||||
9 => "$color{orange}$nick$color{green} is DOMINATING with a $color{orange}9$color{green} correct answer streak!",
|
9 => "$color{orange}$nick$color{green} is DOMINATING with a $color{orange}9$color{green} correct answer streak!",
|
||||||
10 => "$color{orange}$nick$color{green} IS UNTOUCHABLE WITH A $color{orange}10$color{green} CORRECT ANSWER STREAK!"
|
10 => "$color{orange}$nick$color{green} IS UNTOUCHABLE WITH A $color{orange}10$color{green} CORRECT ANSWER STREAK!"
|
||||||
);
|
);
|
||||||
@ -211,6 +215,8 @@ foreach my $answer (@valid_answers) {
|
|||||||
print $fh "$nick\n$data[1]$time\n";
|
print $fh "$nick\n$data[1]$time\n";
|
||||||
close $fh;
|
close $fh;
|
||||||
|
|
||||||
|
close $semaphore;
|
||||||
|
|
||||||
if ($channel eq '#cjeopardy') {
|
if ($channel eq '#cjeopardy') {
|
||||||
my $question = `./cjeopardy.pl $channel`;
|
my $question = `./cjeopardy.pl $channel`;
|
||||||
|
|
||||||
@ -254,10 +260,10 @@ if ($player_data->{highest_wrong_streak} > $player_data->{lifetime_highest_wrong
|
|||||||
}
|
}
|
||||||
|
|
||||||
my %streaks = (
|
my %streaks = (
|
||||||
3 => "$color{red}Try a little bit harder, $color{orange}$nick$color{red}.",
|
3 => "$color{red}Guessing, are we, $color{orange}$nick$color{red}?",
|
||||||
4 => "$color{red}Guessing, are we, $color{orange}$nick$color{red}?",
|
4 => "$color{red}Please use better guesses, $color{orange}$nick!",
|
||||||
5 => "$color{red}Please use better guesses, $color{orange}$nick!",
|
5 => "$color{red}Are you even trying, $color{orange}$nick$color{red}?!",
|
||||||
6 => "$color{red}Are you even trying, $color{orange}$nick$color{red}?!",
|
6 => "$color{red}Try a little bit harder, $color{orange}$nick$color{red}.",
|
||||||
7 => "$color{red}Maybe you should go look it up, $color{orange}$nick$color{red}.",
|
7 => "$color{red}Maybe you should go look it up, $color{orange}$nick$color{red}.",
|
||||||
8 => "$color{red}For fuck's sake, go look it up already, $color{orange}$nick$color{red}!",
|
8 => "$color{red}For fuck's sake, go look it up already, $color{orange}$nick$color{red}!",
|
||||||
9 => "$color{red}Are you sure you weren't dropped on your head, $color{orange}$nick$color{red}?",
|
9 => "$color{red}Are you sure you weren't dropped on your head, $color{orange}$nick$color{red}?",
|
||||||
|
@ -5,6 +5,7 @@ use strict;
|
|||||||
|
|
||||||
use Time::HiRes qw/gettimeofday/;
|
use Time::HiRes qw/gettimeofday/;
|
||||||
use Time::Duration qw/duration/;
|
use Time::Duration qw/duration/;
|
||||||
|
use Fcntl qw(:flock);
|
||||||
|
|
||||||
use Scorekeeper;
|
use Scorekeeper;
|
||||||
use IRCColors;
|
use IRCColors;
|
||||||
@ -26,6 +27,9 @@ if ($channel !~ /^#/) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open my $semaphore, ">", "$CJEOPARDY_DATA-$channel.lock" or die "Couldn't create semaphore lock: $!";
|
||||||
|
flock $semaphore, LOCK_EX;
|
||||||
|
|
||||||
my @data;
|
my @data;
|
||||||
open my $fh, "<", "$CJEOPARDY_DATA-$channel" or print "There is no open C Jeopardy question. Use `cjeopardy` to get a question.\n" and exit;
|
open my $fh, "<", "$CJEOPARDY_DATA-$channel" or print "There is no open C Jeopardy question. Use `cjeopardy` to get a question.\n" and exit;
|
||||||
@data = <$fh>;
|
@data = <$fh>;
|
||||||
|
Loading…
Reference in New Issue
Block a user