-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from sonots/sync_worker
sync worker
- Loading branch information
Showing
17 changed files
with
295 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
web: bundle exec unicorn -E production -p $PORT -o $HOST -c config/unicorn.conf | ||
job: bundle exec bin/fluentd-server job | ||
sync: bundle exec bin/fluentd-server sync | ||
serf: $(bundle exec gem path serf-td-agent)/bin/serf agent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'fluentd_server/model' | ||
require 'fluentd_server/logger' | ||
|
||
class FluentdServer::SyncRunner | ||
include FluentdServer::Logger | ||
|
||
def self.run(opts = {}) | ||
self.new(opts).run | ||
end | ||
|
||
def initialize(opts = {}) | ||
end | ||
|
||
def run | ||
return nil unless FluentdServer::Config.local_storage | ||
plus, minus = find_diff | ||
create(plus) | ||
delete(minus) | ||
end | ||
|
||
def find_locals | ||
return [] unless FluentdServer::Config.local_storage | ||
names = [] | ||
Dir.chdir(FluentdServer::Config.data_dir) do | ||
Dir.glob("*.erb") do |filename| | ||
names << filename.chomp('.erb') | ||
end | ||
end | ||
names | ||
end | ||
|
||
def create(names) | ||
# ToDo: bulk insert with sqlite, postgresql? use activerecord-import for mysql2 | ||
logger.debug "[sync] create #{names}" | ||
names.each do |name| | ||
begin | ||
Post.create(name: name) | ||
rescue ActiveRecord::RecordNotUnique => e | ||
logger.debug "#{e.class} #{e.message} #{name}" | ||
rescue => e | ||
logger.warn "#{e.class} #{e.message} #{name}" | ||
end | ||
end | ||
end | ||
|
||
def delete(names) | ||
logger.debug "[sync] remove #{names}" | ||
begin | ||
Post.where(:name => names).delete_all | ||
rescue => e | ||
logger.warn "#{e.class} #{e.message} #{names}" | ||
end | ||
end | ||
|
||
# Find difference between given array of paths and paths stored in DB | ||
# | ||
# @param [Integer] batch_size The batch size of a select query | ||
# @return [Array] Plus (array) and minus (array) differences | ||
def find_diff(batch_size: 1000) | ||
names = find_locals | ||
plus = names | ||
minus = [] | ||
Post.select('id, name').find_in_batches(batch_size: batch_size) do |batches| | ||
batches = batches.map(&:name) | ||
plus -= batches | ||
minus += (batches - names) | ||
end | ||
[plus, minus] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require "fluentd_server/config" | ||
require "fluentd_server/logger" | ||
require "fluentd_server/sync_runner" | ||
|
||
# reference: https://github.com/focuslight/focuslight/blob/master/lib/focuslight/worker.rb | ||
# thanks! | ||
class FluentdServer::SyncWorker | ||
include FluentdServer::Logger | ||
|
||
DEFAULT_INTERVAL = 60 | ||
attr_reader :interval | ||
|
||
def self.start(opts = {}) | ||
self.new(opts).start | ||
end | ||
|
||
def initialize(opts = {}) | ||
@opts = opts | ||
@interval = opts[:interval] || FluentdServer::Config.sync_interval || DEFAULT_INTERVAL | ||
@signals = [] | ||
end | ||
|
||
def update_next! | ||
now = Time.now | ||
@next_time = now - ( now.to_i % @interval ) + @interval | ||
end | ||
|
||
def start | ||
Signal.trap(:INT){ @signals << :INT } | ||
Signal.trap(:HUP){ @signals << :HUP } | ||
Signal.trap(:TERM){ @signals << :TERM } | ||
Signal.trap(:PIPE, "IGNORE") | ||
|
||
update_next! | ||
logger.info("[sync] first updater start in #{@next_time}") | ||
|
||
childpid = nil | ||
while sleep(0.5) do | ||
if childpid | ||
begin | ||
if Process.waitpid(childpid, Process::WNOHANG) | ||
#TODO: $? (Process::Status object) | ||
logger.debug("[sync] update finished pid: #{childpid}, code: #{$? >> 8}") | ||
logger.debug("[sync] next updater start in #{@next_time}") | ||
childpid = nil | ||
end | ||
rescue Errno::ECHILD | ||
logger.warn("[sync] no child process"); | ||
childpid = nil | ||
end | ||
end | ||
|
||
unless @signals.empty? | ||
logger.warn("[sync] signals_received: #{@signals.join(',')}") | ||
break | ||
end | ||
|
||
next if Time.now < @next_time | ||
update_next! | ||
logger.debug("[sync] (#{@next_time}) updater start") | ||
|
||
if childpid | ||
logger.warn("[sync] previous updater exists, skipping this time") | ||
next | ||
end | ||
|
||
childpid = fork do | ||
FluentdServer::SyncRunner.run(@opts) | ||
end | ||
end | ||
|
||
if childpid | ||
logger.warn("[sync] waiting for updater process finishing") | ||
begin | ||
waitpid childpid | ||
rescue Errno::ECHILD | ||
# ignore | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require_relative 'spec_helper' | ||
require 'fluentd_server/sync_runner' | ||
|
||
if FluentdServer::Config.local_storage | ||
describe 'SyncRunner' do | ||
around { | ||
filenames = File.join(FluentdServer::Config.data_dir, '*.erb') | ||
Dir.glob(filenames).each { |f| File.delete(f) rescue nil } | ||
Post.delete_all | ||
} | ||
let(:runner) { FluentdServer::SyncRunner.new } | ||
|
||
context '#find_locals' do | ||
before { Post.create(name: 'post1') } | ||
before { Post.create(name: 'post2') } | ||
let(:subject) { runner.find_locals } | ||
it { should =~ ['post1', 'post2' ] } | ||
end | ||
|
||
context '#find_diff' do | ||
before { Post.new(name: 'post1').save_without_file } | ||
before { Post.create(name: 'post2') } | ||
before { File.open(Post.new(name: 'post3').filename, "w") {} } | ||
it { | ||
plus, minus = runner.find_diff | ||
expect(minus).to eql(['post1']) | ||
expect(plus).to eql(['post3']) | ||
} | ||
end | ||
|
||
context '#create' do | ||
before { Post.create(name: 'post1') } | ||
before { runner.create(%w[post1 post2]) } | ||
it { | ||
expect(Post.find_by(name: 'post1').body).not_to be_nil | ||
expect(Post.find_by(name: 'post2').body).to be_nil | ||
} | ||
end | ||
|
||
context '#delete' do | ||
before { | ||
post1 = Post.create(name: 'post1') | ||
post2 = Post.create(name: 'post2') | ||
runner.delete(%w[post1]) | ||
} | ||
it { | ||
expect(Post.find_by(name: 'post1')).to be_nil | ||
expect(Post.find_by(name: 'post2')).not_to be_nil | ||
} | ||
end | ||
|
||
context '#run' do | ||
before { Post.new(name: 'post1').save_without_file } | ||
before { Post.create(name: 'post2') } | ||
before { File.open(Post.new(name: 'post3').filename, "w") {} } | ||
it { | ||
runner.run | ||
expect(Post.find_by(name: 'post1')).to be_nil | ||
expect(Post.find_by(name: 'post2')).not_to be_nil | ||
expect(Post.find_by(name: 'post3')).not_to be_nil | ||
} | ||
end | ||
end | ||
else | ||
describe 'SyncRunner' do | ||
context '#run' do | ||
let(:subject) { FluentdServer::SyncRunner.new.run } | ||
it { should be_nil } | ||
end | ||
context '#find_locals' do | ||
before { Post.create(name: 'post1') } | ||
before { Post.create(name: 'post2') } | ||
let(:subject) { FluentdServer::SyncRunner.new.find_locals } | ||
it { should == [] } | ||
end | ||
end | ||
end |
Oops, something went wrong.