Skip to content

Commit

Permalink
feat: 🎸 support event function
Browse files Browse the repository at this point in the history
  • Loading branch information
heimanba committed Sep 17, 2023
1 parent ad57b88 commit 73f55db
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.s

.idea
venv
package-lock.json
2 changes: 1 addition & 1 deletion publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Type: Plugin
Name: keep-warm-fc
Provider:
- 阿里云
Version: 0.0.7
Version: 0.0.8
Description: 定时触发器预热函数,有效降低冷启动的概率
HomePage: https://github.com/devsapp/keep-warm-fc
Tags: #标签详情
Expand Down
41 changes: 37 additions & 4 deletions src/helper/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,43 @@

import requests
import os
import fc2

fc_client_timeout = 3


def build_fc_client(fc_context):
credentials = fc_context.credentials

region_id = fc_context.region
account_id = fc_context.account_id
access_key_id = credentials.accessKeyId
access_key_secret = credentials.accessKeySecret
sts_token = credentials.securityToken

endpoint = "https://{0}.{1}.fc.aliyuncs.com".format(account_id, region_id)

if sts_token:
return fc2.Client(endpoint=endpoint,
accessKeyID=access_key_id,
accessKeySecret=access_key_secret,
securityToken=sts_token,
Timeout=fc_client_timeout)
return fc2.Client(
endpoint=endpoint,
accessKeyID=access_key_id,
accessKeySecret=access_key_secret,
Timeout=fc_client_timeout)


def handler(event, context):
url = os.environ['KEEP_WARM_FC_URL']
method = os.environ['KEEP_WARM_FC_METHOD']
res = requests.request(method, url)
print(res.status_code)
if os.environ.get('KEEP_WARM_FC_URL') is not None:
url = os.environ['KEEP_WARM_FC_URL']
method = os.environ['KEEP_WARM_FC_METHOD'] or 'head'
res = requests.request(method, url)
print(res.status_code)
else:
client = build_fc_client(context)
client.invoke_function(context.service.name, os.environ['FUNCTION_NAME'],
payload='{"action":"heartbeat"}')
print(200)
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = async function index(inputs, args = {}) {
description: `当前定时函数由Serverless Devs自动创建,用于定时预热函数【${lodash.get(serviceFunction, 'name')}】`,
codeUri: path.join(__dirname, "helper"),
runtime: "python3",
timeout: 1,
timeout: 3,
memorySize: 128,
instanceConcurrency: 10,
environmentVariables: {
Expand Down

0 comments on commit 73f55db

Please sign in to comment.