-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add simple examples of gem usage #20
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'uber_task' | ||
require 'colorize' | ||
|
||
require_relative 'download_file' | ||
require_relative 'move_file' | ||
|
||
module Examples | ||
class DownloadAndMoveFile | ||
def self.run(source:, download_path:, move_path:) | ||
UberTask.run('Download and move file', retry_count: 2) do | ||
UberTask.on_success do | ||
UberTask.logger.info( | ||
'Top-level task was completed successfully!'.green, | ||
) | ||
end | ||
|
||
UberTask.on_subtask_error do | ||
UberTask.logger.info( | ||
'Unexpected subtask error can be caught on top-level task ' \ | ||
'and an early return can be made, ' \ | ||
'isn\'t that cool!?'.underline, | ||
) | ||
return # Try to remove this line and see what happens! | ||
end | ||
|
||
# Notice that we are passing `vital: true` option so that | ||
# an early return can be made if task fails | ||
begin | ||
Examples::DownloadFile.run( | ||
source: source, | ||
to: download_path, | ||
vital: true, | ||
) | ||
rescue StandardError => err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the error check inside on_subtask_error, which will show the main feature, centralized error reporting, and how the parent task can force a child task to skip or retry. Here's an example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved error check to |
||
UberTask.logger.info "Failed to download file -- #{err.message}" | ||
return | ||
end | ||
|
||
Examples::MoveFile.run(from: download_path, to: move_path) | ||
end | ||
end | ||
end | ||
end | ||
|
||
if __FILE__ == $PROGRAM_NAME | ||
Dir.mktmpdir do |dir| | ||
download_path = File.join(dir, 'test') | ||
move_path = File.join(dir, 'new_path') | ||
|
||
puts '--- Running successfull case ---' | ||
Examples::DownloadAndMoveFile.run( | ||
source: VALID_FILE_URI, | ||
download_path: download_path, | ||
move_path: move_path, | ||
) | ||
|
||
puts "\n--- Running case which fails to download file ---\n" | ||
Examples::DownloadAndMoveFile.run( | ||
source: NON_EXESTENT_FILE_URI, | ||
download_path: download_path, | ||
move_path: move_path, | ||
) | ||
|
||
puts "\n--- Running case with invalid download path ---\n" | ||
Examples::DownloadAndMoveFile.run( | ||
source: VALID_FILE_URI, | ||
download_path: '/invalid_path', | ||
move_path: move_path, | ||
) | ||
|
||
puts "\n--- Running case with invalid move path ---\n" | ||
Examples::DownloadAndMoveFile.run( | ||
source: VALID_FILE_URI, | ||
download_path: download_path, | ||
move_path: '/invalid_path', | ||
) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'uber_task' | ||
require 'colorize' | ||
require 'open-uri' | ||
require 'tmpdir' | ||
|
||
VALID_FILE_URI = 'https://github.com/shakacode/uber_task/raw/main/README.md' | ||
NON_EXESTENT_FILE_URI = 'https://github.com/shakacode/uber_task/raw/main/NON_EXISTENT.md' | ||
|
||
module Examples | ||
class DownloadFile | ||
def self.run(to:, source: VALID_FILE_URI, **task_options) | ||
UberTask.run( | ||
'Download file from external source', | ||
default_retry_wait: 3, | ||
retry_count: 5, | ||
vital: false, | ||
**task_options, | ||
) do | ||
UberTask.on_retry do | ||
UberTask.logger.info( | ||
"Retrying to download file from #{source}...".yellow, | ||
) | ||
end | ||
|
||
UberTask.on_success do | ||
UberTask.logger.info( | ||
"Downloaded file #{source} and saved it at #{to}".green, | ||
) | ||
end | ||
|
||
save_external_file!(source: source, to: to) | ||
end | ||
end | ||
|
||
def self.save_external_file!(source:, to:) | ||
URI.parse(source).open { |io| File.write(to, io.read) } | ||
rescue OpenURI::HTTPError => err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this check to the parent task on_subtask_error; all errors bubble up to the parent, like in Javascript and if the parent asks the task to be retried, the current task (child) will run again. This was the main idea to deploy apps on popmenu, we have a single place for common network errors and tasks can have many subtasks but all of them have common network errors and each time they happen, the on_subtask_error of the top task checks the error type and just ask to retry. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved |
||
raise UberTask::RetryTask.new(reason: err.message) | ||
end | ||
private_class_method :save_external_file! | ||
end | ||
end | ||
|
||
if __FILE__ == $PROGRAM_NAME | ||
Dir.mktmpdir do |dir| | ||
filepath = File.join(dir, 'test') | ||
|
||
puts '--- Running successfull case ---' | ||
Examples::DownloadFile.run(source: VALID_FILE_URI, to: filepath) | ||
|
||
puts "\n--- Running failing case with `vital: true`---\n" | ||
begin | ||
Examples::DownloadFile.run( | ||
source: NON_EXESTENT_FILE_URI, | ||
to: filepath, | ||
vital: true, | ||
) | ||
rescue StandardError | ||
UberTask.logger.info 'Error was raised'.red | ||
end | ||
|
||
puts "\n--- Running failing case with `vital: false`---\n" | ||
Examples::DownloadFile.run( | ||
source: NON_EXESTENT_FILE_URI, | ||
to: filepath, | ||
vital: false, | ||
) | ||
UberTask.logger.info 'Error wasn\'t raised'.green | ||
|
||
puts "\n--- Running failing case with `retry_count: 1`---\n" | ||
Examples::DownloadFile.run( | ||
source: NON_EXESTENT_FILE_URI, | ||
to: filepath, | ||
retry_count: 1, | ||
) | ||
|
||
puts "\n--- Running failing case with `default_retry_wait: 0`---\n" | ||
Examples::DownloadFile.run( | ||
source: NON_EXESTENT_FILE_URI, | ||
to: filepath, | ||
default_retry_wait: 0, | ||
) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'uber_task' | ||
require 'colorize' | ||
require 'tmpdir' | ||
require 'fileutils' | ||
|
||
module Examples | ||
class MoveFile | ||
def self.run(from:, to:, **task_options) | ||
UberTask.run( | ||
'Download file from external source', | ||
default_retry_wait: 1, | ||
retry_count: 3, | ||
vital: false, | ||
**task_options, | ||
) do | ||
UberTask.on_success do | ||
UberTask.logger.info "Moved file from #{from} to #{to}".green | ||
end | ||
|
||
UberTask.on_retry do | ||
UberTask.logger.info( | ||
"Retrying to move file from #{from} to #{to}...".yellow, | ||
) | ||
end | ||
|
||
move_file!(from: from, to: to) | ||
end | ||
end | ||
|
||
def self.move_file!(from:, to:) | ||
FileUtils.mv(from, to) | ||
rescue Errno::ENOENT => err | ||
raise UberTask::RetryTask.new(reason: err.message) | ||
end | ||
private_class_method :move_file! | ||
end | ||
end | ||
|
||
if __FILE__ == $PROGRAM_NAME | ||
Dir.mktmpdir do |dir| | ||
File.open(File.join(dir, 'old_path'), 'w') do |file| | ||
file.write('test') | ||
|
||
puts '--- Running successfull case ---' | ||
Examples::MoveFile.run(from: file.path, to: File.join(dir, 'new_path')) | ||
end | ||
|
||
### Notice that task has `vital: true` so error is raised | ||
puts "\n--- Running failing case with `vital: true`---\n" | ||
begin | ||
Examples::MoveFile.run( | ||
from: 'invalid_path', | ||
to: File.join(dir, 'new_path'), | ||
vital: true, | ||
) | ||
rescue StandardError | ||
UberTask.logger.info 'Error was raised'.red | ||
end | ||
|
||
### Notice that task has `vital: false` so error isn't raised | ||
puts "\n--- Running failing case with `vital: false`---\n" | ||
Examples::MoveFile.run( | ||
from: 'invalid_path', | ||
to: File.join(dir, 'new_path'), | ||
vital: false, | ||
) | ||
|
||
puts "\n--- Running failing case with `retry_count: 1`---\n" | ||
Examples::MoveFile.run( | ||
from: 'invalid_path', | ||
to: File.join(dir, 'new_path'), | ||
retry_count: 1, | ||
) | ||
|
||
puts "\n--- Running failing case with `default_retry_wait: 3`---\n" | ||
Examples::MoveFile.run( | ||
from: 'invalid_path', | ||
to: File.join(dir, 'new_path'), | ||
default_retry_wait: 3, | ||
) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the return, returns inside this block will affect the method that handles the exection.
We have to raise RetryTask, SkipTask or EventHandled, and if we don't raise either of these options, the exception will continue to bubble up.
It looks like I forgot to add a UberTask.event_handled method, add that method.All events have a .handled method already.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the return and added handling different types of exceptions which can be raised by subtasks