[MUSIC PLAYING]
SPEAKER: Hi, everybody, and welcome back
to what really truly this time should
be the last video in this Firebase Semi-live series.
So we've been making a whole lot of progress.
And as you'll recall in the last video,
we had just finished communicating with our billing
service and properly querying whether we had a billing
account enabled for a project.
And then I kind of cut the video short because I was like,
this has taken a long time.
And I don't want to have people watch like a 50-minute video
or what have you.
So we're going to continue where we left off
from the last video.
Sorry for the kind of awkward transition.
But we have just finished talking to our billing service.
And now, we're going to implement
the final part of our Cloud Function, which
is going to be to disable billing,
again, kind of ripping the cord out of the wall,
in our project to make sure that we don't incur any more billing
costs.
So let's pick up where we left off.
It looks like I am able to get at my billing information.
I can see that billing is currently enabled.
And basically, what I found is that if I want to turn off
billing, all I essentially need to do
is set my billing account name to null.
So let's go ahead and put that into our code as well.
So I'm going to create one more function here called
disable billing for real.
And I will set my credentials for billing.
And if I have my project name, make sure that
isn't null for some reason.
We'll say billing info equals await billing.get billing info.
And again, we'll grab our name as our project name.
And then what I will do is, I guess,
if billing info.data.billing enabled,
well then, we can go ahead and disable billing.
So let's see here.
So we'll say our result equals await billing update billing
info.
And then what we're going to do here
is under our parameters we'll set our name to our project
name.
And then let me check our documentation here.
We're going to set resource equal to billing
account name equal to zero.
So resource.
And in here, we'll have billing account name empty string.
And why is it giving me errors?
No overload matches this [INAUDIBLE]..
Well, I think what was interesting was--
it's funny, as I was typing name,
it had no problem doing some completion.
But then when I tried to do resource,
it didn't give me that.
Let's see.
Let's look at this call here.
These are the parameters.
Update billing info.
Here's our request body.
Wait, request body, not resource.
Request body.
Maybe that's a bug in our documentation.
I'm not sure.
Well, there you go.
But apparently, going in here and just
looking at what parameters could possibly
be passed to update billing info lets us figure that out pretty
quickly.
And then, yeah, under request body,
we can see some of the things we could
set-- billing account name, billing enabled, name
or project ID.
It's funny-- the documentation says don't
set billing enabled to false.
Set billing account name to blank.
So that's what I'm going to do.
And that is probably all I need to do.
So now, I can console log.
I think we just disabled billing for our project name.
And we'll console log our result.
Just see what that looks like.
And otherwise, looks like you already disabled billing.
In which case, I'm sort of surprised
this function is even running because we're
running in node 10.
And node 10, I believe, requires billing.
So we have a function set up that
is going to actually disable billing for real.
Let's call it.
Yikes.
All right, I'm frightened.
So down in here, I've got some of my constants,
my billing alert increment, projected
cost to freak out about.
I'm going to add in one more constant that is going to be,
let's call it, killing project amount.
And this will be $10.
For projects costing me more than $10, I want to kill it.
And so what I will do is down, let's just do it down here,
if, and so we have our spent so far amount
is greater than or equal to our killing project amount,
then here we go.
We will call disable billing for real.
I'm going to make sure that we definitely
send a message about this.
And our message string is going to be warning,
I have disabled billing for this project.
Very frightening, very scary.
All right.
Now, I'm going to run this.
And I should warn you--
even though we are running this locally on our local host,
right now, we're still kind of talking
to our actual production--
you might have noticed--
we're talking to our actual production app.
And so if I were to call this right now,
even while I'm testing it locally,
it's going to go ahead and shut down that project in production
for real.
So for crying out loud, please be careful
when you're running this and testing this.
I hope as you're following along you're testing this in an app
that you don't actually care about too much.
Because we are about to pull the plug on it I'm pretty sure.
So let's see.
We got no errors.
I'm going to go ahead, and here we'll
do something that looks pretty normal and innocuous, right?
We're going to just spend $0.40.
And everything's still happy.
We got a nice little warning message,
or just like nice, little not particularly concerned Slack
message.
Now, we're going to spend $0.45, and Slack's
looking a little more worried.
Gee, it seems to be we're suddenly spending an awful lot
more per hour.
I'm going to change this to $2.45.
OK, yeah, this is bad.
And now I'm going to do the thing that's going to, I think,
kill my project.
This thing's hard to test, but here we go.
We call this-- it looks like it's
disabled billing for the project.
Let me just make sure I didn't get anything weird
happening in my logs.
It looks like, you can see my response here,
I've gotten back that my billing account name is now blank,
and billing enabled is false.
And so, in fact, if I were to call, I believe--
if I were to call my get billing info function--
yeah, this would then print back some billing info
where that billing seems to be disabled.
Let me go into my app and see what's going on.
So what you're going to start to see is--
I'm going to go into usage and billing.
It usually takes about three or four or five minutes for this
to start getting reflected in here.
The fact that I am now back on the free plan.
OK.
But now it's about one minute later.
And a function that I have called that, in theory, is
going to write in a lot of documents to my database
is suddenly running into resource exhausted errors.
Quota exceeded.
And so it's failed to write these documents in my database.
And you'll start to see probably more of that.
Let's see now in my usage and billing--
OK.
Suddenly, I am now getting some errors with my Cloud storage,
where it's saying, hey, you've used up all your quota.
Please upgrade to make sure there's no service disruption.
Things are going to start to look weird and bad as I
start to get into-- there we go, Cloud Firestore rights.
That's probably what it meant, it's called Firestore.
Again, running into all these errors saying your quota
has been used up.
You're no longer able to write to your database.
My project is now listed as being in the Spark plan, which
is free, and more and more of my Cloud functions
are going to fail because I've removed billing.
And these things are gradually failing.
So yeah, you can basically see I have killed my app.
I have killed the usage to it.
I'm no longer going to be able to go over the free quota.
The flip side is, I guess now at this point,
I'm probably no longer spending any money within this app.
Now, if I want to go ahead and enable it again,
I can click Upgrade.
And I would select the Blaze Pay As You Go plan.
In fact, I guess, let me just do that.
I'm going to continue on my billing account as before.
I'll set up a budget alert of $33.
And gradually, things will start to look good again, hopefully
fairly soon.
It sometimes can take a little while, but actually now,
it looks like it's starting to go back up again,
and billing has become enabled.
And things are starting to get happier and happier again.
You will notice, if I go into my billing section,
under budgets and alerts, I think it creates--
it's created a second budget for this project.
I think I now basically have two budgets set up,
both pointed to the same project.
I might want to look at eliminating one of these.
I kind of assume this old one is still
going to be working, even though I kind of disabled
and re-enabled the billing project.
But a couple of weird things like this will happen.
This is, like I said, not a thing
you want to be doing normally.
It's the equivalent of pulling the cord out of the wall
while your game console is showing the, wait, autosaving
your game function.
So yeah, be careful around doing this.
This really should be used in case of emergencies.
Don't do this as my I'm going to do this every day
to make sure I don't go over my spending limit or something.
No, this is again, use for emergencies.
You're breaking the glass.
You're pulling the fire alarm, that sort of thing.
Whew.
So we did it.
I know this one was a long video,
but we did what we wanted to do from the beginning, which
is we set up that kill switch that is going to go in.
And it's going to kill our project
if our budget costs really--
if our pricing really seems out of control.
I think I've given you enough warnings now
that I don't need to give you yet another warning
around be careful around using this.
With great power comes great responsibility.
But for those of you maybe building a weekend hackathon
project, and you're just really worried because there really
shouldn't be any usage on this, maybe this
is something you could look into.
So with that, thanks for tagging along with me on this Firebase
semi-live series.
Is there more you want to see me do?
I'm always open for new ideas, new topics.
You can watch me flail around in TypeScript somewhere,
if that hasn't been painful enough
for those of you that are actual TypeScript developers.
So thanks for watching, and I will see you soon
on another episode, sometime in the future,
of Firebase semi-live.
Bye, everyone.
[MUSIC PLAYING]