Files
redux-scraper/app/jobs/domain/e621/job/static_file_job.rb
Dylan Knutson 48337c08bc Enhance strict typing and refactor job classes in FA and Inkbunny domains
- Updated job classes in the FA and Inkbunny domains to enforce strict typing with Sorbet, including the addition of type signatures in various job classes.
- Refactored initialization methods to improve argument handling and ensure proper type management.
- Enhanced error handling and logging mechanisms across job classes for better traceability.
- Improved job argument handling, including the introduction of `first_log_entry` and `causing_log_entry` for better context in job processing.
- Streamlined the processing of API responses and improved the management of user and post data.

These changes aim to enhance type safety, maintainability, and robustness of the job processing logic.
2025-01-02 03:55:02 +00:00

54 lines
1.5 KiB
Ruby

# typed: strict
class Domain::E621::Job::StaticFileJob < Domain::E621::Job::Base
queue_as :static_file
sig { override.params(args: T::Hash[Symbol, T.untyped]).void }
def perform(args)
post =
T.let(args[:post] || fatal_error("post is required"), Domain::E621::Post)
logger.prefix = proc { "[e621_id #{post.e621_id.to_s.bold}]" }
file_url_str = post.file_url_str
if file_url_str.blank?
logger.warn("post has no file_url_str, enqueueing for scan")
defer_job(Domain::E621::Job::ScanPostJob, { post: post })
return
end
if post.state == "file_error"
retry_count = post.state_detail&.[]("file_error")&.[]("retry_count") || 0
if retry_count >= 3
logger.error("file has been retried 3 times, giving up")
return
end
end
response = http_client.get(file_url_str, caused_by_entry: causing_log_entry)
self.first_log_entry ||= response.log_entry
if response.status_code != 200
post.state = :file_error
fe = (post.state_detail["file_error"] ||= {})
fe["status_code"] = response.status_code
fe["log_entry_id"] = response.log_entry.id
fe["retry_count"] ||= 0
fe["retry_count"] += 1
post.save!
if response.status_code == 404
logger.error("#{response.status_code}, not retrying download")
else
fatal_error("#{response.status_code}, will retry later")
end
return
end
post.state = :ok
post.file = response.log_entry
post.save!
logger.info "downloaded file"
end
end