Skip to content

Commit

Permalink
Update dependency on console gem and modernize usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 4, 2024
1 parent b4f871d commit 89ec929
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion async.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = ">= 3.1.1"

spec.add_dependency "console", "~> 1.10"
spec.add_dependency "console", ["~> 1.25", ">= 1.25.2"]
spec.add_dependency "fiber-annotation"
spec.add_dependency "io-event", ["~> 1.5", ">= 1.5.1"]
spec.add_dependency "timers", "~> 4.1"
Expand Down
12 changes: 6 additions & 6 deletions examples/capture/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def strace(pid, duration = 60)

_, status = Process.waitpid2(pid)

Console.logger.error(status) do |buffer|
Console.error(status) do |buffer|
buffer.puts first_line
end unless status.success?

Expand All @@ -92,28 +92,28 @@ def strace(pid, duration = 60)

pids.each do |pid|
start_times = getrusage(pid)
Console.logger.info("Process #{pid} start times:", start_times)
Console.info("Process #{pid} start times:", start_times)

# sleep 60
summary = strace(pid)

Console.logger.info("strace -p #{pid}") do |buffer|
Console.info("strace -p #{pid}") do |buffer|
summary.each do |fields|
buffer.puts fields.inspect
end
end

end_times = getrusage(pid)
Console.logger.info("Process #{pid} end times:", end_times)
Console.info("Process #{pid} end times:", end_times)

if total = summary[:total]
process_duration = end_times.utime - start_times.utime
wait_duration = summary[:total][:seconds]

Console.logger.info("Process Waiting: #{wait_duration.round(4)}s out of #{process_duration.round(4)}s") do |buffer|
Console.info("Process Waiting: #{wait_duration.round(4)}s out of #{process_duration.round(4)}s") do |buffer|
buffer.puts "Wait percentage: #{(wait_duration / process_duration * 100.0).round(2)}%"
end
else
Console.logger.warn("No system calls detected.")
Console.warn("No system calls detected.")
end
end
2 changes: 1 addition & 1 deletion examples/hup-test/child.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Async do |task|
while true
task.async do
Console.logger.info("Child running.")
Console.info("Child running.")
sleep 0.1
end.wait
end
Expand Down
6 changes: 3 additions & 3 deletions examples/hup-test/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

while true
pid = Process.spawn("./child.rb")
Console.logger.info("Spawned child.", pid: pid)
Console.info("Spawned child.", pid: pid)
sleep 2
Console.logger.info("Sending HUP to child.", pid: pid)
Console.info("Sending HUP to child.", pid: pid)
Process.kill(:HUP, pid)
status = Process.waitpid(pid)
Console.logger.info("Child exited.", pid: pid, status: status)
Console.info("Child exited.", pid: pid, status: status)
end
2 changes: 1 addition & 1 deletion guides/event-loop/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Most methods of the reactor and related tasks are not thread-safe, so you'd typi
~~~ ruby
require 'async'

Console.logger.debug!
Console.debug!
reactor = Async::Reactor.new

# Run the reactor for 1 second:
Expand Down
4 changes: 2 additions & 2 deletions lib/async/barrier.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require 'async/barrier'

barrier = Async::Barrier.new
Sync do
Console.logger.info("Barrier Example: sleep sort.")
Console.info("Barrier Example: sleep sort.")

# Generate an array of 10 numbers:
numbers = 10.times.map{rand(10)}
Expand All @@ -26,7 +26,7 @@ Sync do
# Wait for all the numbers to be sorted:
barrier.wait

Console.logger.info("Sorted", sorted)
Console.info("Sorted", sorted)
ensure
# Ensure all the tasks are stopped when we exit:
barrier.stop
Expand Down
6 changes: 3 additions & 3 deletions lib/async/condition.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Sync do
condition = Async::Condition.new

Async do
Console.logger.info "Waiting for condition..."
Console.info "Waiting for condition..."
value = condition.wait
Console.logger.info "Condition was signalled: #{value}"
Console.info "Condition was signalled: #{value}"
end

Async do |task|
task.sleep(1)
Console.logger.info "Signalling condition..."
Console.info "Signalling condition..."
condition.signal("Hello World")
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/async/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def run(...)

return initial_task
ensure
Console.logger.debug(self) {"Exiting run-loop because #{$! ? $! : 'finished'}."}
Console.debug(self) {"Exiting run-loop because #{$! ? $! : 'finished'}."}
end

# Start an asynchronous task within the specified reactor. The task will be
Expand All @@ -395,7 +395,7 @@ def async(*arguments, **options, &block)
# - Avoid scheduler overhead if no blocking operation is performed.
task.run(*arguments)

# Console.logger.debug "Initial execution of task #{fiber} complete (#{result} -> #{fiber.alive?})..."
# Console.debug "Initial execution of task #{fiber} complete (#{result} -> #{fiber.alive?})..."
return task
end

Expand Down
4 changes: 2 additions & 2 deletions lib/async/semaphore.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Sync do
# Search for the terms:
terms.each do |term|
semaphore.async do |task|
Console.logger.info("Searching for #{term}...")
Console.info("Searching for #{term}...")
response = Net::HTTP.get(URI "https://www.google.com/search?q=#{term}")
Console.logger.info("Got response #{response.size} bytes.")
Console.info("Got response #{response.size} bytes.")
end
end
end
Expand Down
11 changes: 6 additions & 5 deletions lib/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Copyright, 2023, by Math Ieu.

require 'fiber'
require 'console/event/failure'

require_relative 'node'
require_relative 'condition'
Expand Down Expand Up @@ -335,15 +336,15 @@ def failed!(exception = false, propagate = true)
raise exception
elsif @finished.nil?
# If no one has called wait, we log this as a warning:
Console.logger.warn(self, "Task may have ended with unhandled exception.", exception)
Console::Event::Failure.for(exception).emit(self, "Task may have ended with unhandled exception.", severity: :warn)
else
Console.logger.debug(self, exception)
Console::Event::Failure.for(exception).emit(self, severity: :debug)
end
end
end

def stopped!
# Console.logger.info(self, status:) {"Task #{self} was stopped with #{@children&.size.inspect} children!"}
# Console.info(self, status:) {"Task #{self} was stopped with #{@children&.size.inspect} children!"}
@status = :stopped

stopped = false
Expand Down Expand Up @@ -374,15 +375,15 @@ def schedule(&block)

begin
completed!(yield)
# Console.logger.debug(self) {"Task was completed with #{@children.size} children!"}
# Console.debug(self) {"Task was completed with #{@children.size} children!"}
rescue Stop
stopped!
rescue StandardError => error
failed!(error, false)
rescue Exception => exception
failed!(exception, true)
ensure
# Console.logger.info(self) {"Task ensure $! = #{$!} with #{@children&.size.inspect} children!"}
# Console.info(self) {"Task ensure $! = #{$!} with #{@children&.size.inspect} children!"}
finish!
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/async/waiter.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Sync do
# Stop all tasks which we don't care about:
barrier.stop

Console.logger.info("Smallest", numbers)
Console.info("Smallest", numbers)
end
~~~

Expand Down
2 changes: 1 addition & 1 deletion test/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ def sleep_forever

expect(task.result).to be_nil

Console.logger.debug(self) {"Stopping task..."}
Console.debug(self) {"Stopping task..."}
task.stop

expect(task.result).to be_nil
Expand Down
2 changes: 1 addition & 1 deletion test/kernel/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
end

it "can propagate error without logging them" do
expect(Console.logger).not.to receive(:error)
expect(Console).not.to receive(:error)

expect do
Sync do
Expand Down

0 comments on commit 89ec929

Please sign in to comment.