views:

36

answers:

1

So I'm creating a Rails app that will function as a directory for contact information in our organization. But it's not as simple as it sounds.

  • We have a large LDAP-enabled directory which contains information for all of the users in our organization of tens of thousands.
  • We have a smaller, separate LDAP-enabled directory which contains additional information for our department of several hundred, as well as some information duplicating or taking precedence over fields in the larger directory.
  • We will want to hand edit some of this data to override some of the fields in our local directory, which will be represented by a SQL table in the Rails app.
  • The remote directories will be periodically mirrored as SQL tables, and the 3 tables (Organization, Department, Local) will be compared to choose the correct value displayed in the app.

I know that sounds ridiculous, but nothing can be done for it. Our organization is very decentralized, and this is the most straightforward way to get what we want.

This is my second Rails app, so I'm comfortable with most of the design and plumbing, but I don't know the best way to periodically poll the data from the remote directories and import it into our local SQL tables.

Where in my app should I periodically import data into my tables from LDAP? Do I use Rails? Should I do this in straight Ruby and run it as a cron job?

+1  A: 

If you want to have the sync functionality as part of your Rails app, then you can create that logic in a separate model class (let's call it LDAPSynchroniser).

Then you can reuse it from multiple places, including:

  • Rake task for manual syncronisation;
  • Cron job Running the Rake task;
  • Trigger the synchronisation from the web application (take into account the time it takes to run!)

The rake task would look like:

task :cron => :ldapsync do
  puts "Sync-ing with LDAP..."
  status = LDAPSynchroniser.new.run
  puts "done: #{status.to_s}"
end

The web application trigger would be a regular controller:

def LDAPSyncController < ...
  # probably authentication is needed...
  def sync
    status = LDAPSynchroniser.new.run # or run it in a separate thread-ish
    # respond with status
  end
end

Now to answer your questions:

Where in my app should I periodically import data into my tables from LDAP?

Use rake task + cron.

Do I use Rails?

You probably need to boot rails, but you don't need to run the rails web server for that. Although you might want to trigger the task from the web application itself.

Should I do this in straight Ruby and run it as a cron job?

Doing it in Rails would be a little bit easier as you already have your model and all you need. With Plain Ruby it might be possible as well but I don't think it is worth the effort.

Dmytrii Nagirniak
Excellent answer! This sounds great, and I will probably go with rake task + cron. I'm going to wait a bit and see if there are other suggestions.
sidewaysmilk
After researching rake tasks a bit, I'm can't wait to put this into practice. Thanks!
sidewaysmilk