Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

current shard is stack on slave #505

Open
omerdoron1 opened this issue Oct 3, 2018 · 1 comment
Open

current shard is stack on slave #505

omerdoron1 opened this issue Oct 3, 2018 · 1 comment
Labels

Comments

@omerdoron1
Copy link

I am using Octopus 0.8.0 with Rails 3

After I am doing using(:slave), if by mistake I am trying to write I am getting errors of read_only user is trying to write to DB...
Then the current_shard of the connection proxy is stuck on the :slave shard

I thought to add this initializer to rails:

 module OctopusActiveRecordExtension
  extend ActiveSupport::Concern
   attr_accessor :octopus_shard
   included do
    after_initialize :remove_current_shard
    before_save :raise_error_if_shard_is_slave
  end
   protected
    def remove_current_shard
      # we backup if the model was recieved from slave in order
      # to throw error if someone will try to save it to master
      self.octopus_shard = self.current_shard
      self.current_shard = nil
    end
     def raise_error_if_shard_is_slave
      if self.octopus_shard == :slave
        raise "Trying to write to model created from slave"
      end
    end
end
 # include the extension
ActiveRecord::Base.send(:include, OctopusActiveRecordExtension) 

Does that make sense?

Is there any better way to achieve the reset of the current_shard to master after trying to write to slave?

Do you think I should add a middleware to rails that will reset the current_shard to master:
before each request and async job? For example:

Thread.current["octopus.current_shard"] = :master
ActiveRecord::Base.connection().current_shard = :master
@thiagopradi
Copy link
Owner

Hi @omerdoron1,

Sorry for the late response - are you still using Octopus? did you manage to fix this issue?

I believe it might be a bug - but, unfortunately, Octopus doesn't support Rails 3.x anymore.

Thiago

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants