Skip to content

Commit 2770e27

Browse files
authored
Switching delete unused accounts to scheduled functions (firebase#567)
1 parent 02fecab commit 2770e27

File tree

3 files changed

+6
-51
lines changed

3 files changed

+6
-51
lines changed

delete-unused-accounts-cron/README.md

+2-31
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,12 @@ This sample demonstrates how to delete the accounts of users who have not signed
77

88
See the file [functions/index.js](functions/index.js) for the code.
99

10-
Cloud Functions does not natively supports cron jobs. We are working around this by executing the code as an HTTPS-triggered function. Then simply use an external service to periodically "ping" the URL.
11-
12-
Here is a non-exhaustive list of external services for cron jobs:
13-
- https://cloud.google.com/scheduler/
14-
- https://cron-job.org/
15-
- https://www.setcronjob.com/
16-
- https://www.easycron.com/
17-
- https://zapier.com/zapbook/webhook/
10+
**Note:** This function uses Cloud Scheduler and Pub/Sub, which can have associated costs. Your project must be on the Blaze payment plan as these features require billing information. See the [Cloud Scheduler pricing page](https://cloud.google.com/scheduler/pricing) for more information.
1811

1912
The dependencies are listed in [functions/package.json](functions/package.json).
2013

21-
22-
## Trigger rules
23-
24-
The function triggers when the HTTP URL of the Function is requested.
25-
26-
2714
## Deploy and test
2815

29-
Set the `cron.key` Google Cloud environment variables to a randomly generated key. This will be used to authorize requests coming from the 3rd-party cron service. For this use:
30-
31-
```bash
32-
firebase functions:config:set cron.key="YOUR-KEY"
33-
```
34-
35-
You can generate a random key, for instance, by running:
36-
37-
```bash
38-
node -e "console.log(require('crypto').randomBytes(20).toString('hex'))"
39-
```
40-
4116
To set up the sample:
4217

4318
- Create a Firebase Project using the [Firebase Developer Console](https://console.firebase.google.com)
@@ -46,8 +21,4 @@ To set up the sample:
4621
- Setup the sample with your project `firebase use --add` and follow the instructions.
4722
- Install node dependencies of your Functions `cd functions; npm install; cd -`
4823
- Deploy your project using `firebase deploy`.
49-
- Open an account with a 3rd party cron service (e.g. www.setcronjob.com, cron-job.org, www.easycron.com, [Zapier](https://zapier.com/zapbook/webhook/) ...) and setup a daily cron job to hit the URL (don't forget to change `<YOUR-KEY>` and `<PROJECT-ID>`):
50-
51-
```
52-
https://us-central1-<PROJECT-ID>.cloudfunctions.net/accountcleanup?key=<YOUR-KEY>
53-
```
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).

delete-unused-accounts-cron/functions/index.js

+3-18
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,20 @@ const admin = require('firebase-admin');
2020
admin.initializeApp();
2121
const promisePool = require('es6-promise-pool');
2222
const PromisePool = promisePool.PromisePool;
23-
const secureCompare = require('secure-compare');
2423
// Maximum concurrent account deletions.
2524
const MAX_CONCURRENT = 3;
2625

2726
/**
28-
* When requested this Function will delete every user accounts that has been inactive for 30 days.
29-
* The request needs to be authorized by passing a 'key' query parameter in the URL. This key must
30-
* match a key set as an environment variable using `firebase functions:config:set cron.key="YOUR_KEY"`.
27+
* Run once a day at midnight, to cleanup the users
28+
* Manually run the task here https://console.cloud.google.com/cloudscheduler
3129
*/
32-
exports.accountcleanup = functions.https.onRequest(async (req, res) => {
33-
const key = req.query.key;
34-
35-
// Exit if the keys don't match.
36-
if (!secureCompare(key, functions.config().cron.key)) {
37-
console.log('The key provided in the request does not match the key set in the environment. Check that', key,
38-
'matches the cron.key attribute in `firebase env:get`');
39-
res.status(403).send('Security key does not match. Make sure your "key" URL query parameter matches the ' +
40-
'cron.key environment variable.');
41-
return null;
42-
}
43-
30+
exports.accountcleanup = functions.pubsub.schedule('every day 00:00').onRun(async context => {
4431
// Fetch all user details.
4532
const inactiveUsers = await getInactiveUsers();
4633
// Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.
4734
const promisePool = new PromisePool(() => deleteInactiveUser(inactiveUsers), MAX_CONCURRENT);
4835
await promisePool.start();
4936
console.log('User cleanup finished');
50-
res.send('User cleanup finished');
51-
return null;
5237
});
5338

5439
/**

delete-unused-accounts-cron/functions/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
"description": "Periodically delete unused Firebase accounts",
44
"dependencies": {
55
"es6-promise-pool": "^2.4.4",
6-
"secure-compare": "^3.0.1",
76
"firebase-admin": "~7.1.1",
8-
"firebase-functions": "^2.2.1"
7+
"firebase-functions": "^2.3.0"
98
},
109
"devDependencies": {
1110
"eslint": "^4.13.1",

0 commit comments

Comments
 (0)