Skip to content

Commit a981947

Browse files
committed
update comments
1 parent 16f8e23 commit a981947

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

2nd-gen/delete-unused-accounts-cron/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ To set up the sample:
1717

1818
- Create a Firebase Project using the [Firebase Developer Console](https://console.firebase.google.com)
1919
- Download this sample e.g. `git clone https://github.com/firebase/functions-samples`
20-
- Enter the sample directory `cd functions-samples/delete-unused-accounts-cron`
20+
- Enter the sample directory `cd functions-samples/2nd-gen/delete-unused-accounts-cron`
2121
- Setup the sample with your project `firebase use --add` and follow the instructions.
2222
- Install node dependencies of your Functions `cd functions; npm install; cd -`
2323
- Deploy your project using `firebase deploy`.
24-
- The pubsub task should then run once a day and delete any inactive users. You can manually run the task by [navigating to Cloud Scheduler in the Google Cloud Platform Console](https://console.cloud.google.com/cloudscheduler).
24+
- The Cloud Scheduler job should then run once a day and delete any inactive users. You can manually run the task by [navigating to Cloud Scheduler in the Google Cloud Platform Console](https://console.cloud.google.com/cloudscheduler).

2nd-gen/delete-unused-accounts-cron/functions/index.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,50 @@
1515
*/
1616
'use strict';
1717

18-
const functions = require('firebase-functions/v2');
18+
// [START all]
19+
// [START import]
20+
// The Cloud Functions for Firebase SDK to create v2 Cloud Functions and setup triggers.
21+
const { onSchedule } = require('firebase-functions/v2/scheduler');
22+
const { logger } = require('firebase-functions');
23+
24+
// The Firebase Admin SDK to delete inactive users.
1925
const admin = require('firebase-admin');
2026
admin.initializeApp();
27+
28+
// The es6-promise-pool to limit the concurrency of Promise's.
2129
const PromisePool = require('es6-promise-pool').default;
2230
// Maximum concurrent account deletions.
2331
const MAX_CONCURRENT = 3;
32+
// [END import]
2433

25-
/**
26-
* Run once a day at midnight, to cleanup the users
27-
* Manually run the task here https://console.cloud.google.com/cloudscheduler
28-
*/
29-
exports.accountcleanup = functions.scheduler.onSchedule('every day 00:00', async (event) => {
34+
// [START accountcleanup]
35+
// Run once a day at midnight, to cleanup the users
36+
// Manually run the task here https://console.cloud.google.com/cloudscheduler
37+
exports.accountcleanup = onSchedule('every day 00:00', async (event) => {
3038
// Fetch all user details.
3139
const inactiveUsers = await getInactiveUsers();
3240
// Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.
3341
const promisePool = new PromisePool(() => deleteInactiveUser(inactiveUsers), MAX_CONCURRENT);
3442
await promisePool.start();
35-
functions.logger.log('User cleanup finished');
43+
logger.log('User cleanup finished');
3644
});
45+
// [END accountcleanup]
3746

38-
/**
39-
* Deletes one inactive user from the list.
40-
*/
47+
// [START deleteInactiveUser]
48+
// Deletes one inactive user from the list.
4149
function deleteInactiveUser(inactiveUsers) {
4250
if (inactiveUsers.length > 0) {
4351
const userToDelete = inactiveUsers.pop();
4452

4553
// Delete the inactive user.
4654
return admin.auth().deleteUser(userToDelete.uid).then(() => {
47-
return functions.logger.log(
55+
return logger.log(
4856
'Deleted user account',
4957
userToDelete.uid,
5058
'because of inactivity'
5159
);
5260
}).catch((error) => {
53-
return functions.logger.error(
61+
return logger.error(
5462
'Deletion of inactive user account',
5563
userToDelete.uid,
5664
'failed:',
@@ -61,10 +69,10 @@ function deleteInactiveUser(inactiveUsers) {
6169
return null;
6270
}
6371
}
72+
// [END deleteInactiveUser]
6473

65-
/**
66-
* Returns the list of all inactive users.
67-
*/
74+
// [START getInactiveUsers]
75+
// Returns the list of all inactive users.
6876
async function getInactiveUsers(users = [], nextPageToken) {
6977
const result = await admin.auth().listUsers(1000, nextPageToken);
7078
// Find users that have not signed in in the last 30 days.
@@ -81,3 +89,5 @@ async function getInactiveUsers(users = [], nextPageToken) {
8189

8290
return users;
8391
}
92+
// [END getInactiveUsers]
93+
// [END all]

2nd-gen/delete-unused-accounts-cron/functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"dependencies": {
55
"es6-promise-pool": "^2.5.0",
66
"firebase-admin": "^10.2.0",
7-
"firebase-functions": "^4.0.0"
7+
"firebase-functions": "^3.23.0"
88
},
99
"devDependencies": {
1010
"eslint": "^6.8.0",

0 commit comments

Comments
 (0)