I’ve encountered an issue while trying to broadcast the creation of a new image in our system. The Image model is mind, and it has_one_attached :file. I broadcast its creation via:
after_create_commit do
broadcast_append_later_to article.online_newsletter, target: ActionView::RecordIdentifier.dom_id(article, :images),
partial: 'online_newsletters/articles/image', locals: { editable: true }
end
Is there a way to wait until we know that the image is in S3 before broadcasting the update? I notice that if I update the page with a turbo-stream from the controller to the user that added the image then it works just fine. I’m not sure if this is just a timing coincidence or if the request waits for the image to be in S3 before rendering the view?
Also, given dom_id is so useful in both the controller and model context now, how come it’s not required into those contexts by default?
Interesting @nickhammond So I suppose I could wrap that check in a while loop to delay the delivery of the broadcast until the file exists? Probably would need to throttle that though!
Bit of a shame that there’s no hook bubbled up to the model when the file has been stored.
@Spike Yah, I was thinking there’d be a stored_at or uploaded_at timestamp in the attachments table but there’s not weirdly, be a great PR though. The other use case though for this method is if that file is removed from the storage service for whatever reason you can check that.
Digging into this deeper, it seems that sometimes Amazon S3 will return a 404 if we ask it for a variant too quickly after the file upload has been processed even with something like this:
after_create_commit prepend: true do
if file.blob.service.exist?(file.key)
broadcast_append_to article.online_newsletter, target: dom_id(article, :images),
partial: 'online_newsletters/articles/image', locals: { editable: true }
end
end