CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/hub/run/stripe-sync.js
Views: 687
1
#!/usr/bin/node
2
3
/*
4
ALMOST DEPRECATED - THIS EXISTS ONLY TO SUPPORT THE LEGACY UPGRADE SUBSCRIPTIONS.
5
6
1. Periodically pull all data from Stripe for now. The only point of this is
7
to ensure there isn't drift between stripe and our database. Any time a user
8
actually looks at their subscription info, or an admin updates the stripe info
9
(by 'add user to stripe' in account settings) the info is also synced. So this
10
is not super important.
11
12
TODO: Obviously, we should redo this to only pull maybe once per week (?) AND
13
also have a webhook so that any changes in stripe are immediately reflected here...
14
15
2. Periodically
16
17
This is run as a singleton deployment on some preemptible.
18
*/
19
20
const ms = require("ms");
21
const postgres = require("@cocalc/database");
22
const { stripe_sync } = require("@cocalc/server/stripe/sync");
23
const { callback2 } = require("@cocalc/util/async-utils");
24
25
const db = postgres.db({ ensure_exists: false });
26
27
async function do_stripe_sync() {
28
console.log("doing stripe_sync...");
29
await stripe_sync({
30
database: db,
31
logger: { debug: console.log },
32
});
33
console.log("did stripe_sync");
34
}
35
36
// make sure all user upgrades to projects are valid and consistent
37
// (e.g. if upgrades expire remove them)
38
async function upgrade_check() {
39
console.log("doing project upgrade_check...");
40
await callback2(db.ensure_all_user_project_upgrades_are_valid.bind(db), {
41
limit: 1,
42
});
43
console.log("done with project upgrade_check");
44
}
45
46
async function main() {
47
console.log("doing the stripe related periodic tasks...");
48
try {
49
await do_stripe_sync();
50
} catch (err) {
51
console.log(`ERROR do_stripe_sync -- ${err}`);
52
}
53
try {
54
await upgrade_check();
55
} catch (err) {
56
console.log(`ERROR upgrade_check -- ${err}`);
57
}
58
console.log("success -- waiting 11 hours before doing this again...");
59
setTimeout(main, ms("11 hours"));
60
}
61
62
main();
63
64