-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #954 from funmusicplace/various-fixes
feat: queue and job for moving files from minio to backblaze
- Loading branch information
Showing
5 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Job, Queue, QueueEvents } from "bullmq"; | ||
import { REDIS_CONFIG } from "../config/redis"; | ||
import { promises as fsPromises } from "fs"; | ||
import logger from "../logger"; | ||
import { | ||
getBufferFromMinio, | ||
getObjectList, | ||
minioClient, | ||
removeObjectsFromBucket, | ||
trackGroupFormatBucket, | ||
uploadWrapper, | ||
} from "../utils/minio"; | ||
import { sleep } from "../jobs/optimize-image"; | ||
|
||
const queueOptions = { | ||
prefix: "mirlo", | ||
connection: REDIS_CONFIG, | ||
}; | ||
|
||
export const moveFilesToBackBlazeQueue = new Queue( | ||
"move-file-to-backblaze", | ||
queueOptions | ||
); | ||
|
||
export const moveFilesToBackBlazeQueueEvents = new QueueEvents( | ||
"move-file-to-backblaze", | ||
queueOptions | ||
); | ||
|
||
moveFilesToBackBlazeQueueEvents.on( | ||
"completed", | ||
async (result: { jobId: string; returnvalue?: any }) => { | ||
logger.info( | ||
`Job with id ${JSON.stringify( | ||
result.jobId | ||
)} has been completed, ${JSON.stringify(result.returnvalue)}` | ||
); | ||
} | ||
); | ||
|
||
moveFilesToBackBlazeQueueEvents.on( | ||
"stalled", | ||
async (result: { jobId: string }) => { | ||
logger.info(`jobId ${result.jobId} stalled: that's a bummer`); | ||
} | ||
); | ||
|
||
moveFilesToBackBlazeQueueEvents.on("error", async (error) => { | ||
logger.error(`jobId ${JSON.stringify(error)} had an error`); | ||
}); | ||
|
||
export const startMovingFiles = async (bucketName: string) => { | ||
const files = await getObjectList(bucketName, ""); | ||
files.map(async (file, i) => { | ||
try { | ||
console.log("adding to queue", file.name); | ||
moveFilesToBackBlazeQueue.add( | ||
"move-file-to-backblaze", | ||
{ | ||
bucketName, | ||
fileName: file.name, | ||
}, | ||
{ | ||
delay: i * 1500, | ||
} | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
logger.error("Error adding to queue"); | ||
} | ||
}); | ||
}; | ||
|
||
export const moveFilesToBackblazeJob = async (job: Job) => { | ||
const bucketName = job.data.bucketName; | ||
const fileName = job.data.fileName; | ||
|
||
logger.info(`moving file: bucket: ${bucketName}, ${fileName}`); | ||
try { | ||
const stream = await minioClient.getObject(bucketName, fileName); | ||
await uploadWrapper(bucketName, fileName, stream); | ||
} catch (e) { | ||
console.error(e); | ||
logger.error("Error moving file"); | ||
} | ||
logger.info(`done transfering ${bucketName}/${fileName}`); | ||
}; |
This file contains 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