Skip to content

Instantly share code, notes, and snippets.

@kovyrin
Created November 2, 2011 16:07
Show Gist options
  • Save kovyrin/1334044 to your computer and use it in GitHub Desktop.
Save kovyrin/1334044 to your computer and use it in GitHub Desktop.
Flickr-inspired Ticket Server client
class Ticket
class CouldNotRetrieveTicket < StandardError ; end
def self.connections(connections)
@connections = connections
end
def self.set_table_name(table_name)
@table_name = table_name
end
def self.get_next_id
connections = @connections.dup
begin
conn_name = connections.rand
raise 'No connection to try to use!' unless conn_name
connections.delete conn_name
conn = DbCharmer::ConnectionFactory.connect(conn_name, true)
conn.execute "REPLACE INTO `#{@table_name}` (stub) VALUES ('a');"
return conn.raw_connection.insert_id
rescue ActiveRecord::StatementInvalid => e
retry unless connections.size == 0
raise CouldNotRetrieveTicket
end
end
def self.create_table_sql(table_name)
<<-SQL
CREATE TABLE `#{table_name}` (
`id` int(11) unsigned NOT NULL auto_increment,
`stub` char(1) NOT NULL default '',
PRIMARY KEY (`id`),
UNIQUE KEY `stub` (`stub`)
) ENGINE=MyISAM
SQL
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment