简体   繁体   中英

Run ruby methods one after the other

I have a doctors importer created, but I want to run and finish some methods first like creating new offices and such before updating that doctors data. On the run method do they run concurrently? Or do methods run one at a time?

ruby

  def run
    # Create all of our dependencies
    create_hospitals
    create_departments
    create_specialties
    create_offices

    # Map the dependencies to each doctor
    map_hospitals
    map_departments
    map_specialties
    map_offices

    # Save the mapped data, then traverse and create doctors that don't exist
    @record.save
    update_doctors  # Update existing physicians
    create_doctors  # Create new physicians

    # Update the record status
    @record.import_log.empty? ? @record.completed! : @record.failed!
  end

I want the first create and map methods to run and finish before the update methods run.

Ruby will run the methods in the order in which they are called. Ruby default is not asynchronous. However in Rails it is common and recommend to use asynchronous background jobs. See documentation for ideas on how to set that up.

Also, looking at the long list of methods you're running inside of your run method, without knowing what those do, I can only speculate that there might be some complex business logic. You might also want to have a look at this article on interactors in rails which might be a useful design pattern in such cases. Also see the following related gems, interactor and activeinteractor

They will run sequentially. You could spawn multiple threads and start them concurrently, but keep in mind that Ruby has a GIL , so it's only going to benefit you if some of those functions make web calls or other O/I operations.

If you decide you want to do that, you could use a library like Concurrent Ruby to make it easier. https://github.com/ruby-concurrency/concurrent-ruby

If you are doing this in Rails , Active Record Callbacks give you access to call all those methods such that it can execute before or after your expected function executes. Example are:

Creating an Object

before_validation
after_validation
before_save
around_save
before_create
around_create
after_create
after_save
after_commit/after_rollback

Updating an Object

before_validation
after_validation
before_save
around_save
before_update
around_update
after_update
after_save
after_commit/after_rollback

Destroying an Object

before_destroy
around_destroy
after_destroy
after_commit/after_rollback

Refer to more details about how HERE and it will significantly help in drying out your code. You can also override those callback by defining them as methods so you can extend it to your wishes if need be.

Ruby functions run in the order in which they are called.

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