Skip to content

Fix saving pipeline state using record_last_run setting #374

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/logstash/plugin_mixins/jdbc/jdbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def execute_statement
rescue Sequel::DatabaseConnectionError, Sequel::DatabaseError, Java::JavaSql::SQLException => e
@logger.warn("Exception when executing JDBC query", :exception => e)
else
@value_tracker.set_value(sql_last_value)
@value_tracker.set_value(sql_last_value) if @record_last_run
ensure
close_jdbc_connection
@connection_lock.unlock
Expand Down
44 changes: 44 additions & 0 deletions spec/inputs/jdbc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,50 @@
end
end

context "when state is not to be persisted iteratively" do
let(:mixin_settings) do
{ "jdbc_user" => ENV['USER'], "jdbc_driver_class" => "org.apache.derby.jdbc.EmbeddedDriver",
"jdbc_connection_string" => "jdbc:derby:memory:testdb;create=true"
}
end

let(:settings) do
{
"statement" => "SELECT num, created_at, custom_time FROM test_table WHERE custom_time > :sql_last_value",
"record_last_run" => false,
"clean_run" => true
}
end

let(:nums) { [10, 20, 30, 40, 50] }
let(:times) {["2015-05-06 13:14:15","2015-05-07 13:14:15","2015-05-08 13:14:15","2015-05-09 13:14:15","2015-05-10 13:14:15"]}

before do
plugin.register
end

after do
plugin.stop
end

it "should successfully always start from the very beginning" do
test_table = db[:test_table]

plugin.run(queue)
expect(plugin.instance_variable_get("@value_tracker").value).to eq(Time.parse("1970-01-01 00:00:00.000000000 +0000"))
test_table.insert(:num => nums[0], :created_at => Time.now.utc, :custom_time => times[0])
test_table.insert(:num => nums[1], :created_at => Time.now.utc, :custom_time => times[1])

plugin.run(queue)
expect(plugin.instance_variable_get("@value_tracker").value).to eq(Time.parse("1970-01-01 00:00:00.000000000 +0000"))
test_table.insert(:num => nums[2], :created_at => Time.now.utc, :custom_time => times[2])
test_table.insert(:num => nums[3], :created_at => Time.now.utc, :custom_time => times[3])
test_table.insert(:num => nums[4], :created_at => Time.now.utc, :custom_time => times[4])

plugin.run(queue)
expect(plugin.instance_variable_get("@value_tracker").value).to eq(Time.parse("1970-01-01 00:00:00.000000000 +0000"))
end
end

context "when state is not to be persisted" do
let(:settings) do
Expand Down