Last active
January 22, 2026 16:17
-
-
Save danielmfern/12bb8a005e5249e31c82bb896f8a6c8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # process_variants.rb | |
| # Lambda tiene concurrencia de 20 + SQS queue, no necesita rate limiting manual | |
| Current.account = Account.find 9 | |
| Current.user = User.first | |
| shot_ids = Flow::Shot | |
| .joins(task: :card) | |
| .where(principal_flag: 'e1', flag: 'e1', step: 'final', editable_id: nil) | |
| .merge(Flow::Card.where(campaign: 'V2026')) | |
| .distinct | |
| .pluck(:id) | |
| editable_ids = Flow::Shot.joins(task: :card) | |
| .where.not(editable_id: nil) | |
| .where(flag: 'e1', step: 'final') | |
| .merge(Flow::Card.where(campaign: 'V2026')) | |
| .distinct | |
| .pluck(:editable_id) | |
| final_ids = shot_ids - editable_ids | |
| shot_with_psd_ids = Flow::ShotImage.where(flow_shot_id: final_ids, extension: 'psd', step: 'final').pluck(:flow_shot_id) | |
| shots = Flow::Shot.where(id: shot_with_psd_ids) | |
| Rails.logger.info("Total shots to process: #{shots.count}") | |
| processed_count = 0 | |
| error_count = 0 | |
| skipped_count = 0 | |
| shots.find_each(batch_size: 100) do |shot| | |
| begin | |
| filename = shot.download_filename('retouch').gsub(shot.id.to_s, shot.parent_variant.id.to_s) | |
| if filename.start_with?('2') | |
| skipped_count += 1 | |
| next | |
| end | |
| actions = Flow::AutomaticActions::DetectionService.new(filename).execute | |
| action_code = 'background_remove_jpg' | |
| output_filename = actions.find { |a| a['action'] == action_code }['output_filename'] | |
| action_execution = Flow::ActionExecution.create!( | |
| filename:, | |
| output_filename:, | |
| blob_id: shot.file.signed_id, | |
| action_code:, | |
| user_action_id: '-999999', # Generar un nuevo user_action_id por "batch" | |
| request_params: { step: 'final' } | |
| ) | |
| processed_count += 1 | |
| Rails.logger.info("SUCCESS: #{filename} (#{processed_count} processed)") if processed_count % 10 == 0 | |
| rescue StandardError => e | |
| error_count += 1 | |
| Rails.logger.error("ERROR shot #{shot.id}: #{e.message}") | |
| end | |
| end | |
| Rails.logger.info("Script completed - Processed: #{processed_count}, Errors: #{error_count}, Skipped: #{skipped_count}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment