Files
redux-scraper/app/jobs/domain/e621/job/posts_index_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

57 lines
1.5 KiB
Ruby

# typed: strict
class Domain::E621::Job::PostsIndexJob < Domain::E621::Job::Base
queue_as :e621
sig { override.params(args: T::Hash[Symbol, T.untyped]).void }
def perform(args)
response =
http_client.get(
"https://e621.net/posts.json",
caused_by_entry: causing_log_entry,
)
log_entry = response.log_entry
self.first_log_entry ||= log_entry
if response.status_code != 200
fatal_error(
"non 200 response for /posts.json: #{response.status_code.to_s.underline}",
)
end
json = JSON.parse(response.body)
if json["posts"].nil?
fatal_error("no posts in response, hle #{log_entry.id}")
end
e621_posts =
json["posts"].map do |post_json|
Domain::E621::TagUtil.initialize_or_update_post(
post_json: post_json,
caused_by_entry: causing_log_entry,
)
end
created_posts = []
updated_posts = []
seen_posts = []
e621_posts.each do |e621_post|
created_posts << e621_post if e621_post.new_record?
updated_posts << e621_post if e621_post.changed?
seen_posts << e621_post
e621_post.save!
end
(created_posts + updated_posts).uniq.each do |post|
logger.info(
"[e621_id: #{post.e621_id.to_s.bold}] enqueueing static file job",
)
defer_job(Domain::E621::Job::StaticFileJob, { post: post })
end
logger.info(
"#{updated_posts.count} updated, #{created_posts.count} created, #{seen_posts.count} seen",
)
end
end