Usa el controlador de tareas para activar Cloud Functions
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Usa Cloud Tasks para activar Cloud Functions.
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],[],[],[],null,["# Use task handler to trigger Cloud Functions\n\nUses Cloud Tasks to trigger Cloud Functions.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Trigger Cloud Run functions using Cloud Tasks](/tasks/docs/tutorial-gcf)\n\nCode sample\n-----------\n\n### Node.js\n\n\nTo learn how to install and use the client library for Cloud Tasks, see\n[Cloud Tasks client libraries](/tasks/docs/reference/libraries).\n\n\nTo authenticate to Cloud Tasks, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const sendgrid = require('@sendgrid/mail');\n\n /**\n * Responds to an HTTP request from Cloud Tasks and sends an email using data\n * from the request body.\n *\n * @param {object} req Cloud Function request context.\n * @param {object} req.body The request payload.\n * @param {string} req.body.to_email Email address of the recipient.\n * @param {string} req.body.to_name Name of the recipient.\n * @param {string} req.body.from_name Name of the sender.\n * @param {object} res Cloud Function response context.\n */\n exports.sendEmail = async (req, res) =\u003e {\n // Get the SendGrid API key from the environment variable.\n const key = process.env.SENDGRID_API_KEY;\n if (!key) {\n const error = new Error(\n 'SENDGRID_API_KEY was not provided as environment variable.'\n );\n error.code = 401;\n throw error;\n }\n sendgrid.setApiKey(key);\n\n // Get the body from the Cloud Task request.\n const {to_email, to_name, from_name} = req.body;\n if (!to_email) {\n const error = new Error('Email address not provided.');\n error.code = 400;\n throw error;\n } else if (!to_name) {\n const error = new Error('Recipient name not provided.');\n error.code = 400;\n throw error;\n } else if (!from_name) {\n const error = new Error('Sender name not provided.');\n error.code = 400;\n throw error;\n }\n\n // Construct the email request.\n const msg = {\n to: to_email,\n from: 'postcard@example.com',\n subject: 'A Postcard Just for You!',\n html: postcardHTML(to_name, from_name),\n };\n\n try {\n await sendgrid.send(msg);\n // Send OK to Cloud Task queue to delete task.\n res.status(200).send('Postcard Sent!');\n } catch (error) {\n // Any status code other than 2xx or 503 will trigger the task to retry.\n res.status(error.code).send(error.message);\n }\n };\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=cloud_tasks)."]]