Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleanup Temp Directory Firebase cloud Functions

I am using Cloud Functions for Firebase for my webapp. I need to create thumbnail for any image uploaded on Firebase Storage. For that I need to download the uploaded file from GCS bucket to temp directory(using mkdirp-promise), and apply imageMagick command to create a thumbnail. (Firebase Function Samples- Generate Thumbnail)

return mkdirp(tempLocalDir).then(() => {
    console.log('Temporary directory has been created', tempLocalDir);
    // Download file from bucket.
    return bucket.file(filePath).download({
      destination: tempLocalFile
    });
  }).then(() => {
    //rest of the program
  });
});

My Question is:

  1. where is this temp directory created?
  2. Is this temp storage counted against my firebase cloud storage or Google cloud Storage quota?
  3. How can I cleanup my temp directory, after i have successfully uploaded newly created thumbnail file? So that my quota doesnt exceed.
like image 574
Ravi Shankar Bharti Avatar asked Jun 20 '17 12:06

Ravi Shankar Bharti


2 Answers

  1. The temp directory is created in tmpfs, which in the Cloud Functions environment is kept in memory. See https://cloud.google.com/functions/pricing#local_disk
  2. Since tmpfs is kept in memory, it counts against the memory usage of your Functions.
  3. You remove a directory by calling fs.rmdir(): https://nodejs.org/api/fs.html#fs_fs_rmdir_path_callback
like image 146
Frank van Puffelen Avatar answered Nov 09 '22 06:11

Frank van Puffelen


Here's some of the code I wrote for the "Fire!sale" continuous deployment demo at Google I/O (warning: it's in TypeScript, not JavaScript. This lets me use await/async which is easier to read, especially in the case of error handling)

import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
let tempy = require('tempy'); // No .d.ts

function rmFileAsync(file: string) {
  return new Promise((resolve, reject) => {
    fs.unlink(file, (err) => {
      if (err) {
        reject(err);
      } else {
        resolve();
      }
    })
  })
}

function statAsync(file: string): Promise<fs.Stats> {
  return new Promise((resolve, reject) => {
    fs.stat(file, (err, stat) => {
      if (err) {
        reject(err);
      } else {
        resolve(stat);
      }
    })
  })
}

async function rmrfAsync(dir: string) {
  // Note: I should have written this to be async too
  let files = fs.readdirSync(dir);

  return Promise.all(_.map(files, async (file) => {
    file = path.join(dir, file);
    let stat = await statAsync(file);
    if (stat.isFile()) {
      return rmFileAsync(file);
    }
    return rmrfAsync(file);
  }));
}

Then inside my Cloud Functions code I could do something like the following:

export let myFunction = functions.myTrigger.onEvent(async event => {
  // If I want to be extra aggressive to handle any timeouts/failures and
  // clean up before execution:
  try {
    await rmrfAsync(os.tmpdir());
  } catch (err) {
    console.log('Failed to clean temp directory. Deploy may fail.', err);
  }
  
  // In an async function we can use try/finally to ensure code runs
  // without changing the error status of the function.
  try {
    // Gets a new directory under /tmp so we're guaranteed to have a
    // clean slate.
    let dir = tempy.directory(); 
    // ... do stuff ...
  } finally {
    await rmrfAsync(dir);
  }
}
like image 42
Thomas Bouldin Avatar answered Nov 09 '22 08:11

Thomas Bouldin