Created
December 27, 2012 02:44
-
-
Save keating/4385018 to your computer and use it in GitHub Desktop.
use ActiveRecord with in ruby script
This file contains hidden or 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 "active_record" | |
| ActiveRecord::Base.establish_connection( | |
| :adapter => "postgresql", | |
| :host => "localhost", | |
| :username => "map", | |
| :password => "map", | |
| :database => "lspf" | |
| ) | |
| #ActiveRecord::Base.connection.execute create_table_sql | |
| class Edi < ActiveRecord::Base | |
| self.table_name = "cy_records_no_repeat" | |
| end | |
| class Ctn < ActiveRecord::Base | |
| self.table_name = "ctns" | |
| end | |
| arr = [] | |
| id_for_ctn = 1 | |
| Edi.order("ctn_no_ asc, oper_time_ desc").each do |edi| | |
| next if arr.include?(edi.ctn_no_) | |
| arr << edi.ctn_no_ | |
| #Ctn.create(code: edi.ctn_no_f_, size: edi.ctn_size_f_id_, ctn_type: edi.ctn_type_f_name_, last_action_id: edi.id_) | |
| hash = { | |
| id_: id_for_ctn, | |
| code: edi.ctn_no_, size: edi.ctn_size_id_ | |
| } | |
| Ctn.create hash | |
| id_for_ctn+=1 | |
| end |
This file contains hidden or 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
| class WaitPersisting < ActiveRecord::Base | |
| end | |
| while true | |
| puts "SolveUnpersistedData is running" | |
| begin | |
| id_arr = [] | |
| WaitPersisting.establish_connection( | |
| :adapter => "postgresql", | |
| :host => "localhost", | |
| :username => "map", | |
| :password => "map", | |
| :database => "dev2" | |
| ) | |
| WaitPersisting.where("created_at > ?", 5.days.ago).select("id").each do |id| | |
| id_arr << id | |
| end | |
| WaitPersisting.remove_connection | |
| unless id_arr.empty? | |
| uri = URI.parse("http://localhost:3000/inner_api/solve_un_persisted") | |
| result = Net::HTTP.post_form uri, :ids => id_arr.join | |
| if result.is_a? Net::HTTPOK | |
| logger.info "send successful:#{id_arr}" | |
| else | |
| logger.error "send failed:#{id_arr}" | |
| end | |
| end | |
| sleep 20 | |
| rescue => e | |
| puts "Error in class SolveUnpersistedData: #{e.message}" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment