简体   繁体   中英

Communicating between 2 Rails applications

I have 2 separate rails applications (app a, app b). Both of these apps maintain a customer list. I would like to run a rake task once a day and have app b pull in select customers from app a.

This is the way I have attempted to solve this. If I am going down the wrong road please let me know.

I am using JBuilder to generate the JSON

My issue is with how to have App B set an id in app A, so that the system knows the customer has already been transfered over.

Im assuming I have to do a put request similar to what I have done to get the customers list, but I am having issues getting that to work.

App A

Customers Model

scope :for_export, :conditions => {:verified => true, :new_system_id => nil ...}

Customers Controller

skip_before_filter :verify_authenticity_token, :only => [:update]

#
def index
    @customers = Customer.for_export
end

def update
    @customer = Customer.find(params[:id])
    if @customer.update_attributes(params[:customer])
        render :text => 'success', :status => 200
    end
end

App B

rake task

task :import_customers => :environment do
   c = Curl::Easy.new("http://domain.com/customers.json")
   c.http_auth_types = :basic
   c.username = 'username'
   c.password = 'password'
   c.perform
   a = JSON.parse(c.body_str)

   a.each do |customer|
       customer = Customer.create(customer)
           #put request back to server a to update field
       end


end

end end

What I have is currently working, Im just not sure if this is the correct method, and also how to initiate a put request to call the update method in the customer controller.

Thanks!

Ryan

I'm sorry that I'm not answering your question, but I am giving you an alternative. What you are trying to create sounds a lot like an ETL job. You may want to consider having a batch job move a copy of your customers table from app a over to app b periodically, and then have another batch job import that table into app b's database. I know, it's a little clunky, but it's a very popular and reliable pattern to solve your problem.

Also, if both apps are in the same data center, then you may want to create a read-only database view of app a's customer data and then have app b read that using SQL calls. It's a slightly cheaper and easier way to integrate the two apps than the option that I listed above.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM