Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calendar synchronisation #125

Merged
merged 31 commits into from
Jul 14, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f3f059e
Drop `update_calendar` and associated code.
Jul 8, 2016
6a4a0a4
Rename Workers::Synchronisation to Workers::PropertySynchronisation.
Jul 8, 2016
982a829
Introduce background workers, configurable per supplier.
Jul 11, 2016
4dd4ee3
Ability to filter workers for a given supplier.
Jul 11, 2016
61c05a8
Include flows for creating suppliers and associated workers.
Jul 11, 2016
afb4826
Drop the contradicting on_delete: set null option on foreign keys.
Jul 11, 2016
5438de3
Flexible supplier sync workers configuration via suppliers.yml.
Jul 11, 2016
74dff28
Drops `next_run_at` column from hosts.
Jul 11, 2016
76dd403
Rename supplier creation flow to host creation flow.
Jul 12, 2016
7393342
Ability to query for hosts by identifier and supplier.
Jul 12, 2016
cdbc667
Query for backround workers associated with a host instead of supplier.
Jul 12, 2016
85d8939
Create background workers associated with hosts.
Jul 12, 2016
e046e38
Revert suppliers rake task to simplicity.
Jul 12, 2016
e004485
Run host creation in a database transaction.
Jul 12, 2016
1e66a17
Include rake task to update workers definition for all hosts.
Jul 12, 2016
e04b467
Support querying for pending background workers.
Jul 12, 2016
12c3cae
Schedule workers rather than hosts.
Jul 12, 2016
df7a08b
Process background workers instead of sync operations.
Jul 12, 2016
f985e48
Background worker coordination by the queue processor.
Jul 12, 2016
a3a4294
Update AtLeisure event name according to recent changes.
Jul 12, 2016
5041bf7
Introduce Roomorama::Calendar to specify rates and availabilities.
Jul 12, 2016
45be50a
Add new UpdateCalendar Roomorama API operation.
Jul 12, 2016
9e39461
Include type of synchronisation worker when sync process starts.
Jul 12, 2016
5050890
Add stats and type to sync processes.
Jul 13, 2016
d7831ce
Migrate to stats field on property synchronisation.
Jul 13, 2016
0d1d969
Validate calendars when required.
Jul 13, 2016
d230af3
Operation runner for calendar updates.
Jul 13, 2016
bd1ff84
Introduce a CalendarSynchronisation helper class.
Jul 13, 2016
267b475
Permanently delete property counters from sync_processes.
Jul 13, 2016
9600f7b
Update change log.
Jul 13, 2016
cc681fb
Documentation fixes.
Jul 14, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Background worker coordination by the queue processor.
  • Loading branch information
Renato Mascarenhas committed Jul 12, 2016
commit f985e48235118f8999cd537237a6f5cef4bb9a9b
8 changes: 2 additions & 6 deletions apps/workers/operation_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ def initialize(host)
# Extra arguments depend on the specific implementation of the runner.
# Check particular classes for more information.
def perform(operation, *args)
runner_for(operation).perform(*args).tap do |result|
update_next_run if result.success?
end
runner_for(operation).perform(*args)
end

private
Expand All @@ -68,10 +66,8 @@ def roomorama_client
end

# if the result of performing the operation was successful, we update
# the timestamp when the next synchronisation for the given host
# should happen.
# the timestamp when the next execution of the worker should happen.
def update_next_run
return true
one_day = 24 * 60 * 60 # TODO make this a dynamic value
host.next_run_at = Time.now + one_day

Expand Down
43 changes: 36 additions & 7 deletions apps/workers/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ def process!
def run_worker(args)
worker = BackgroundWorkerRepository.find(args[:background_worker_id])

timing_out(worker.type, args) do
host = HostRepository.find(worker.host_id)
supplier = SupplierRepository.find(host.supplier_id)
broadcast = [worker.type, ".", supplier.name].join

Concierge::Announcer.trigger(broadcast, host)
Result.new(true)
running(worker) do
timing_out(worker.type, args) do
host = HostRepository.find(worker.host_id)
supplier = SupplierRepository.find(host.supplier_id)
broadcast = [worker.type, ".", supplier.name].join

Concierge::Announcer.trigger(broadcast, host)
Result.new(true)
end
end
end

Expand All @@ -91,10 +93,37 @@ def timing_out(operation, params)
Result.error(:timeout)
end

# coordinates the +BackgroundWorker+ instance status and timestamps by changing
# the worker status to +running+, yielding the block (which is supposed to do
# the worker's job), and ensuring that the worker's status is set back to +idle+
# at the end of the process, as well as properly updating the +next_run_at+ column
# according to the specified worker +interval+.
def running(worker)
worker_started(worker)
yield

ensure
# reload the worker instance to make sure to account for any possible
# changes in the process
worker_completed(BackgroundWorkerRepository.find(worker.id))
end

def message
@message = json_decode(payload)
end

def worker_started(worker)
worker.status = "running"
BackgroundWorkerRepository.update(worker)
end

def worker_completed(worker)
worker.status = "idle"
worker.next_run_at = Time.now + worker.interval

BackgroundWorkerRepository.update(worker)
end

# NOTE this time out should be shorter than the +VisibilityTimeout+ configured
# on the SQS queue to be used by Concierge.
def processing_timeout
Expand Down
12 changes: 0 additions & 12 deletions spec/workers/operation_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@
subject.perform(operation)
end

xit "updates host next synchrnonisation time when successful" do
allow(subject).to receive(:runner_for) { double(perform: Result.new(true)) }
expect(host.next_run_at).to be_nil

identifiers = ["prop1"]
operation = Roomorama::Client::Operations.disable(identifiers)
subject.perform(operation)

updated = HostRepository.find(host.id)
expect(updated.next_run_at > Time.now).to eq true
end

it "raises an error if the operation is not recognised" do
operation = nil
expect {
Expand Down
16 changes: 11 additions & 5 deletions spec/workers/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,27 @@
it "triggers the associated supplier synchronisation mechanism on background worker operations" do
invoked = false

supplier = create_supplier(name: "AcmeTest")
host = create_host(username: "acme-host", supplier_id: supplier.id)
worker = create_background_worker(host_id: host.id, type: "metadata", interval: 10)
payload[:data][:background_worker_id] = worker.id

Concierge::Announcer.on("metadata.AcmeTest") do |host|
expect(host.username).to eq "acme-host"
expect(SupplierRepository.find(host.supplier_id).name).to eq "AcmeTest"
invoked = true
end

supplier = create_supplier(name: "AcmeTest")
host = create_host(username: "acme-host", supplier_id: supplier.id)
worker = create_background_worker(host_id: host.id, type: "metadata")
payload[:data][:background_worker_id] = worker.id
expect(BackgroundWorkerRepository.find(worker.id).status).to eq "running"
end

result = subject.process!
expect(result).to be_a Result
expect(result).to be_success
expect(invoked).to eq true

reloaded_worker = BackgroundWorkerRepository.find(worker.id)
expect(reloaded_worker.next_run_at - Time.now).to be_within(1).of(worker.interval)
expect(reloaded_worker.status).to eq "idle"
end

it "times out and fails if the operation takes too long" do
Expand Down