Class: Ohajiki::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/ohajiki/service.rb

Instance Method Summary collapse

Constructor Details

#initializeService

Returns a new instance of Service.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ohajiki/service.rb', line 10

def initialize
  @log = Logger.new(File.expand_path(Config::LOG_PATH))
  @interval_sec = Config::SYNC_INTERVAL_SEC 

  dir_path = File.expand_path(Config::SYNC_DIR_PATH) 
  FileUtils.mkdir_p dir_path
  @repo = fetch_repository(dir_path)
rescue => e
  @log.error "init: #{e.message}"
  raise InitializeError
end

Instance Method Details

#fetch_repository(dir_path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/ohajiki/service.rb', line 22

def fetch_repository(dir_path)
  unless Repo.exist? dir_path
    Repo.init(dir_path) do
      remote_add('origin', Config::REMOTE_REPO_URL)
      pull!
    end
  else
    Repo.new dir_path
  end
end

#startObject



33
34
35
36
37
38
39
40
# File 'lib/ohajiki/service.rb', line 33

def start
  @log.info "start service"
  loop do 
    sync
    update
    sleep @interval_sec
  end
end

#syncObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ohajiki/service.rb', line 42

def sync 
  unless @repo.latest? 
    @log.info "pull #{@repo.remote_head_id}"
    @repo.reset_hard_head!
    @repo.pull!
  else
    @log.info "repository latest"
  end
rescue => e 
  @log.error "sync: #{e.message}"
end

#updateObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ohajiki/service.rb', line 54

def update
  @repo.add_stage! if @repo.file_changed? and @repo.file_added?
  message = "push #{@repo.changed_count} files"
  if @repo.commit_all!
    @log.info message
    @repo.push!
  else
    @log.info "no changed"
  end
rescue => e
  @log.error "update: #{e.message}"
end