Created
October 30, 2012 13:27
-
-
Save jimhj/3980168 to your computer and use it in GitHub Desktop.
Eventmachine with AR demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'rubygems' | |
require 'eventmachine' | |
require 'logger' | |
require 'active_record' | |
# Rails -v: 2.3.5 | |
REF_RAILS_ROOT_DIR = File.expand_path(File.join(File.dirname(__FILE__),'..','..')) | |
class MobileBalanceServer < EventMachine::Connection | |
def initialize | |
@logger = Logger.new(File.join(File.dirname(__FILE__),'logs',"cmb_store_direct_#{Time.now.strftime("%Y%m%d")}.log")) | |
db_conn | |
@logger.info("MobileBalanceServer start........") | |
super | |
end | |
def post_init | |
@logger.info("------------ receive_data start ----------") | |
end | |
def connection_completed | |
end | |
def receive_data(data) | |
@logger.info(data) | |
@logger.info("------------ receive_data end ----------") | |
begin | |
BalanceInquiryTransform.update_by_resp(data) | |
rescue Exception => e | |
@logger.info e.backtrace.join("\n") | |
ensure | |
close_connection_after_writing | |
end | |
end | |
def unbind | |
end | |
private | |
def db_conn | |
db_yaml_file = File.expand_path(File.join(REF_RAILS_ROOT_DIR,'config','database.yml')) | |
# db_config = YAML::load(File.open(db_yaml_file))[rails_env] | |
db_config = YAML::load(File.open(db_yaml_file))['development'] | |
ActiveRecord::Base.establish_connection(db_config) | |
require File.expand_path(File.join(REF_RAILS_ROOT_DIR,'app','models','balance_inquiry_transform')) | |
end | |
end | |
EventMachine.run { | |
EventMachine.start_server "localhost", 10100, MobileBalanceServer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment