Fluentd
Search…
Plugin Helper: Thread
The thread plugin helper manages threads and these threads are integrated with plugins. No need manual run or shutdown in the plugin.
Here is an example:
1
require 'fluent/plugin/output'
2
3
module Fluent::Plugin
4
class ExampleOutput < Output
5
Fluent::Plugin.register_output('example', self)
6
7
# 1. Load thread helper
8
helpers :thread
9
10
# Omit `configure`, `shutdown` and other plugin APIs
11
12
def start
13
super
14
15
# 2. Create and run thread with unique name.
16
thread_create(:example_thread_run, &method(:run))
17
end
18
19
def run
20
# ...
21
end
22
end
23
end
Copied!
The launched thread is managed by the plugin helper. No need of thread shutdown code in plugin's shutdown method. It shutdowns the launched threads automatically.

Methods

thread_create(title)

This method creates a thread and runs it with the given routine.
title must be unique.
1
# Pass block directly
2
thread_create(:example_plugin_main) {
3
# ...
4
}
5
6
# Pass method object with existing method
7
thread_create(:foo_plugin_body, &method(:run))
8
def run
9
# ...
10
end
Copied!

thread_current_running?

Checks whether the current thread is running or not. This method is available in the running block.
1
thread_create(:example_plugin_main) {
2
while thread_current_running?
3
# ...
4
end
5
}
Copied!

Plugins using thread

If this article is incorrect or outdated, or omits critical information, please let us know. Fluentd is an open-source project under Cloud Native Computing Foundation (CNCF). All components are available under the Apache 2 License.
Last modified 4mo ago