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

data synchronization #52

Open
o-jill opened this issue Aug 24, 2017 · 12 comments
Open

data synchronization #52

o-jill opened this issue Aug 24, 2017 · 12 comments

Comments

@o-jill
Copy link
Owner

o-jill commented Aug 24, 2017

you may have to lock DB when you want to update it like

db = DB.new
db.lock
db.read
db.change_something
db.write
db.unlock

now locking files are used but it's not enough.

@o-jill
Copy link
Owner Author

o-jill commented Aug 30, 2017

db = DB.new
db.change_something

class DB
 def change_something
 File.open(path, "r+") do |file|
  file.flock(File::LOCK_EX)
  data = file.read
  edit_something(data)
  file.write(data)
 end
end

@o-jill
Copy link
Owner Author

o-jill commented Aug 30, 2017

now, data shown below should be considered.

  • mandatory
    • user info file
    • taikyoku-chu file
    • taikyoku file
  • maybe not mandatory because of adding only.
    • chat file
    • taikyoku file
  • don't have to
    • admin config file
    • match info file
    • jkf file
    • sfen log

@o-jill
Copy link
Owner Author

o-jill commented Sep 1, 2017

adding a user become thread-safer. c91c5c6.
adding a game become thread-safer. c3b4eb4.

@o-jill
Copy link
Owner Author

o-jill commented Sep 4, 2017

situations:

  • update a user's stats.
  • update a game's last move.
  • remove a game from taikyoku-chu.

@o-jill
Copy link
Owner Author

o-jill commented Sep 4, 2017

begin
  db = DB.new
  db.lock
  db.read
  db.change_something
  db.write
rescure StandardError => e
 puts "error:#{e}"
ensure
  db.unlock
end

def lock
  if !Dir.mkdir('lock.dir', 0o755)
    // locked
  else
    // lock failed or some already locked.
  end
end

def unlock
  Dir.rmdir('lock.dir')
end

@o-jill
Copy link
Owner Author

o-jill commented Sep 7, 2017

require 'timeout'

class AccessDenied < StandardError; end

def lock(&block)
  # 10秒以内に終わらない場合はAccessDenied例外が発生
  Timeout::timeout(10) do
    open(File.join(Dir.tmpdir, 'my-application.lock'), 'w') do |f|
      begin
        f.flock(File::LOCK_EX)
        block.call
      ensure
        f.flock(File::LOCK_UN)
      end
    end
  end
rescue Exception => ex
  raise AccessDenied.new('timeout')
end

lock do
  # 排他処理
end

refer:http://qiita.com/yuku_t/items/5aeff2a617576b9bc6d2

@o-jill
Copy link
Owner Author

o-jill commented Sep 7, 2017

locking was implemented for UserInfoFile class in 25ff05c.

@o-jill
Copy link
Owner Author

o-jill commented Sep 10, 2017

locking for Taikyoku/Taikyokuchu class was implemented but still only for append().

@o-jill
Copy link
Owner Author

o-jill commented Sep 11, 2017

lock for Taikyoku/Taikyokuchu class was fully imlemented in 4afd8f8.

@o-jill o-jill closed this as completed Sep 11, 2017
@o-jill
Copy link
Owner Author

o-jill commented Sep 11, 2017

you have to use DB Server to avoid this issue!

@o-jill
Copy link
Owner Author

o-jill commented Dec 27, 2017

now, data shown below should be considered.

  • mandatory
    • user info file
    • taikyoku-chu file
    • taikyoku file
    • match info file <-- moved here
    • jkf file <-- moved here
  • maybe not mandatory because of adding only.
    • chat file
    • taikyoku file
  • don't have to
    • admin config file
    • match info file
    • jkf file
    • sfen log

@o-jill o-jill reopened this Dec 27, 2017
@o-jill
Copy link
Owner Author

o-jill commented Dec 27, 2017

match info file and jkf file are moved to mandatory because players and byouyomichan will update them asynchronously.

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

No branches or pull requests

1 participant