class EnClient::SyncTask

Constants

MAX_SYNCED_ENTRY

Public Class Methods

new(sm, dm, tm) click to toggle source

MAX_SYNCED_ENTRY = 1 for test

# File usr/lib/evernote-mode/enclient.rb, line 1052
def initialize(sm, dm, tm)
  @sm = sm
  @dm = dm
  @tm = tm
end

Public Instance Methods

exec() click to toggle source
# File usr/lib/evernote-mode/enclient.rb, line 1058
def exec
  note_store = @sm.note_store
  sync_state = note_store.getSyncState @sm.auth_token

  LOG.info "[sync state begin]"
  LOG.info "currentTime    = #{sync_state.currentTime}"
  LOG.info "fullSyncBefore = #{sync_state.fullSyncBefore}"
  LOG.info "updateCount    = #{sync_state.updateCount}"
  LOG.info "[sync state end]"
  LOG.info "expiration     = #{@sm.expiration}"

  @sm.refresh_authentication sync_state.currentTime
  last_sync, usn = DBUtils.get_last_sync_and_usn @dm

  LOG.info "[current state begin]"
  LOG.info "last_sync      = #{last_sync}"
  LOG.info "USN            = #{usn}"
  LOG.info "[current state end]"

  return if sync_state.updateCount == usn

  is_full_sync = false

  if last_sync < sync_state.fullSyncBefore
    @dm.transaction do
      @dm.clear_db
    end
    @dm.set_during_full_sync true
    LOG.debug "begin full sync"
  end

  if @dm.during_full_sync?
    is_full_sync = true
  end

  sync_chunk = note_store.getSyncChunk @sm.auth_token, usn, MAX_SYNCED_ENTRY, is_full_sync
  LOG.debug "sync (#{usn}-#{sync_chunk.chunkHighUSN}) full_sync = #{is_full_sync}"
  sync_db sync_chunk

  if sync_chunk.chunkHighUSN < sync_chunk.updateCount
    @tm.put SyncTask.new(@sm, @dm, @tm)
  else
    @dm.set_during_full_sync false
    LOG.debug "finish full sync"
  end

rescue
  if $!.is_a? SystemCallError
    # workaround for corruption of note_store after timed out
    @sm.fix_note_store
  end
  message = ErrorUtils.get_message $!
  LOG.warn message
  LOG.warn $!.backtrace
end

Private Instance Methods

sync_db(sync_chunk) click to toggle source
# File usr/lib/evernote-mode/enclient.rb, line 1116
def sync_db(sync_chunk)
  DBUtils.sync_updated_notebooks @dm, sync_chunk.notebooks if sync_chunk.notebooks
  DBUtils.sync_updated_notes @dm, @sm, @tm, sync_chunk.notes if sync_chunk.notes
  DBUtils.sync_updated_tags @dm, sync_chunk.tags if sync_chunk.tags
  DBUtils.sync_updated_searches @dm, sync_chunk.searches if sync_chunk.searches
  DBUtils.sync_expunged_notebooks @dm, sync_chunk.expungedNotebooks if sync_chunk.expungedNotebooks
  DBUtils.sync_expunged_notes @dm, sync_chunk.expungedNotes, @tm if sync_chunk.expungedNotes
  DBUtils.sync_expunged_tags @dm, sync_chunk.expungedTags if sync_chunk.expungedTags
  DBUtils.sync_expunged_searches @dm, sync_chunk.expungedSearches if sync_chunk.expungedSearches
  DBUtils.set_last_sync_and_usn @dm, sync_chunk.currentTime, sync_chunk.chunkHighUSN
end