Send emails using a Function Compute node

更新时间:
复制 MD 格式

This topic describes how to call Function Compute from a Function Compute node in DataWorks to send emails.

Background

DataWorks uses a Function Compute node to invoke Function Compute. This allows you to run custom logic as part of a DataWorks workflow.

Prerequisites

  • You have activated DataWorks. For more information, see Activate DataWorks.

  • You have activated Function Compute. For more information, see Quickly create a function.

Limitations

  • Feature limitations

    DataWorks supports invoking only event functions, not HTTP functions. Therefore, you must use an event function for tasks that you want to schedule periodically. For more information about function types, see Function types.

  • Region limitations

    The Function Compute feature is available only in workspaces that reside in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), China (Zhangjiakou), China (Shenzhen), China (Chengdu), China (Hong Kong), Singapore, Malaysia (Kuala Lumpur), Indonesia (Jakarta), Germany (Frankfurt), UK (London), US (Silicon Valley), and US (Virginia).

Step 1: Create a function

  1. Log on to the Function Compute console and select the target region from the top navigation bar.

  2. In the left-side navigation pane, click Function.

  3. On the Function page, click Create Function.

  4. Select Event Function and configure the function.

    Parameter

    Description

    Function Name

    Enter a name for the function. This example uses send-mail-fc.

    Environment

    This example uses Python 3.9.

    Code upload method

    This example uses Use Sample Code and the Hello, world! sample.

    You can configure Advanced Settings and Environment variables based on your business requirements. For more information, see Create a function.

  5. Click Create. You are redirected to the Code tab on the function details page.

    The code editor for the send-mail-fc function is displayed. The runtime is Python 3.9, the current version is LATEST, and a default code template is generated in the editor.

  6. The following example code sends an email:

    Important
    • In the following sample code, replace the values of the mail_host, mail_port, mail_username, mail_password, mail_sender, and mail_receivers parameters with your actual values.

    • The email account must have the Simple Mail Transfer Protocol (SMTP) service enabled. Some email providers disable this service by default, requiring you to enable it manually.

    • For security reasons, some email providers, such as 163.com, require an authorization code, which is a dedicated password for logging in from third-party clients. In this case, you must set mail_password to this authorization code instead of your regular account password.

    # -*- coding: utf-8 -*-
    import logging
    import json
    import smtplib
    from email.mime.text import MIMEText
    def handler(event, context):
      evts = json.loads(event)
      logger = logging.getLogger()
      logger.info('event: %s', evts)
      mail_host = 'smtp.163.com'                 ## Email server address
      mail_port = '465';                         ## SMTP port
      mail_username = 'sender_****@163.com'      ## Logon username
      mail_password = 'EWEL******KRU'            ## Logon password
      mail_sender = 'sender_****@163.com'        ## Sender email address
      mail_receivers = ['receiver_****@163.com'] ## Recipient email addresses
      mail_content=generate_mail_content(evts)
      message = MIMEText(mail_content,'plain','utf-8')
      message['Subject'] = 'mail test'
      message['From'] = mail_sender
      message['To'] = mail_receivers[0]
      smtpObj = smtplib.SMTP_SSL(mail_host + ':' + mail_port)
      smtpObj.login(mail_username,mail_password)
      smtpObj.sendmail(mail_sender,mail_receivers,message.as_string())
      smtpObj.quit()
      return 'mail send success'
    def generate_mail_content(evt):
      mail_content=''
      if 'msg' in evt.keys():
          mail_content=evt['msg']
      else:
          logger = logging.getLogger()
          logger.error('msg not present in event')
      '''
      You can add logic here to process the email content.
      '''
      return mail_content
  7. Click Deploy Code.

  8. Click the drop-down arrowimage next to Test Function, select Configure Test Parameters, enter the test parameters, and then click Determine.

    On the Create New Test Event tab, select hello-world for Event Template and enter test-event for Event Name.

    Enter the following event content:

    {
        "msg": "This is a test message"
    }
  9. Click Test Function to complete the test.

    After the execution succeeds, you can view the request content, such as {"msg": "This is a test message"}, on the Request Information tab in the Details pane.

  10. Check the recipient's inbox for the test email.

    Note

    Some email servers may identify emails from unknown senders as spam and block them. If you cannot find the test email in your inbox, check your spam folder.

Step 2: Create and configure a Function Compute node

  1. Log on to the DataWorks console.

  2. In the left-side navigation pane, click Workspace to go to the Workspace list page.

  3. In the top navigation bar, switch to the region you selected in Step 1: Create a function.

  4. In the Workspace list, click the name of the target workspace to go to the Workspace Details page. If you do not have a workspace in the current region, you must create one. For more information, see Create a workspace.

  5. In the left-side navigation pane, choose Data Modeling and Development > Data Studio to go to the DataStudio page.

  6. Click the target Workflow name, right-click General in the business flow, and select Create Node > Function Compute. In the Create Node dialog box, enter a node name and click Determine to create the Function Compute node.

  7. Configure the parameters for the Function Compute node.

    Parameter

    Description

    Select Version or Alias

    Select the service version or alias for function invocations. The default version is LATEST. This example uses the Default Version.

    • Service version

      Function Compute provides a service-level versioning feature that allows you to publish one or more versions of your service. When you publish a version, Function Compute creates a snapshot of the service, including its configuration, function code, and function configurations. Triggers are not included. Function Compute then automatically assigns a version number to the snapshot for future use. For more information, see Publish a version.

    • Version alias

      Function Compute allows you to create an alias for a service version. An alias points to a specific version, which facilitates operations such as publishing, rollbacks, and canary releases. An alias cannot exist independently of a service or version. When you use an alias to access a service or function, Function Compute resolves the alias to the version it points to. The caller does not need to know the specific version. For more information, see Create an alias.

    Select Function

    Select the function that you created in Step 1. This example uses send-mail-fc. To create a new function, see Manage functions.

    Note

    DataWorks supports invoking only event functions, not HTTP functions. Therefore, you must use an event function for tasks that you want to schedule periodically. For more information about function types, see Function types.

    Invocation Method

    This example uses and synchronizes. For more information about invocation methods, see Function invocation.

    • Synchronous invocation: The event directly triggers the function. Function Compute runs the function and waits for a response. After the function is invoked, Function Compute returns the execution result.

    • Asynchronous invocation: Function Compute queues the event request and returns a response immediately, instead of waiting for the request to complete.

      • If a function is time-consuming, resource-intensive, or contains error-prone logic, you can use asynchronous invocation to improve response speed and reliably handle traffic spikes.

      • For Function Compute tasks that run for more than one hour, use asynchronous invocation.

    Variable

    Parameters for invoking the function. For example:

    {
        "msg": "This is a test message from dataworks!"
    }
  8. Optional: Debug the Function Compute node. After configuring the node, you can debug it by clicking the运行 icon, specifying the resource group for the task, and assigning constant values to variables.

  9. Configure scheduling properties for the node. You can use DataWorks scheduling parameters for dynamic parameter passing in scheduled tasks. For more information about how to configure scheduling parameters, see Supported formats of scheduling parameters. For more information about scheduling properties, see Overview of task scheduling configuration.

Step 3: Commit and deploy the node

To run a Function Compute node on an automatic schedule, you must first commit and deploy it to the production environment.

  1. Save and commit the node.

    Click the 保存 and 提交 icons in the toolbar to save and commit the node. In the commit dialog box, enter a change description and, if required, select the options for code review and smoke testing.

    Note
    • You must configure the Rerun attribute property and the Parent Nodes in the scheduling properties before you can commit the node.

    • If code review is enabled, the code of a submitted node must be approved by a reviewer before the node can be deployed. For more information, see Code review.

    • To ensure that the scheduled node runs as expected, we recommend that you perform smoke testing on the task before you deploy it. For more information, see Smoke testing.

  2. Optional. Deploy the node.

    If you are using a workspace in standard mode, you must click Deploy in the upper-right corner to deploy the node after you commit it. For more information, see Workspaces in standard mode and Deploy tasks.

Next steps

Related documents

Function Compute node