Class: Ohajiki::Service
- Inherits:
-
Object
- Object
- Ohajiki::Service
- Defined in:
- lib/ohajiki/service.rb
Instance Method Summary collapse
- #fetch_repository(dir_path) ⇒ Object
-
#initialize ⇒ Service
constructor
A new instance of Service.
- #start ⇒ Object
- #sync ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize ⇒ Service
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.(Config::LOG_PATH)) @interval_sec = Config::SYNC_INTERVAL_SEC dir_path = File.(Config::SYNC_DIR_PATH) FileUtils.mkdir_p dir_path @repo = fetch_repository(dir_path) rescue => e @log.error "init: #{e.}" 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 |
#start ⇒ Object
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 |
#sync ⇒ Object
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.}" end |
#update ⇒ Object
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? = "push #{@repo.changed_count} files" if @repo.commit_all! @log.info @repo.push! else @log.info "no changed" end rescue => e @log.error "update: #{e.}" end |