I'm going to show you how to run background tasks
in App Engine applications using queues. When a conference is
created, you can imagine that there might be a variety
of tasks that need to be executed. Such as starting
the process to book the venue, creating the agenda, organizing
the catering. And sending a confirmation email to the organizer.
You'd want to do all of these tasks in the
background rather than trying to
complete everything before the create conference
function returns. In this exercise we're going to use
the default task queue to send a confirmation email, like
this one. When someone creates a conference. The learning
goal is to understand how to use task cues. But
you can understand that there are many other tasks
that needed to be completed, when a new conference is
created. To execute a task in the background using the
task cue. The first thing you need to do is
to find the work to be done. It's up to you
to write the code to execute the task. This can be in
a servlet or in an endpoints function. It just needs to
be somewhere that can be accessed by a URL. Another thing you're
going to need to do is to configure the queue. And
you do that in queue.xml. If you're using the default queue, you
actually don't have to do any configuration. But if you're using
anything other than all the defaults, you're going to need to configure
your queue in queue.xml. So here's an example of
the content of queue.xml. You have the queue-entries element, that
contains. A queue element for every queue that you want
to configure. Specify the name of the queue and the
rate that it operates at. And there's a bunch
of other parameters you can specify as well. And then
you're going to need to write the code to add the
task to the queue. You need to start by getting
the queue. And you use the QueueFactory to
get a queue. If you want the default queue,
then you just call getDefaultQueue on it. If you've
configured a queue in queue.xml and you want to
use that queue. Then you call the getQueue
method on QueueFactory and passing the queueName. Where the
queueName is a name from q.xml. Many need to
specify the options for the tasks. The options include
the parameters for the task and the URL
that's going to execute the task. And the easiest
way to build up the task options, is to
use TaskOptions.Builder. You must provide the URL that's going to
execute the task. When you use TaskOptions.Builder you just
call the withURL method and you pass the URL.
That's going to execute the task. Then to add params
to the task and just call the .param method.
I'm passing the params you can have as many
as you want here I've just got two. If your
task doesn't have any params and obviously you don't
need to do that. You can write the code that
adds the task to the queue, inside of the transaction if you want. And to do
that, you specify the transaction as a very first argument to queue.add.
When you're using objectify. You can just call the ofy.getTransaction
method to get the current transaction. It's important to
understand that you can put the task on the queue
during the transaction. So the task only gets put on
the queue if the transaction is committed. But the execution
of the task takes places outside the transaction. The task
going to be executed whenever the task gets to the
top of the queue, whenever that happens to be. So
long of course, that the transaction has already been committed.
So let's take a look at the send
confirmation email servlet. This servlet has a doPost method
that sends an email whenever anybody creates a conference.
We're implementing as a servlet rather than as an
endpoint function. Because this functionality needs to be
completely internal to the application. We absolutely don't want
anybody else to trigger the sending of these emails.
We've written this servlet for you. You can find
the servlet in the lesson five folder if you want to.
We've already defined it for you completely because our goal here is
not to teach you how to use email but to, how
to add a task to a task queue. However if you prefer
you can write it from scratch yourself. And actually I do
recommend that you read through this code because it does help you
learn how to send emails from App Engine. Which is a cool
thing to know how to do. So, basically what we do is,
we get the email out of the request. We get
the information about the conference out of the request. Then
we create the body for the email, and include the
information about the conference that just got created. We set
the from field, we add the recipient, we set the
subject, we set the text. And then here we send
the email, and then we catch the exception in the
case email didn't get sent. One thing to be aware of
it is there errors in your code and the email
doesn't get sent, your going to get an error in the last.
However if the email doesn't get sent because you have exceeded
your email closure for the day you not going to get an
error here. So if you're email doesn't get sent, you might
want to come and check your quota for sending emails. And you
can see the quota page shows you exactly how many emails
you've sent. Oh good, I've only sent one so far today.
So, you can add the same confirmation email servlet as a
done deal if you want. But then you do need to
write the code to schedule the task, to add the task
to the task queue. And to do that, you're going to
need to update the createConference function, to add the send confirmation
email task to the default task queue. And you're going to be
doing that yourself. First though, I want to show you that
you can use the admin console. To explore your task queues.
So here in the task queues page you see the
quota of your task queues. Iâ€™ve had one API call
go out for task queues so far. And you can
see the individual queues get listed. Iâ€™m only using the default
queue. So, we only see the default queue here. So
I drill down into it and I have nothing in my
queue. So we go to my application though and create
a conference. See here, Iâ€™ve created this conference task queues everybody.
Now very quickly, I'm going to go to the Admin
console, and update my Task Queues page. Okay, I wasn't quick
enough to see it running. You can see that one
task ran in the last minute. If I check my email,
I see that I've got an email confirming the conference
I just created. So we see that my task ran, and
it ran successfully. But I wonder what happens if the
task isn't successful. So over here in the create conference function
in conference API, this is the code where I
add the task to the queue. What I'm going to do
now is, I'm going to change the URL for the task
to be a non existent URL. So, I've redeployed my
app. It has an incorrect URL for the task
that's going to be executed when I create a conference. So
let's just see. Okay, my conference got created. Now let's
go look at the task queues in the admin console.
So now let's see what happens when I refresh the
task queues viewer in the admin console. Okay. So now
looking down into the default queue I see there's one
task in the queue, two were run in the last
minute. It's running five per second. Scroll down, see over
here there has been three retries so far. We are
going to leave that running just for a little bit.
So far we have had nine retries, that's probably enough.
So I am going to purge the queue, to get the
task off the queue, because it is never going to complete.
It is important to understand that when a task gets onto
the push queue and reaches the top of the queue it is
going to. App Engine is going to keep trying to run it,
until it actually succeeds, if you use all the default options.
So it's really important to test your queues, so that you
don't get some task wedged in there that's never going to finish.
One other thing we can do here is we can look in the logs.
And you can see that we do have
this report, that the tasks/send_confirmation_email_x. That no handlers matched
this URL. So whenever you see that kind
of error, no handlers matched this url, that means
your app's trying to do something. With a
URL that doesn't exist and that often just means
you've got a typo in your code. Yeah,
and of course, that never happens. You could actually
choose a different lock level to show so here if
we just show warnings, we very quickly see the problem.
No handlers matches this URL. No handlers
matches this URL. It's pretty easy here to
see what the problem was. So I've shown you how to use the task keys
viewer and admin console. And talked about
how to schedule the task. Now it's your
turn to write the code, to send the
confirmation email whenever anybody creates a new conference.