Skip to content

Commit 6caed0c

Browse files
authored
Refactor: start using scheduler mixin (#134)
+ remove EoTime initialization - now part of mixin
1 parent 1bd6984 commit 6caed0c

File tree

4 files changed

+16
-26
lines changed

4 files changed

+16
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 5.4.0
2+
- Refactor: start using scheduler mixin [#134](https://github.com/logstash-plugins/logstash-input-http_poller/pull/134)
3+
14
## 5.3.1
25
- Fix: make sure plugin is closing the http client [#130](https://github.com/logstash-plugins/logstash-input-http_poller/pull/130)
36

lib/logstash/inputs/http_poller.rb

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
require "logstash/plugin_mixins/http_client"
55
require "socket" # for Socket.gethostname
66
require "manticore"
7-
require "rufus/scheduler"
87
require "logstash/plugin_mixins/ecs_compatibility_support"
98
require 'logstash/plugin_mixins/ecs_compatibility_support/target_check'
109
require 'logstash/plugin_mixins/validator_support/field_reference_validation_adapter'
1110
require 'logstash/plugin_mixins/event_support/event_factory_adapter'
11+
require 'logstash/plugin_mixins/scheduler'
1212

1313
class LogStash::Inputs::HTTP_Poller < LogStash::Inputs::Base
1414
include LogStash::PluginMixins::HttpClient
@@ -18,6 +18,8 @@ class LogStash::Inputs::HTTP_Poller < LogStash::Inputs::Base
1818

1919
extend LogStash::PluginMixins::ValidatorSupport::FieldReferenceValidationAdapter
2020

21+
include LogStash::PluginMixins::Scheduler
22+
2123
config_name "http_poller"
2224

2325
default :codec, "json"
@@ -45,7 +47,6 @@ class LogStash::Inputs::HTTP_Poller < LogStash::Inputs::Base
4547
config :metadata_target, :validate => :string, :default => '@metadata'
4648

4749
public
48-
Schedule_types = %w(cron every at in)
4950
def register
5051
@host = Socket.gethostname.force_encoding(Encoding::UTF_8)
5152

@@ -55,15 +56,15 @@ def register
5556

5657
# @overload
5758
def stop
58-
shutdown_scheduler_and_close_client(:wait)
59+
close_client
5960
end
6061

6162
# @overload
6263
def close
63-
shutdown_scheduler_and_close_client
64+
close_client
6465
end
6566

66-
def shutdown_scheduler_and_close_client(opt = nil)
67+
def close_client
6768
@logger.debug("closing http client", client: client)
6869
begin
6970
client.close # since Manticore 0.9.0 this shuts-down/closes all resources
@@ -72,12 +73,8 @@ def shutdown_scheduler_and_close_client(opt = nil)
7273
details[:backtrace] = e.backtrace if @logger.debug?
7374
@logger.warn "failed closing http client", details
7475
end
75-
if @scheduler
76-
@logger.debug("shutting down scheduler", scheduler: @scheduler)
77-
@scheduler.shutdown(opt) # on newer Rufus (3.8) this joins on the scheduler thread
78-
end
7976
end
80-
private :shutdown_scheduler_and_close_client
77+
private :close_client
8178

8279
private
8380
def setup_requests!
@@ -185,12 +182,11 @@ def setup_schedule(queue)
185182
raise Logstash::ConfigurationError, msg_invalid_schedule if @schedule.keys.length != 1
186183
schedule_type = @schedule.keys.first
187184
schedule_value = @schedule[schedule_type]
188-
raise LogStash::ConfigurationError, msg_invalid_schedule unless Schedule_types.include?(schedule_type)
185+
raise LogStash::ConfigurationError, msg_invalid_schedule unless %w(cron every at in).include?(schedule_type)
189186

190-
@scheduler = Rufus::Scheduler.new(:max_work_threads => 1)
191-
opts = schedule_type == "every" ? { :first_in => 0.01 } : {}
192-
@scheduler.send(schedule_type, schedule_value, opts) { run_once(queue) }
193-
@scheduler.thread.join # due newer rufus (3.8) doing a blocking operation on scheduler.join
187+
opts = schedule_type == "every" ? { first_in: 0.01 } : {}
188+
scheduler.public_send(schedule_type, schedule_value, opts) { run_once(queue) }
189+
scheduler.join
194190
end
195191

196192
def run_once(queue)

logstash-input-http_poller.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'logstash-input-http_poller'
3-
s.version = '5.3.1'
3+
s.version = '5.4.0'
44
s.licenses = ['Apache License (2.0)']
55
s.summary = "Decodes the output of an HTTP API into events"
66
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
2121
s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
2222
s.add_runtime_dependency 'logstash-codec-plain'
2323
s.add_runtime_dependency "logstash-mixin-http_client", ">= 7.2.0"
24-
s.add_runtime_dependency 'rufus-scheduler', ">= 3.0.9"
24+
s.add_runtime_dependency 'logstash-mixin-scheduler', '~> 1.0'
2525
s.add_runtime_dependency 'logstash-mixin-ecs_compatibility_support', '~>1.3'
2626
s.add_runtime_dependency 'logstash-mixin-event_support', '~> 1.0', '>= 1.0.1'
2727
s.add_runtime_dependency 'logstash-mixin-validator_support', '~> 1.0'

spec/inputs/http_poller_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@
77
require 'rspec/matchers/built_in/raise_error.rb'
88
require 'logstash/plugin_mixins/ecs_compatibility_support/spec_helper'
99

10-
begin
11-
# TODO: CI work-around - will most likely be moved to the scheduler mixin
12-
require 'et-orbi.rb' # a dependency of rufus-scheduler since 3.4
13-
::EtOrbi::EoTime.now # might take a long time to initialize - loading time zone
14-
# data (from tz-info) and thus gets un-predictable on CI, since the scheduler worker
15-
# thread might be stuck starting while we attempt to shutdown in a given time frame
16-
rescue LoadError
17-
end
18-
1910
describe LogStash::Inputs::HTTP_Poller do
2011
let(:metadata_target) { "_http_poller_metadata" }
2112
let(:queue) { Queue.new }

0 commit comments

Comments
 (0)