Skip to content

handle error with error handler in async scheduler #248

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions lib/optimizely/config_manager/async_scheduler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2019, Optimizely and contributors
# Copyright 2019-2020, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -19,20 +19,22 @@ module Optimizely
class AsyncScheduler
attr_reader :running

def initialize(callback, interval, auto_update, logger = nil)
def initialize(callback, interval, auto_update, logger = nil, error_handler = nil)
# Sets up AsyncScheduler to execute a callback periodically.
#
# callback - Main function to be executed periodically.
# interval - How many seconds to wait between executions.
# auto_update - boolean indicates to run infinitely or only once.
# logger - Optional Provides a logger instance.
# error_handler - Optional Provides a handle_error method to handle exceptions.

@callback = callback
@interval = interval
@auto_update = auto_update
@running = false
@thread = nil
@logger = logger || NoOpLogger.new
@error_handler = error_handler || NoOpErrorHandler.new
end

def start!
Expand All @@ -54,6 +56,7 @@ def start!
Logger::ERROR,
"Couldn't create a new thread for async scheduler. #{e.message}"
)
@error_handler.handle_error(e)
end
end

Expand All @@ -80,6 +83,7 @@ def execution_wrapper(callback)
Logger::ERROR,
"Something went wrong when executing passed callback. #{e.message}"
)
@error_handler.handle_error(e)
stop!
end
break unless @auto_update
Expand Down
27 changes: 21 additions & 6 deletions spec/config_manager/async_scheduler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2019, Optimizely and contributors
# Copyright 2019-2020, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -17,21 +17,36 @@
#
require 'spec_helper'
require 'optimizely/config_manager/async_scheduler'
require 'optimizely/error_handler'
require 'optimizely/logger'

describe Optimizely::AsyncScheduler do
let(:error_handler) { Optimizely::RaiseErrorHandler.new }
let(:spy_logger) { spy('logger') }

it 'should log error trace when callback fails to execute' do
allow(Thread).to receive(:new).and_yield
def some_callback(_args); end

spy_logger = spy('logger')

scheduler = Optimizely::AsyncScheduler.new(method(:some_callback), 10, false, spy_logger)
scheduler.start!
scheduler = Optimizely::AsyncScheduler.new(method(:some_callback), 10, false, spy_logger, error_handler)

while scheduler.running; end
expect { scheduler.start! }.to raise_error(StandardError)
expect(spy_logger).to have_received(:log).with(
Logger::ERROR,
'Something went wrong when executing passed callback. wrong number of arguments (given 0, expected 1)'
)
end

it 'should log error trace when new thread creation fails' do
allow(Thread).to receive(:new).and_raise
def some_callback(_args); end

scheduler = Optimizely::AsyncScheduler.new(method(:some_callback), 10, false, spy_logger, error_handler)

expect { scheduler.start! }.to raise_error(StandardError)
expect(spy_logger).to have_received(:log).with(
Logger::ERROR,
"Couldn't create a new thread for async scheduler. "
)
end
end