| 1 | #!perl | ||
| 2 | |||
| 3 | package File::Lock::Multi::MySQL; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings (FATAL => 'all'); | ||
| 7 | use File::Lock::Multi::Base::Iterative; | ||
| 8 | use base q(File::Lock::Multi::Base::Iterative); | ||
| 9 | use Carp qw(croak); | ||
| 10 | |||
| 11 | |||
| 12 | use DBD::mysql; | ||
| 13 | |||
| 14 | __PACKAGE__->mk_accessors(qw(format dbh _id _path)); | ||
| 15 | |||
| 16 | return 1; | ||
| 17 | |||
| 18 | sub __Validators { | ||
| 19 | my $class = shift; | ||
| 20 | |||
| 21 | return( | ||
| 22 | $class->SUPER::__Validators( | ||
| 23 | format => { default => "%s.%i" }, | ||
| 24 | dbh => 1, | ||
| 25 | @_ | ||
| 26 | ) | ||
| 27 | ); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my $class = shift; | ||
| 32 | my $self = $class->SUPER::new(@_); | ||
| 33 | if(ref($self->dbh) eq 'CODE') { | ||
| 34 | $self->dbh($self->dbh->()); | ||
| 35 | } | ||
| 36 | return $self; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub locked { | ||
| 40 | my $self = shift; | ||
| 41 | return $self->_path ? 1 : 0; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub lock_non_block_for { | ||
| 45 | my($self, $id) = @_; | ||
| 46 | croak "lock_non_block_for called while already locked" if $self->locked; | ||
| 47 | if(my $path = $self->obtain_lock_for($id)) { | ||
| 48 | $self->_path($path); | ||
| 49 | $self->_id($id); | ||
| 50 | return $id; | ||
| 51 | } else { | ||
| 52 | return; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | sub obtain_lock_for { | ||
| 57 | my($self, $id) = @_; | ||
| 58 | return $self->lock_path($self->format_path($id)); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub lock_held_for { | ||
| 62 | my($self, $id) = @_; | ||
| 63 | return $self->is_used_lock($self->format_path($id)); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub format_path { | ||
| 67 | my($self, $id) = @_; | ||
| 68 | return sprintf($self->format, $self->file, $id); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub path { | ||
| 72 | my $self = shift; | ||
| 73 | croak "can not obtain a path without an ID" unless defined $self->_id; | ||
| 74 | return $self->format_path($self->_id); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _release { | ||
| 78 | my $self = shift; | ||
| 79 | my($result) = ($self->dbh->selectrow_array( | ||
| 80 | "SELECT RELEASE_LOCK(?)", | ||
| 81 | { RaiseError => 1, PrintError => 0 }, | ||
| 82 | $self->_path | ||
| 83 | )); | ||
| 84 | $self->_path(undef); | ||
| 85 | $self->_id(undef); | ||
| 86 | return 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub is_used_lock { | ||
| 90 | my($self, $path) = @_; | ||
| 91 | my($result) = ($self->dbh->selectrow_array( | ||
| 92 | "SELECT IS_USED_LOCK(?)", { RaiseError => 1, PrintError => 0 }, $path | ||
| 93 | )); | ||
| 94 | |||
| 95 | if($result) { | ||
| 96 | return $path; | ||
| 97 | } else { | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | sub lock_path { | ||
| 103 | my($self, $path) = @_; | ||
| 104 | my($result) = ($self->dbh->selectrow_array( | ||
| 105 | "SELECT GET_LOCK(?,0)", { RaiseError => 1, PrintError => 0 }, $path | ||
| 106 | )); | ||
| 107 | |||
| 108 | if($result) { | ||
| 109 | return $path; | ||
| 110 | } else { | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | sub DESTROY { | ||
| 116 | my $self = shift; | ||
| 117 | $self->release if $self->locked; | ||
| 118 | $self->SUPER::DESTROY if $self->SUPER::can('DESTROY'); | ||
| 119 | } | ||
| 120 | |||
| 121 | __END__ |