简体   繁体   English

依次运行红宝石方法

[英]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? 它们在run方法上是否同时运行? 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. 我希望在运行更新方法之前先运行并完成第一个create和map方法。

Ruby will run the methods in the order in which they are called. Ruby将按照调用它们的顺序运行这些方法。 Ruby default is not asynchronous. Ruby默认不是异步的。 However in Rails it is common and recommend to use asynchronous background jobs. 但是在Rails中很常见,建议使用异步后台作业。 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. 您可能还想看一下有关Rails中的交互器的文章,这在这种情况下可能是有用的设计模式。 Also see the following related gems, interactor and activeinteractor 另请参阅以下相关的gem, interactoractiveinteractor

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. 您可以生成多个线程并同时启动它们,但是请记住Ruby具有GIL ,因此,只有其中一些函数进行Web调用或其他O / I操作时,它才会对您有利。

If you decide you want to do that, you could use a library like Concurrent Ruby to make it easier. 如果您决定要这样做,可以使用Concurrent Ruby之类的库来简化它。 https://github.com/ruby-concurrency/concurrent-ruby 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. 如果您在Rails中执行此操作,则Active Record回调将使您能够调用所有这些方法,以便可以在预期函数执行之前或之后执行。 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. 请参阅有关HERE的更多详细信息,它将大大有助于使您的代码变干。 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. Ruby函数按调用顺序运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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