|
14 | 14 |
|
15 | 15 | // This is a generated sample, using the typeless sample bot. Please
|
16 | 16 | // look for the source TypeScript sample (.ts) for modifications.
|
17 |
| -'use strict'; |
18 | 17 |
|
19 | 18 | // sample-metadata:
|
20 | 19 | // title: Create Job
|
21 | 20 | // description: Create a job that posts to /log_payload on an App Engine service.
|
22 | 21 | // usage: node createJob.js [project-id] [location-id] [app-engine-service-id]
|
23 |
| - |
24 | 22 | const args = process.argv.slice(2);
|
25 | 23 | const [projectId, locationId, serviceId] = args;
|
26 |
| - |
27 | 24 | // [START cloudscheduler_create_job]
|
28 |
| -const {CloudSchedulerClient} = require('@google-cloud/scheduler'); |
29 |
| - |
| 25 | +import { CloudSchedulerClient } from '@google-cloud/scheduler'; |
30 | 26 | // TODO(developer): Uncomment and set the following variables
|
31 | 27 | // const projectId = "PROJECT_ID"
|
32 | 28 | // const locationId = "LOCATION_ID"
|
33 | 29 | // const serviceId = "my-serivce"
|
34 |
| - |
35 | 30 | // Create a client.
|
36 | 31 | const client = new CloudSchedulerClient();
|
37 |
| - |
38 | 32 | /**
|
39 | 33 | * Create a job with an App Engine target via the Cloud Scheduler API
|
40 | 34 | */
|
41 | 35 | async function createJob(projectId, locationId, serviceId) {
|
42 |
| - // Construct the fully qualified location path. |
43 |
| - const parent = client.locationPath(projectId, locationId); |
44 |
| - |
45 |
| - // Construct the request body. |
46 |
| - const job = { |
47 |
| - appEngineHttpTarget: { |
48 |
| - appEngineRouting: { |
49 |
| - service: serviceId, |
50 |
| - }, |
51 |
| - relativeUri: '/log_payload', |
52 |
| - httpMethod: 'POST', |
53 |
| - body: Buffer.from('Hello World'), |
54 |
| - }, |
55 |
| - schedule: '* * * * *', |
56 |
| - timeZone: 'America/Los_Angeles', |
57 |
| - }; |
58 |
| - |
59 |
| - const request = { |
60 |
| - parent: parent, |
61 |
| - job: job, |
62 |
| - }; |
63 |
| - |
64 |
| - // Use the client to send the job creation request. |
65 |
| - const [response] = await client.createJob(request); |
66 |
| - console.log(`Created job: ${response.name}`); |
| 36 | + // Construct the fully qualified location path. |
| 37 | + const parent = client.locationPath(projectId, locationId); |
| 38 | + // Construct the request body. |
| 39 | + const job = { |
| 40 | + appEngineHttpTarget: { |
| 41 | + appEngineRouting: { |
| 42 | + service: serviceId, |
| 43 | + }, |
| 44 | + relativeUri: '/log_payload', |
| 45 | + httpMethod: 'POST', |
| 46 | + body: Buffer.from('Hello World'), |
| 47 | + }, |
| 48 | + schedule: '* * * * *', |
| 49 | + timeZone: 'America/Los_Angeles', |
| 50 | + }; |
| 51 | + const request = { |
| 52 | + parent: parent, |
| 53 | + job: job, |
| 54 | + }; |
| 55 | + // Use the client to send the job creation request. |
| 56 | + const [response] = await client.createJob(request); |
| 57 | + console.log(`Created job: ${response.name}`); |
67 | 58 | }
|
68 |
| - |
69 | 59 | createJob(projectId, locationId, serviceId).catch(err => {
|
70 |
| - console.error(err.message); |
71 |
| - process.exitCode = 1; |
| 60 | + console.error(err.message); |
| 61 | + process.exitCode = 1; |
72 | 62 | });
|
73 | 63 | // [END cloudscheduler_create_job]
|
0 commit comments