Module: Revenant::MySQL

Extended by:
MySQL
Included in:
MySQL
Defined in:
lib/locks/mysql.rb

Instance Method Summary collapse

Instance Method Details

#acquire_lock(lock_name) ⇒ Object

Expects the connection to behave like an instance of Mysql If you need something else, replace acquire_lock with your own code. Or define your own lock_function while configuring a new Revenant task.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/locks/mysql.rb', line 21

def acquire_lock(lock_name)
  begin
    acquired = false
    sql = lock_query(lock_name)
    connection.query(sql) do |result|
      acquired = result.fetch_row.first == "1"
    end
    acquired
  rescue ::Exception
    false
  end
end

#connectionObject

Currently defaults to the ActiveRecord connection if AR is loaded. Set this in your task setup block if that is not what you want.



40
41
42
43
44
45
46
# File 'lib/locks/mysql.rb', line 40

def connection
  @connection ||= if defined?(ActiveRecord)
                    ActiveRecord::Base.connection.raw_connection
                  else
                    raise "No connection established or discovered. Use Revenant::MySQL::connection="
                  end
end

#connection=(conn) ⇒ Object



48
49
50
# File 'lib/locks/mysql.rb', line 48

def connection=(conn)
  @connection = conn
end

#lock_functionObject



12
13
14
15
16
# File 'lib/locks/mysql.rb', line 12

def lock_function
  Proc.new do |lock_name|
    ::Revenant::MySQL.acquire_lock(lock_name)
  end
end

#lock_query(lock_name) ⇒ Object



34
35
36
# File 'lib/locks/mysql.rb', line 34

def lock_query(lock_name)
  "select get_lock('#{lock_name}',0);"
end