Skip to content

Commit

Permalink
Merge 1a9a0f0 into efe042a
Browse files Browse the repository at this point in the history
  • Loading branch information
gburgett committed Jan 17, 2020
2 parents efe042a + 1a9a0f0 commit 0187d67
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 14 deletions.
6 changes: 2 additions & 4 deletions wcc-contentful/lib/wcc/contentful.rb
Expand Up @@ -110,10 +110,8 @@ def self.init!
indexer = ContentTypeIndexer.from_json_schema(@content_types)
@types = indexer.types

if Services.instance.sync_engine&.should_sync?
# Drop an initial sync
WCC::Contentful::SyncEngine::Job.perform_later if defined?(WCC::Contentful::SyncEngine::Job)
end
# Drop an initial sync
WCC::Contentful::SyncEngine::Job.perform_later if defined?(WCC::Contentful::SyncEngine::Job)

WCC::Contentful::ModelBuilder.new(@types).build_models
@configuration = @configuration.freeze
Expand Down
6 changes: 6 additions & 0 deletions wcc-contentful/lib/wcc/contentful/store/postgres_store.rb
Expand Up @@ -34,6 +34,8 @@ def keys
arr = []
result.each { |r| arr << r['id'].strip }
arr
rescue PG::ConnectionBad
[]
end

def delete(key)
Expand All @@ -48,6 +50,8 @@ def find(key, **_options)
return if result.num_tuples == 0

JSON.parse(result.getvalue(0, 1))
rescue PG::ConnectionBad
nil
end

def find_all(content_type:, options: nil)
Expand Down Expand Up @@ -131,6 +135,8 @@ def resolve

statement = 'SELECT * FROM contentful_raw ' + @statement
@resolved = @connection_pool.with { |conn| conn.exec(statement, @params) }
rescue PG::ConnectionBad
[]
end

def push_param(param, params)
Expand Down
22 changes: 12 additions & 10 deletions wcc-contentful/lib/wcc/contentful/sync_engine.rb
Expand Up @@ -38,15 +38,15 @@ def initialize(state: nil, store: nil, client: nil, key: nil)
@mutex = Mutex.new

if store
@fetch_method = FETCH_METHODS.find { |m| store.respond_to?(m) }
@read_method = READ_METHODS.find { |m| store.respond_to?(m) }
@write_method = WRITE_METHODS.find { |m| store.respond_to?(m) }
unless @fetch_method && @write_method
raise ArgumentError, ":store param must implement one of #{FETCH_METHODS}" \
unless @read_method && @write_method
raise ArgumentError, ":store param must implement one of #{READ_METHODS}" \
" AND one of #{WRITE_METHODS}"
end

@store = store
@state = fetch if should_sync?
@state = read_state if should_sync?
end
if state
@state = { 'token' => state } if state.is_a? String
Expand All @@ -70,7 +70,7 @@ def next(up_to_id: nil)
count = 0

@mutex.synchronize do
@state ||= fetch || {}
@state ||= read_state || {}
next_sync_token = @state['token']

sync_resp = client.sync(sync_token: next_sync_token)
Expand All @@ -87,13 +87,13 @@ def next(up_to_id: nil)
end

@state['token'] = sync_resp.next_sync_token
write
write_state
end

[id_found, count]
end

FETCH_METHODS = %i[fetch find].freeze
READ_METHODS = %i[fetch find].freeze
WRITE_METHODS = %i[write set].freeze

def emit_event(event)
Expand All @@ -105,11 +105,11 @@ def emit_event(event)

private

def fetch
store&.public_send(@fetch_method, @state_key)
def read_state
store&.public_send(@read_method, @state_key)
end

def write
def write_state
store&.public_send(@write_method, @state_key, @state)
end

Expand All @@ -124,6 +124,8 @@ class Job < ActiveJob::Base
queue_as :default

def perform(event = nil)
return unless sync_engine&.should_sync?

up_to_id = nil
up_to_id = event[:up_to_id] || event.dig('sys', 'id') if event
sync!(up_to_id: up_to_id)
Expand Down
35 changes: 35 additions & 0 deletions wcc-contentful/spec/wcc/contentful/store/postgres_store_spec.rb
Expand Up @@ -49,4 +49,39 @@
# assert
expect(results).to eq([data, data, data])
end

context 'db does not exist' do
subject {
WCC::Contentful::Store::PostgresStore.new(double('Configuration'),
{ dbname: 'asdf' }, size: 5)
}

it '#set raises error' do
expect {
subject.set('foo', { 'key' => 'val' })
}.to raise_error(PG::ConnectionBad)
end

it '#delete raises error' do
expect {
subject.delete('foo')
}.to raise_error(PG::ConnectionBad)
end

# DB should not need to exist in order to run rake db:setup
it '#find returns nil' do
result = subject.find('foo')
expect(result).to be nil
end

it '#find_all returns empty' do
result = subject.find_all(content_type: 'foo').to_a
expect(result).to eq([])
end

it '#keys returns empty' do
result = subject.keys.to_a
expect(result).to eq([])
end
end
end
28 changes: 28 additions & 0 deletions wcc-contentful/spec/wcc/contentful_spec.rb
Expand Up @@ -504,5 +504,33 @@
expect(button2.text).to eq('About')
end
end

context 'content_delivery = eager_sync' do
before(:each) do
WCC::Contentful.configure do |config|
config.management_token = contentful_management_token
config.store = nil
config.content_delivery = :eager_sync
end
end

# Checking and advancing the sync engine needs to happen post initialization
# in an asynchronous background job. It should never be advanced during
# the rails init process or it will interfere with rake tasks.
it 'should not access the sync engine during initialization' do
expect(WCC::Contentful::Services.instance).to_not receive(:sync_engine)
allow(WCC::Contentful::SyncEngine::Job).to receive(:perform_later)

# act
WCC::Contentful.init!
end

it 'should perform a sync job' do
expect(WCC::Contentful::SyncEngine::Job).to receive(:perform_later)

# act
WCC::Contentful.init!
end
end
end
end

0 comments on commit 0187d67

Please sign in to comment.