Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update cloud task samples #610

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 28 additions & 48 deletions appengine/cloudtasks/createTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,46 @@

'use strict';

const google = require('googleapis').google;
const {google} = require('googleapis');
const cloudtasks = google.cloudtasks('v2beta2');

/**
* Create a task for a given queue with an arbitrary payload.
*/
function createTask (project, location, queue, options) {
async function createTask (project, location, queue, options) {
// [START cloud_tasks_appengine_create_task]
authorize((authClient) => {
const task = {
app_engine_http_request: {
http_method: 'POST',
relative_url: '/log_payload'
}
};

if (options.payload !== undefined) {
task.app_engine_http_request.payload = Buffer.from(options.payload).toString('base64');
}
const authClient = await google.auth.getClient({
scopes: ['https://www.googleapis.com/auth/cloud-platform']
});

if (options.inSeconds !== undefined) {
task.schedule_time = (new Date(options.inSeconds * 1000 + Date.now())).toISOString();
const task = {
app_engine_http_request: {
http_method: 'POST',
relative_url: '/log_payload'
}
};

const request = {
parent: `projects/${project}/locations/${location}/queues/${queue}`, // TODO: Update placeholder value.
resource: {
task: task
},
auth: authClient
};
if (options.payload !== undefined) {
task.app_engine_http_request.payload = Buffer.from(options.payload).toString('base64');
}

console.log('Sending task %j', task);
if (options.inSeconds !== undefined) {
task.schedule_time = (new Date(options.inSeconds * 1000 + Date.now())).toISOString();
}

cloudtasks.projects.locations.queues.tasks.create(request, (err, response) => {
if (err) {
console.error(err);
return;
}
const request = {
parent: `projects/${project}/locations/${location}/queues/${queue}`, // TODO: Update placeholder value.
resource: {
task: task
},
auth: authClient
};

console.log('Created task.', response.name);
console.log(JSON.stringify(response, null, 2));
});
});
console.log('Sending task %j', task);

function authorize (callback) {
google.auth.getApplicationDefault(function (err, authClient) {
if (err) {
console.error('authentication failed: ', err);
return;
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
authClient = authClient.createScoped(scopes);
}
callback(authClient);
});
}
const response = await cloudtasks.projects.locations.queues.tasks.create(request);
console.log('Created task.', response.name);
console.log(JSON.stringify(response, null, 2));
// [END cloud_tasks_appengine_create_task]
}

Expand Down Expand Up @@ -121,10 +103,8 @@ const cli = require(`yargs`)

if (module === require.main) {
const opts = cli.help().parse(process.argv.slice(2));

process.env.GCLOUD_PROJECT = opts.project;

createTask(opts.project, opts.location, opts.queue, opts);
createTask(opts.project, opts.location, opts.queue, opts).catch(console.error);
}

exports.createTask = createTask;
2 changes: 1 addition & 1 deletion appengine/cloudtasks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dependencies": {
"body-parser": "1.18.2",
"express": "4.16.2",
"googleapis": "27.0.0",
"googleapis": "29.0.0",
"yargs": "11.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion appengine/loopback/server/boot/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = function(server) {
// Install a `/` route that returns server status
var router = server.loopback.Router();
const router = server.loopback.Router();
router.get('/', server.loopback.status());
server.use(router);
};
2 changes: 1 addition & 1 deletion cloudtasks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test": "repo-tools test run --cmd npm -- run system-test"
},
"dependencies": {
"googleapis": "27.0.0",
"googleapis": "29.0.0",
"yargs": "11.0.0"
},
"devDependencies": {
Expand Down
164 changes: 55 additions & 109 deletions cloudtasks/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

'use strict';

function createTask (project, location, queue) {
async function createTask (project, location, queue) {
// [START cloud_tasks_create_task]
const google = require('googleapis').google;
const {google} = require('googleapis');
const cloudtasks = google.cloudtasks('v2beta2');

/**
Expand All @@ -27,56 +27,38 @@ function createTask (project, location, queue) {
// const location = 'Location of queue, e.g. us-central1';
// const queue = 'Queue ID, e.g. queue-1';

authorize((authClient) => {
// Schedule the task for 2 minutes from now
const scheduleTime = new Date();
scheduleTime.setUTCMinutes(scheduleTime.getUTCMinutes() + 2);

const task = {
scheduleTime: scheduleTime,
pull_message: {
payload: Buffer.from('a message for the recipient').toString('base64')
}
};

const request = {
parent: `projects/${project}/locations/${location}/queues/${queue}`,
resource: {
task: task
},
auth: authClient
};

cloudtasks.projects.locations.queues.tasks.create(request, (err, response) => {
if (err) {
console.error('ERROR:', err);
return;
}

console.log(`Created task ${response.data.name}.`);
console.log(JSON.stringify(response.data, null, 2));
});
const authClient = await google.auth.getClient({
scopes: ['https://www.googleapis.com/auth/cloud-platform']
});

function authorize (callback) {
google.auth.getApplicationDefault(function (err, authClient) {
if (err) {
console.error('authentication failed: ', err);
return;
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
authClient = authClient.createScoped(scopes);
}
callback(authClient);
});
}
// Schedule the task for 2 minutes from now
const scheduleTime = new Date();
scheduleTime.setUTCMinutes(scheduleTime.getUTCMinutes() + 2);

const task = {
scheduleTime: scheduleTime,
pull_message: {
payload: Buffer.from('a message for the recipient').toString('base64')
}
};

const request = {
parent: `projects/${project}/locations/${location}/queues/${queue}`,
resource: {
task: task
},
auth: authClient
};

const response = await cloudtasks.projects.locations.queues.tasks.create(request);
console.log(`Created task ${response.data.name}.`);
console.log(JSON.stringify(response.data, null, 2));
// [END cloud_tasks_create_task]
}

function pullTask (project, location, queue) {
async function pullTask (project, location, queue) {
// [START cloud_tasks_pull_and_acknowledge_task]
const google = require('googleapis').google;
const {google} = require('googleapis');
const cloudtasks = google.cloudtasks('v2beta2');

/**
Expand All @@ -86,42 +68,24 @@ function pullTask (project, location, queue) {
// const location = 'Location of queue, e.g. us-central1';
// const queue = 'Queue ID, e.g. queue-1';

authorize((authClient) => {
const request = {
parent: `projects/${project}/locations/${location}/queues/${queue}`,
responseView: 'FULL',
pageSize: 1,
auth: authClient
};

cloudtasks.projects.locations.queues.tasks.list(request, (err, response) => {
if (err) {
console.error(err);
return;
}

const task = response.data.tasks[0];
console.log('Pulled task %j', task);
});
const authClient = await google.auth.getClient({
scopes: ['https://www.googleapis.com/auth/cloud-platform']
});

function authorize (callback) {
google.auth.getApplicationDefault(function (err, authClient) {
if (err) {
console.error('authentication failed: ', err);
return;
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
authClient = authClient.createScoped(scopes);
}
callback(authClient);
});
}
const request = {
parent: `projects/${project}/locations/${location}/queues/${queue}`,
responseView: 'FULL',
pageSize: 1,
auth: authClient
};

const response = await cloudtasks.projects.locations.queues.tasks.list(request);
const task = response.data.tasks[0];
console.log('Pulled task %j', task);
}

function acknowledgeTask (task) {
const google = require('googleapis').google;
async function acknowledgeTask (task) {
const {google} = require('googleapis');
const cloudtasks = google.cloudtasks('v2beta2');

/**
Expand All @@ -132,36 +96,18 @@ function acknowledgeTask (task) {
// scheduleTime: '2017-11-01T21:02:28.994Z' // TODO(developer): set this to your task's scheduled time
// };

authorize((authClient) => {
const request = {
name: task.name,
scheduleTime: task.scheduleTime,
auth: authClient
};

cloudtasks.projects.locations.queues.tasks.acknowledge(request, (err, response) => {
if (err) {
console.error(err);
return;
}

console.log(`Acknowledged task ${task.name}.`);
});
const authClient = await google.auth.getClient({
scopes: ['https://www.googleapis.com/auth/cloud-platform']
});

function authorize (callback) {
google.auth.getApplicationDefault(function (err, authClient) {
if (err) {
console.error('authentication failed: ', err);
return;
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
authClient = authClient.createScoped(scopes);
}
callback(authClient);
});
}
const request = {
name: task.name,
scheduleTime: task.scheduleTime,
auth: authClient
};

await cloudtasks.projects.locations.queues.tasks.acknowledge(request);
console.log(`Acknowledged task ${task.name}.`);
// [END cloud_tasks_pull_and_acknowledge_task]
}

Expand All @@ -171,19 +117,19 @@ require(`yargs`) // eslint-disable-line
`create <project> <location> <queue>`,
`Create a task.`,
{},
(opts) => createTask(opts.project, opts.location, opts.queue)
(opts) => createTask(opts.project, opts.location, opts.queue).catch(console.error)
)
.command(
`pull <project> <location> <queue>`,
`Pull a task.`,
{},
(opts) => pullTask(opts.project, opts.location, opts.queue)
(opts) => pullTask(opts.project, opts.location, opts.queue).catch(console.error)
)
.command(
`acknowledge <task>`,
`Acknowledge a task.`,
{},
(opts) => acknowledgeTask(JSON.parse(opts.task))
(opts) => acknowledgeTask(JSON.parse(opts.task)).catch(console.error)
)
.example(`node $0 create my-project-id us-central1 my-queue`)
.example(`node $0 pull my-project-id us-central1 my-queue`)
Expand Down
2 changes: 1 addition & 1 deletion kms/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ function disableCryptoKeyVersion (projectId, locationId, keyRingId, cryptoKeyId,

function buildAndAuthorizeService (callback) {
// Imports the Google APIs client library
const google = require('googleapis').google;
const {google} = require('googleapis');

// Acquires credentials
google.auth.getApplicationDefault((err, authClient) => {
Expand Down
2 changes: 1 addition & 1 deletion kms/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

// [START kms_quickstart]
// Imports the Google APIs client library
const google = require('googleapis').google;
const {google} = require('googleapis');

// Your Google Cloud Platform project ID
const projectId = process.env.GCLOUD_PROJECT;
Expand Down