11/* eslint-disable no-console */
2+ import { H } from "@highlight-run/cloudflare" ;
23import { createClient } from "@sanity/client" ;
34import { ensureKeys } from "../utils" ;
45import { GoogleImportQueueElement } from "../../src/datasources/queues/google_import" ;
56import { v5 } from "uuid" ;
7+ import { APP_ENV } from "../../src/env" ;
68
79type ENV = {
810 SANITY_PROJECT_ID : string ;
11+ HIGHLIGHT_PROJECT_ID : string ;
912 SANITY_DATASET : string ;
1013 SANITY_API_VERSION : string ;
1114 SANITY_SECRET_TOKEN : string ;
@@ -23,55 +26,75 @@ const getSanityClient = (env: ENV) =>
2326export const queueConsumer : ExportedHandlerQueueHandler <
2427 ENV ,
2528 GoogleImportQueueElement
26- > = async ( batch , env ) => {
27- ensureKeys ( env , [
28- "SANITY_PROJECT_ID" ,
29- "SANITY_DATASET" ,
30- "SANITY_API_VERSION" ,
31- "SANITY_SECRET_TOKEN" ,
32- ] ) ;
33- const sanityClient = getSanityClient ( env ) ;
34- console . log ( "Processing queue" , batch . queue ) ;
35- for await ( const msg of batch . messages ) {
36- console . log ( "Processing message" , msg ) ;
37- const { googleMedia, sanityEventInstanceId } = msg . body ;
38- const eventInstance = await sanityClient . getDocument ( sanityEventInstanceId ) ;
39- if ( ! eventInstance ) {
40- throw new Error ( `Event instance ${ sanityEventInstanceId } not found` ) ;
41- }
42- const response = await fetch ( googleMedia . baseUrl + "=w4096" ) ;
43- const blob = await response . blob ( ) ;
44-
45- const createdAsset = await sanityClient . assets . upload ( "image" , blob , {
46- contentType : googleMedia . mimeType ,
47- title : googleMedia . id ,
48- preserveFilename : true ,
49- creditLine : "JavaScript Chile" ,
50- extract : [ "blurhash" , "exif" , "image" , "location" , "lqip" , "palette" ] ,
29+ > = async ( batch , env , ctx ) => {
30+ try {
31+ const r = new Request ( "cloudflare:workers:google_import_queue_consumer" ) ;
32+ H . init ( r , { HIGHLIGHT_PROJECT_ID : env . HIGHLIGHT_PROJECT_ID ?? "" } , ctx ) ;
33+ H . setAttributes ( {
34+ APP_ENV : APP_ENV ?? "none" ,
5135 } ) ;
52- console . log ( "Created asset" , createdAsset , createdAsset . metadata ) ;
36+ ensureKeys ( env , [
37+ "SANITY_PROJECT_ID" ,
38+ "SANITY_DATASET" ,
39+ "SANITY_API_VERSION" ,
40+ "SANITY_SECRET_TOKEN" ,
41+ "HIGHLIGHT_PROJECT_ID" ,
42+ ] ) ;
43+ const sanityClient = getSanityClient ( env ) ;
44+ console . log ( "Processing queue" , batch . queue ) ;
45+ for await ( const msg of batch . messages ) {
46+ try {
47+ console . log ( "Processing message" , msg ) ;
48+ const { googleMedia, sanityEventInstanceId } = msg . body ;
49+ const eventInstance = await sanityClient . getDocument (
50+ sanityEventInstanceId ,
51+ ) ;
52+ if ( ! eventInstance ) {
53+ throw new Error ( `Event instance ${ sanityEventInstanceId } not found` ) ;
54+ }
55+ const response = await fetch ( googleMedia . baseUrl + "=w4096" ) ;
56+ const blob = await response . blob ( ) ;
5357
54- const createdImage = await sanityClient . createOrReplace ( {
55- _id : v5 ( googleMedia . id , v5 . URL ) ,
56- _type : "eventImage" ,
57- externalId : googleMedia . id ,
58- externalURL : googleMedia . baseUrl ,
59- eventInstance : {
60- _type : "reference" ,
61- _ref : sanityEventInstanceId ,
62- } ,
63- title : googleMedia . filename ,
64- image : {
65- _type : "image" ,
66- asset : {
67- _type : "reference" ,
68- _ref : createdAsset . _id ,
69- } ,
70- } ,
71- } ) ;
58+ const createdAsset = await sanityClient . assets . upload ( "image" , blob , {
59+ contentType : googleMedia . mimeType ,
60+ title : googleMedia . id ,
61+ preserveFilename : true ,
62+ creditLine : "JavaScript Chile" ,
63+ extract : [ "blurhash" , "exif" , "image" , "location" , "lqip" , "palette" ] ,
64+ } ) ;
65+ console . log ( "Created asset" , createdAsset , createdAsset . metadata ) ;
7266
73- console . log ( "Created image" , createdImage ) ;
67+ const createdImage = await sanityClient . createOrReplace ( {
68+ _id : v5 ( googleMedia . id , v5 . URL ) ,
69+ _type : "eventImage" ,
70+ externalId : googleMedia . id ,
71+ externalURL : googleMedia . baseUrl ,
72+ eventInstance : {
73+ _type : "reference" ,
74+ _ref : sanityEventInstanceId ,
75+ } ,
76+ title : googleMedia . filename ,
77+ image : {
78+ _type : "image" ,
79+ asset : {
80+ _type : "reference" ,
81+ _ref : createdAsset . _id ,
82+ } ,
83+ } ,
84+ } ) ;
7485
75- msg . ack ( ) ;
86+ console . log ( "Created image" , createdImage ) ;
87+
88+ msg . ack ( ) ;
89+ } catch ( e ) {
90+ console . error ( e ) ;
91+ H . consumeError ( e as Error ) ;
92+ throw e ;
93+ }
94+ }
95+ } catch ( e ) {
96+ console . error ( e ) ;
97+ H . consumeError ( e as Error ) ;
98+ throw e ;
7699 }
77100} ;
0 commit comments