systemd-journal 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5c615cee8de41012105d99aec5b93f8c88f322b67bbf4d97bcb4afbd74cec35
4
- data.tar.gz: 84447c503addd9fc6c3c6564472e7eb1558f6e6a184a98214c604feb2649ecea
3
+ metadata.gz: 7645b91580b12afef524d1ef0b89e515f5d656e7f4e2e5cd0ffccfaa3fa84db5
4
+ data.tar.gz: 385370380f9a850cb8c7332034ea0222ff7ef9fc6bc6d3089c54543f6cfd1784
5
5
  SHA512:
6
- metadata.gz: dd1375594a013f9d237c0a62f5c4aed77f80b54637e1275cf32565905fde72187eb08feefc4d93129a3a942949de93d2f037b0af2893e95f52c006d682e32b39
7
- data.tar.gz: 9debfc4954cc65ddf9fda2d448c30b67c0995cf2ad554dcfb00b067103218a5898d8d9f619e6b04236d184742397d64b5365efbbd90cb0be6f26a25cbeb8887b
6
+ metadata.gz: 8b04180f1b9215ceb0711b7a03b23fbb26cea7a9e35add3bc3c5e3151b0ac29bb5fcbb213c58bdc8d0c526395bbf2bad843e54602abc2669ed623815366f7e97
7
+ data.tar.gz: ffe8e22c89d32ec57b13300514717c79993fa67f8516c9b1eb54793b4201097f7d3e9a16ba1c16f9fed10e078c76ee2be999b4598eeb7820dc19b68ce2400f36
@@ -1,5 +1,5 @@
1
1
  name: RSpec Matrix
2
- on: [push]
2
+ on: [push, pull_request]
3
3
  jobs:
4
4
  test:
5
5
  strategy:
data/README.md CHANGED
@@ -80,6 +80,16 @@ j.current_entry # get the entry we're currently positioned at
80
80
  j.seek(Time.parse('2013-10-31T12:00:00+04:00:00'))
81
81
  ```
82
82
 
83
+ Re-open the journal automatically to reduce memory usage with moving the journal:
84
+
85
+ ```ruby
86
+ Journal.new(auto_reopen: false) # do not ever re-open the journal.
87
+ # this should be the default for now
88
+
89
+ Journal.new(auto_reopen: true) # re-open the journal after the default ITERATIONS_TO_AUTO_REOPEN
90
+ Journal.new(auto_reopen: 50_000) # re-open the journal after 50k iterations
91
+ ```
92
+
83
93
  Waiting for things to happen:
84
94
 
85
95
  ```ruby
@@ -1,6 +1,9 @@
1
1
  module Systemd
2
2
  class Journal
3
3
  module Navigable
4
+ ITERATIONS_TO_AUTO_REOPEN = 10_000
5
+ private_constant :ITERATIONS_TO_AUTO_REOPEN
6
+
4
7
  # returns a string representing the current read position.
5
8
  # This string can be passed to {#seek} or {#cursor?}.
6
9
  # @return [String] a cursor token.
@@ -40,9 +43,11 @@ module Systemd
40
43
  # @return [Boolean] False if unable to move to the next entry, indicating
41
44
  # that the pointer has reached the end of the journal.
42
45
  def move_next
43
- rc = Native.sd_journal_next(@ptr)
44
- raise JournalError, rc if rc < 0
45
- rc > 0
46
+ with_auto_reopen {
47
+ rc = Native.sd_journal_next(@ptr)
48
+ raise JournalError, rc if rc < 0
49
+ rc > 0
50
+ }
46
51
  end
47
52
 
48
53
  # Move the read pointer forward by `amount` entries.
@@ -50,9 +55,11 @@ module Systemd
50
55
  # moved. If this number is less than the requested amount, the read
51
56
  # pointer has reached the end of the journal.
52
57
  def move_next_skip(amount)
53
- rc = Native.sd_journal_next_skip(@ptr, amount)
54
- raise JournalError, rc if rc < 0
55
- rc
58
+ with_auto_reopen {
59
+ rc = Native.sd_journal_next_skip(@ptr, amount)
60
+ raise JournalError, rc if rc < 0
61
+ rc
62
+ }
56
63
  end
57
64
 
58
65
  # Move the read pointer to the previous entry in the journal.
@@ -60,9 +67,11 @@ module Systemd
60
67
  # @return [Boolean] False if unable to move to the previous entry,
61
68
  # indicating that the pointer has reached the beginning of the journal.
62
69
  def move_previous
63
- rc = Native.sd_journal_previous(@ptr)
64
- raise JournalError, rc if rc < 0
65
- rc > 0
70
+ with_auto_reopen {
71
+ rc = Native.sd_journal_previous(@ptr)
72
+ raise JournalError, rc if rc < 0
73
+ rc > 0
74
+ }
66
75
  end
67
76
 
68
77
  # Move the read pointer backwards by `amount` entries.
@@ -70,9 +79,11 @@ module Systemd
70
79
  # was moved. If this number is less than the requested amount, the
71
80
  # read pointer has reached the beginning of the journal.
72
81
  def move_previous_skip(amount)
73
- rc = Native.sd_journal_previous_skip(@ptr, amount)
74
- raise JournalError, rc if rc < 0
75
- rc
82
+ with_auto_reopen {
83
+ rc = Native.sd_journal_previous_skip(@ptr, amount)
84
+ raise JournalError, rc if rc < 0
85
+ rc
86
+ }
76
87
  end
77
88
 
78
89
  # Seek to a position in the journal.
@@ -90,7 +101,7 @@ module Systemd
90
101
  # and seek to that entry.
91
102
  # @return [True]
92
103
  # @example Read last journal entry
93
- # j = Systemd::Joural.new
104
+ # j = Systemd::Journal.new
94
105
  # j.seek(:tail)
95
106
  # j.move_previous
96
107
  # puts j.current_entry
@@ -115,6 +126,35 @@ module Systemd
115
126
 
116
127
  true
117
128
  end
129
+
130
+ private
131
+
132
+ # reopen the journal automatically due to reduce memory usage
133
+ def with_auto_reopen
134
+ @sd_call_count ||= 0
135
+
136
+ ret = yield
137
+
138
+ if auto_reopen
139
+ @sd_call_count += 1
140
+ if @sd_call_count >= auto_reopen
141
+ cursor = self.cursor
142
+
143
+ self.close
144
+ self.initialize(@reopen_options)
145
+
146
+ self.seek(cursor)
147
+ # To avoid 'Cannot assign requested address' error
148
+ # It invokes native API directly to avoid nest with_auto_reopen calls
149
+ rc = Native.sd_journal_next_skip(@ptr, 0)
150
+ raise JournalError, rc if rc < 0
151
+
152
+ @sd_call_count = 0
153
+ end
154
+ end
155
+
156
+ ret
157
+ end
118
158
  end
119
159
  end
120
160
  end
@@ -1,6 +1,6 @@
1
1
  module Systemd
2
2
  class Journal
3
3
  # The version of the systemd-journal gem.
4
- VERSION = '2.0.0'.freeze
4
+ VERSION = '2.1.0'.freeze
5
5
  end
6
6
  end
@@ -26,11 +26,14 @@ module Systemd
26
26
  include Systemd::Journal::Filterable
27
27
  include Systemd::Journal::Waitable
28
28
 
29
+ # Returns the iterations to auto reopen
30
+ attr_reader :auto_reopen
31
+
29
32
  # Returns a new instance of a Journal, opened with the provided options.
30
33
  # @param [Hash] opts optional initialization parameters.
31
34
  # @option opts [Integer] :flags a set of bitwise OR-ed
32
35
  # {Systemd::Journal::Flags} which control what journal files are opened.
33
- # Defaults to `0`, meaning all journals avaiable to the current user.
36
+ # Defaults to `0`, meaning all journals available to the current user.
34
37
  # @option opts [String] :path if provided, open the journal files living
35
38
  # in the provided directory only. Any provided flags will be ignored
36
39
  # since sd_journal_open_directory does not currently accept any flags.
@@ -46,9 +49,14 @@ module Systemd
46
49
  # path: '/var/log/journal/5f5777e46c5f4131bd9b71cbed6b9abf'
47
50
  # )
48
51
  def initialize(opts = {})
52
+ @reopen_options = opts.dup # retain the options for auto reopen
49
53
  open_type, flags = validate_options!(opts)
50
54
  ptr = FFI::MemoryPointer.new(:pointer, 1)
51
55
 
56
+ @auto_reopen = (opts.key?(:auto_reopen) ? opts.delete(:auto_reopen) : false)
57
+ if @auto_reopen
58
+ @auto_reopen = @auto_reopen.is_a?(Integer) ? @auto_reopen : ITERATIONS_TO_AUTO_REOPEN
59
+ end
52
60
  @finalize = (opts.key?(:finalize) ? opts.delete(:finalize) : true)
53
61
  rc = open_journal(open_type, ptr, opts, flags)
54
62
  raise JournalError, rc if rc < 0
@@ -1,7 +1,7 @@
1
1
  require 'ffi'
2
2
 
3
3
  module Systemd
4
- # This execption is raised whenever a sd_journal_* call returns an error.
4
+ # This exception is raised whenever a sd_journal_* call returns an error.
5
5
  class JournalError < StandardError
6
6
  # Returns the (positive) error number.
7
7
  attr_reader :code
@@ -75,7 +75,7 @@ RSpec.describe Systemd::JournalEntry do
75
75
  .to eq('Process 123 said test message')
76
76
  end
77
77
 
78
- it 'skips field substition if requested' do
78
+ it 'skips field substitution if requested' do
79
79
  expect(entry.catalog(replace: false)).to eq(catalog)
80
80
  end
81
81
  end
@@ -41,6 +41,39 @@ RSpec.describe Systemd::Journal do
41
41
 
42
42
  expect { j.new(container: 'test') }.to raise_error(ArgumentError)
43
43
  end
44
+
45
+ context 'auto_reopen' do
46
+ it 'returns nil as default behavior' do
47
+ journal = Systemd::Journal.new()
48
+ expect(journal.auto_reopen).to be_falsy
49
+ end
50
+
51
+ it 'returns default iterations with true' do
52
+ journal = Systemd::Journal.new(auto_reopen: true)
53
+ expect(journal.auto_reopen).to be_truthy
54
+ end
55
+
56
+ it 'returns nil with false' do
57
+ journal = Systemd::Journal.new(auto_reopen: false)
58
+ expect(journal.auto_reopen).to be_falsy
59
+ end
60
+
61
+ it 'returns iterations with custom value' do
62
+ journal = Systemd::Journal.new(auto_reopen: 12345)
63
+ expect(journal.auto_reopen).to be 12345
64
+ end
65
+
66
+ it 'should re-open internal journal pointer at specified iterations' do
67
+ journal = Systemd::Journal.new(auto_reopen: 2)
68
+ internal_journal = journal.instance_variable_get(:@ptr)
69
+
70
+ journal.move_next
71
+ expect(journal.instance_variable_get(:@ptr)).to be internal_journal
72
+
73
+ journal.move_next
74
+ expect(journal.instance_variable_get(:@ptr)).to_not be internal_journal
75
+ end
76
+ end
44
77
  end
45
78
 
46
79
  describe 'close' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: systemd-journal
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Ledbetter
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-09-11 00:00:00.000000000 Z
12
+ date: 2025-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi
@@ -187,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
187
  - !ruby/object:Gem::Version
188
188
  version: '0'
189
189
  requirements: []
190
- rubygems_version: 3.5.18
190
+ rubygems_version: 3.5.23
191
191
  signing_key:
192
192
  specification_version: 4
193
193
  summary: Ruby bindings to libsystemd-journal