Skip to content

Commit 2deb8ec

Browse files
committed
add ability to optionally sample a percentage of images to a lower environment's queue bucket, after they've successfully uploaded via S3 copy operation (quick/cheap) - triggered by presence of new s3.sampling.lowerEnvironmentQueueBucket config option. Sampling is 1% by default, but can be configured via s3.sampling.percentage
1 parent be7f6ba commit 2deb8ec

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

image-loader/app/lib/ImageLoaderConfig.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class ImageLoaderConfig(resources: GridConfigResources) extends CommonConfig(res
1818
val quarantineBucket: Option[String] = stringOpt("s3.quarantine.bucket")
1919
val uploadToQuarantineEnabled: Boolean = boolean("upload.quarantine.enabled")
2020

21+
val lowerEnvironmentSamplingPercentageAsDecimal = intOpt("s3.sampling.percentage").getOrElse(1) / 100.0
22+
val maybeLowerEnvironmentQueueBucketToSampleInto = stringOpt("s3.sampling.lowerEnvironmentQueueBucket")
23+
2124
val tempDir: File = new File(stringDefault("upload.tmp.dir", "/tmp"))
2225

2326
val thumbWidth: Int = 256

image-loader/app/model/Uploader.scala

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package model
22

33
import com.gu.mediaservice.{GridClient, ImageDataMerger}
44
import com.gu.mediaservice.lib.Files.createTempFile
5+
import com.gu.mediaservice.lib.ImageIngestOperations.fileKeyFromId
56

67
import java.io.File
78
import java.net.URLEncoder
@@ -395,7 +396,25 @@ class Uploader(val store: ImageLoaderStore,
395396
updateMessage = UpdateMessage(subject = Image, image = Some(imageUpload.image))
396397
_ <- Future { notifications.publish(updateMessage) }
397398
// TODO: centralise where all these URLs are constructed
398-
} yield UploadStatusUri(s"${config.rootUri}/uploadStatus/${uploadRequest.imageId}")
399+
} yield {
400+
config.maybeLowerEnvironmentQueueBucketToSampleInto.foreach { lowerEnvironmentQueueBucket =>
401+
if (math.random() < config.lowerEnvironmentSamplingPercentageAsDecimal) {
402+
val mediaId = imageUpload.image.id
403+
logger.info(logMarker, s"Copying $mediaId to lower environment queue bucket $lowerEnvironmentQueueBucket")
404+
try {
405+
store.client.copyObject(
406+
config.imageBucket, fileKeyFromId(mediaId),
407+
lowerEnvironmentQueueBucket, s"${uploadRequest.uploadedBy}/${uploadRequest.uploadInfo.filename.getOrElse(mediaId)}"
408+
)
409+
} catch {
410+
case e: Throwable =>
411+
logger.error(logMarker, s"Failed to copy $mediaId to lower environment queue bucket $lowerEnvironmentQueueBucket", e)
412+
}
413+
}
414+
}
415+
416+
UploadStatusUri(s"${config.rootUri}/uploadStatus/${uploadRequest.imageId}")
417+
}
399418

400419
}
401420

0 commit comments

Comments
 (0)