-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathusing-tasks.html.md.erb
259 lines (187 loc) · 10.9 KB
/
using-tasks.html.md.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
---
title: Running tasks in your apps
owner: CAPI/CLI
---
A task is an app or script, the code of which is included as part of a deployed app, but runs
independently in its own container. This topic describes how to run tasks in <%= vars.app_runtime_first %>.
## <a id='about-tasks'></a> Tasks in <%= vars.app_runtime_abbr %>
In contrast to a long-running process (LRP), tasks run for a finite amount of time, then stop. Tasks run in their own containers and are designed to use minimal resources. After a task runs, <%= vars.app_runtime_abbr %> destroys the container running the task.
As a single use object, a task can be checked for its state and for a success or failure message.
<%= vars.tasks_ai %>
### <a id='use-cases'></a> Use cases for tasks
Tasks are used to perform one-off jobs, which include:
* Migrating a database
* Sending an email
* Running a batch job
* Running a data processing script
* Processing images
* Optimizing a search index
* Uploading data
* Backing-up data
* Downloading content
### <a id='task-processes'></a> How tasks are run
Tasks are always run asynchronously, meaning that they run independently from the parent app or other tasks that run on the same app.
The life cycle of a task is as follows:
1. A user initiates a task in <%= vars.app_runtime_abbr %> using one of the following mechanisms:
* The `cf run-task APP-NAME "TASK"` command. For more information, see [Running tasks in your apps](#run-tasks).
* A Cloud Controller v3 API call. For more information, see the [Cloud Foundry API documentation](http://v3-apidocs.cloudfoundry.org/version/3.0.0/index.html#tasks).
* The Cloud Foundry Java Client. For more information, see [Cloud Foundry Java Client Library](../buildpacks/java/java-client.html) and the [Cloud Foundry Java Client](https://github.com/cloudfoundry/cf-java-client) repository on GitHub.
1. <%= vars.app_runtime_abbr %> creates a container specifically for the task.
1. <%= vars.app_runtime_abbr %> runs the task on the container using the value passed to the `cf run-task` command.
1. <%= vars.app_runtime_abbr %> destroys the container.
The container also inherits environment variables, service bindings, and security groups bound to the app.
<p class="note">
You cannot SSH into the container running a task.</p>
### <a id='task-logging-execution'></a> Task logging and execution history
Any data or messages the task outputs to stdout or stderr is available in the firehose logs of the app. A syslog drain attached to the app receives the task log output.
The task execution history is retained for one month.
## <a id='manage-tasks'></a> Manage tasks
At the system level, a user with admin-level privileges can use the Cloud Controller v3 API to view all tasks that are running within an org or space. For
more information, see the [Cloud Foundry API documentation](http://v3-apidocs.cloudfoundry.org/version/3.0.0/index.html#list-tasks).
Admins can set the default memory, disk usage and log rate quotas for tasks on a global level.
Tasks use the same memory, disk usage, and log rate limit defaults as apps, unless you customize them using the `cf run-task` command. For more information
about the `cf run-task` command, see the [Cloud Foundry CLI reference guide](https://cli.cloudfoundry.org/en-US/v8/run-task.html).
<% if vars.platform_code == 'PCF' %>
<%= partial "/pcf/core/tasks_rec_alloc_pcf" %>
<% else %>
<%= partial 'tasks_rec_alloc_oss' %>
<% end %>
## <a id='run-tasks'></a> Run a task on an app
You can use the Cloud Foundry Command Line Interface (cf CLI) to run a task in the context of an app.
<div class="note important">
<ul>
<li>To run tasks with the cf CLI, you must install cf CLI v6.23.0 or later, or install cf CLI v7 or v8. To download, install, and uninstall the cf CLI, see <a href="../cf-cli/install-go-cli.html">Installing the Cloud Foundry Command Line Interface</a>.</li>
<li>To run a task using cf CLI v6 without starting the app, push the app with <code>cf push -i 0</code> and then run the task. You can run the app later by scaling up its instance count.</li>
</ul>
</div>
### <a id='run-tasks-v6'></a> Run a task on an app with cf CLI v6
To run a task on an app with cf CLI v6:
1. In a terminal window, push your app by running:
```
cf push APP-NAME
```
Where `APP-NAME` is the name of your app,
1. Run your task on the deployed app by running:
```
cf run-task APP-NAME "TASK" --name TASK-NAME
```
Where:
<ul>
<li><code>APP-NAME</code> is the name of your app.</li>
<li><code>TASK</code> is the task you want to run.</li>
<li><code>TASK-NAME</code> is the name you want to give the task.</li>
</ul>
The following example command runs a database migration as a task on the `example-app` app:
<pre class="terminal">
cf run-task example-app "bin/rails db:migrate" --name example-task
</pre>
When the task runs successfully, you see an output similar to the following example:
<pre class="terminal">
Creating task for app example-app in org example-org / space development as [email protected]...
OK
Task 1 has been submitted successfully for execution.
</pre>
<p class="note">
To run a task again, you must run it as a new task using the previous command.</p>
1. To display the recent logs of the app and all its tasks, run:
```
cf logs APP-NAME --recent
```
Where `APP-NAME` is the name of your app.
<br><br>
If a task succeeds, you see logs similar to the following example:
<pre class="terminal">
2017-01-03T15:58:06.57-0800 [APP/TASK/my-task/0]OUT Creating container
2017-01-03T15:58:08.45-0800 [APP/TASK/my-task/0]OUT Successfully created container
2017-01-03T15:58:13.32-0800 [APP/TASK/my-task/0]OUT D, [2017-01-03T23:58:13.322258 #7] DEBUG -- : (15.9ms) CREATE TABLE "schema_migrations" ("version" character varying PRIMARY KEY)
2017-01-03T15:58:13.33-0800 [APP/TASK/my-task/0]OUT D, [2017-01-03T23:58:13.337723 #7] DEBUG -- : (11.9ms) CREATE TABLE "ar_internal_metadata" ("key" character varying PRIMARY KEY, "value" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
2017-01-03T15:58:13.34-0800 [APP/TASK/my-task/0]OUT D, [2017-01-03T23:58:13.340234 #7] DEBUG -- : (1.6ms) SELECT pg_try_advisory_lock(3720865444824511725);
2017-01-03T15:58:13.35-0800 [APP/TASK/my-task/0]OUT D, [2017-01-03T23:58:13.351853 #7] DEBUG -- : ActiveRecord::SchemaMigration Load (0.7ms) SELECT "schema_migrations".* FROM "schema_migrations"
2017-01-03T15:58:13.35-0800 [APP/TASK/my-task/0]OUT I, [2017-01-03T23:58:13.357294 #7] INFO -- : Migrating to Createtopics (20161118225627)
2017-01-03T15:58:13.35-0800 [APP/TASK/my-task/0]OUT D, [2017-01-03T23:58:13.359565 #7] DEBUG -- : (0.5ms) BEGIN
2017-01-03T15:58:13.35-0800 [APP/TASK/my-task/0]OUT == 20161118225627 Createtopics: migrating ===================================
2017-01-03T15:58:13.50-0800 [APP/TASK/my-task/0]OUT Exit status 0
2017-01-03T15:58:13.56-0800 [APP/TASK/my-task/0]OUT Destroying container
2017-01-03T15:58:15.65-0800 [APP/TASK/my-task/0]OUT Successfully destroyed container
</pre>
If a task fails, you see logs similar to the following example:
<pre class="terminal">
2016-12-14T11:09:26.09-0800 [APP/TASK/my-task/0]OUT Creating container
2016-12-14T11:09:28.43-0800 [APP/TASK/my-task/0]OUT Successfully created container
2016-12-14T11:09:28.85-0800 [APP/TASK/my-task/0]ERR bash: bin/rails: command not found
2016-12-14T11:09:28.85-0800 [APP/TASK/my-task/0]OUT Exit status 127
2016-12-14T11:09:28.89-0800 [APP/TASK/my-task/0]OUT Destroying container
2016-12-14T11:09:30.50-0800 [APP/TASK/my-task/0]OUT Successfully destroyed container
</pre>
If your task name is unique, you can `grep` the output of the `cf logs` command for the task name to view task-specific logs.
### <a id='run-tasks-v7'></a> Run a task on an app with cf CLI v7
To run a task on an app with cf CLI v7:
1. Configure your v3 API manifest with a task as a process type. For more information, see the [Cloud Foundry API documentation](https://v3-apidocs.cloudfoundry.org/version/3.78.0/index.html#the-app-manifest-specification).
1. In a terminal, push your app by running:
```
cf push APP-NAME --task
```
Where `APP-NAME` is the name of your app.
1. Run your task on the deployed app by running:
```
cf run-task APP-NAME --name TASK-NAME
```
Where:
<ul>
<li><code>APP-NAME</code> is the name of your app.</li>
<li><code>TASK-NAME</code> is the name you want to give the task.</li>
</ul>
<p class="note important">
<code>cf run-task</code> allows you to include the <code>--process</code> and <code>--command</code> flags. Including the <code>--command</code> flag overrides the manifest property.</p>
The following example command runs a task on the `example-app` app:
<pre class="terminal">
cf run-task example-app --name example-task
</pre>
When the task runs successfully, you see terminal output similar to the following example:
<pre class="terminal">
Creating task for app example-app in org example-org / space development as [email protected]...
OK
Task 1 has been submitted successfully for execution.
</pre>
1. To display the recent logs of the app and all its tasks, run:
```
cf logs APP-NAME --recent
```
Where `APP-NAME` is the name of your app.
## <a id='list-tasks'></a> List tasks running on an app
To list the tasks for a given app:
1. In a terminal window, run:
```
cf tasks APP-NAME
```
Where `APP-NAME` is the name of your app. The command returns output similar to the following example:
<pre class="terminal">
Getting tasks for app example-app in org example-org / space development as [email protected]...
OK
id name state start time command
2 339044ef FAILED Wed, 23 Nov 2016 21:52:52 UTC echo foo; sleep 100; echo bar
1 8d0618cf SUCCEEDED Wed, 23 Nov 2016 21:37:28 UTC bin/rails db:migrate
</pre>
Tasks can be in these states:
| State | Description |
| ----- | ----------- |
| `RUNNING` | The task is in progress. |
| `FAILED` | The task did not complete. This state occurs when a task does not work correctly or a user cancels the task. |
| `SUCCEEDED` | The task completed successfully. |
## <a id='cancel-task'></a> Cancel a task
After you run a task, you might be able to cancel it before it finishes.
To cancel a running task:
1. In a terminal window, run:
```
cf terminate-task APP-NAME TASK-ID
```
Where:
<ul>
<li><code>APP-NAME</code> is the name of your app.</li>
<li><code>TASK-NAME</code> is the name you want to give the task.</li>
</ul>
The previous command returns output similar to the following example:
<pre class="terminal">
Terminating task 2 of app example-app in org example-org / space development as [email protected]...
OK
</pre>