@@ -15,7 +15,6 @@ process.on('uncaughtException', (err) => {
1515 console . error ( '💥 Uncaught exception:' , err ) ;
1616} ) ;
1717
18- const sqs = new AWS . SQS ( { region : process . env . AWS_REGION } ) ;
1918const s3 = new AWS . S3 ( { region : process . env . AWS_REGION } ) ;
2019
2120function randomText ( len ) {
@@ -27,32 +26,6 @@ function randomTags() {
2726 return Array . from ( { length : 3 } , ( ) => tags [ Math . floor ( Math . random ( ) * tags . length ) ] ) . join ( ',' ) ;
2827}
2928
30- async function getMessages ( ) {
31- console . log ( "📥 SQS message requested" ) ;
32- const resp = await sqs . receiveMessage ( {
33- QueueUrl : process . env . SQS_URL ,
34- MaxNumberOfMessages : 1 ,
35- WaitTimeSeconds : 20
36- } ) . promise ( ) ;
37-
38- if ( resp . Messages ?. length ) {
39- console . log ( "📭 SQS message received" ) ;
40- } else {
41- console . log ( "📭 No messages received" ) ;
42- }
43-
44- return resp . Messages || [ ] ;
45- }
46-
47- async function deleteMessage ( receiptHandle ) {
48- console . log ( "🧹 Deleting SQS message" ) ;
49- await sqs . deleteMessage ( {
50- QueueUrl : process . env . SQS_URL ,
51- ReceiptHandle : receiptHandle
52- } ) . promise ( ) ;
53- console . log ( "🧹 Message deleted" ) ;
54- }
55-
5629async function resizeAndUpload ( s3bucket , key , outBucket , outKey ) {
5730 console . log ( "📤 S3 image download started:" , { bucket : s3bucket , key } ) ;
5831 const imgResp = await s3 . getObject ( { Bucket : s3bucket , Key : key } ) . promise ( ) ;
@@ -96,8 +69,8 @@ async function saveToDB(dbConfig, data) {
9669
9770async function main ( ) {
9871 console . log ( "🔧 ENVIRONMENT LOADED" ) ;
99- console . log ( "SQS_URL:" , process . env . SQS_URL ) ;
10072 console . log ( "S3_BUCKET:" , process . env . S3_BUCKET ) ;
73+ console . log ( "INPUT_KEY:" , process . env . INPUT_KEY ) ;
10174 console . log ( "REGION:" , process . env . AWS_REGION ) ;
10275
10376 try {
@@ -111,46 +84,29 @@ async function main() {
11184 console . log ( "🔧 DB config:" , dbConfig ) ;
11285
11386 const inputBucket = process . env . S3_BUCKET ;
87+ const key = process . env . INPUT_KEY ;
11488 const outputBucket = process . env . OUTPUT_BUCKET || inputBucket ;
89+ const outKey = `output/${ key . split ( '/' ) . pop ( ) } ` ;
90+
91+ console . log ( "📦 Payload parsed:" , { key, outKey } ) ;
92+
93+ await resizeAndUpload ( inputBucket , key , outputBucket , outKey ) ;
94+
95+ const post = {
96+ url : `https://${ outputBucket } .s3.${ process . env . AWS_REGION } .amazonaws.com/${ outKey } ` ,
97+ title : "Post " + randomText ( 4 ) ,
98+ text : "Generated text: " + randomText ( 12 ) ,
99+ tags : randomTags ( )
100+ } ;
101+
102+ await saveToDB ( dbConfig , post ) ;
115103
116- const messages = await getMessages ( ) ;
117- if ( ! messages . length ) {
118- console . log ( "📭 No messages in queue." ) ;
119- return ;
120- }
121-
122- for ( const msg of messages ) {
123- try {
124- console . log ( "🧾 Raw message body:" , msg . Body ) ;
125- const payload = JSON . parse ( msg . Body ) ;
126- const key = payload . detail ?. object ?. key ;
127- const base = key ?. split ( '/' ) . pop ( ) ;
128- const outKey = `output/${ base } ` ;
129-
130- console . log ( "📦 Payload parsed:" , { key, outKey } ) ;
131-
132- await resizeAndUpload ( inputBucket , key , outputBucket , outKey ) ;
133-
134- const post = {
135- url : `https://${ outputBucket } .s3.${ process . env . AWS_REGION } .amazonaws.com/${ outKey } ` ,
136- title : "Post " + randomText ( 4 ) ,
137- text : "Generated text: " + randomText ( 12 ) ,
138- tags : randomTags ( )
139- } ;
140-
141- await saveToDB ( dbConfig , post ) ;
142- await deleteMessage ( msg . ReceiptHandle ) ;
143-
144- console . log ( "✅ Processed:" , post ) ;
145- } catch ( err ) {
146- console . error ( "❌ Error processing message:" , err ) ;
147- }
148- }
104+ console . log ( "✅ Processed:" , post ) ;
149105 } catch ( e ) {
150106 console . error ( "💥 Fatal error:" , e ) ;
151107 } finally {
152108 console . log ( "⏹ Task finished. Exiting..." ) ;
153- await new Promise ( ( res ) => setTimeout ( res , 3000 ) ) ; // дати CloudWatch часу дописати лог
109+ await new Promise ( ( res ) => setTimeout ( res , 3000 ) ) ;
154110 process . exit ( 0 ) ;
155111 }
156112}
0 commit comments