
يستمر هذا المقال في سلسلة من الملاحظات المترجمة حول OpenWhisk التي كتبها Priti Desai . سننظر اليوم في أمثلة لنشر GitHub webhook والإشارات الدورية ، وكذلك أبسط تطبيق لإرسال الرسائل إلى Slack.
جيثب
يمكن تشغيل وظائف OpenWhisk من خلال العديد من الأحداث (على سبيل المثال ، طلب الدفع أو السحب أو ما إلى ذلك) من مستودع GitHub .
دعونا نرى كيف يمكنك نشر وظيفة للتعامل مع Webhook GitHub باستخدام wskdeploy .
الخطوة الأولى
قم بإنشاء ملف بيان:
للتعامل مع الأحداث مع GitHub ، تحتاج إلى تحديد مصدر الحدث / /whisk.system/github/webhook
كشرط /whisk.system/github/webhook
:
packages: helloworld: actions: helloworld: location: src/hello.js runtime: nodejs:6 inputs: name: type: string description: name of a person place: type: string description: location of a person outputs: payload: type: string description: a simple greeting message, Hello World! triggers: GitHubWebhookTrigger: feed: /whisk.system/github/webhook rules: helloworldOnWebhook: action: helloworld trigger: GitHubWebhookTrigger
الخطوة الثانية
نقوم بإنشاء ملف للنشر ، حيث نقوم بتعيين قيم username
accessToken
الإدخال repository
accessToken
والوصول إلى events
المعالجة الشرطية لمصدر الحدث GitHubWebhookTrigger :
application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: inputs: name: Amy place: Paris triggers: GitHubWebhookTrigger: inputs: username: pritidesai repository: pritidesai/helloworld accessToken: <accessToken> events: push
الخطوة الثالثة
توسيع الوظيفة:
./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld bindings: * action: helloworld bindings: - name: name value: Amy - name: place value: Paris Triggers: * trigger: GitHubWebhookTrigger bindings: - name: accessToken value: **** - name: events value: push - name: username value: pritidesai - name: repository value: pritidesai/helloworld annotations: - name: feed value: /whisk.system/github/webhook Rules * rule: helloworldOnWebhook - trigger: GitHubWebhookTrigger - action: helloworld Do you really want to deploy this? (y/N): y Deploying package helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deploying trigger feed GitHubWebhookTrigger ... Done! Deploying rule helloworldOnWebhook ... Done! Deployment completed successfully.
لقد نشرنا وظيفة hello world ، التي تم استدعاؤها من GitHub في كل مرة نقوم فيها بتحديث الرمز في مستودع GitHub ، واستخدمنا webhook لهذا:

يمكننا التحقق من الوظيفة المنشورة عن طريق إنشاء تغييرات التعليمات البرمجية الجديدة في مستودع جيثب. في هذه الحالة ، سيتم إرجاع رسائل ترحيب بسيطة. دعونا نحاول استخدام الحمولة النافعة عند تحديث الكود المرسل من جيثب عبر طلب POST. تتوفر هذه البيانات كمعلمات دالة ، على سبيل المثال:
function main(params) { console.log("GitHub repository is at ", params.repository.url); return {commits: params.commits}; }
فيما يلي مقتطف من الحمولة النافعة التي تم الحصول عليها عن طريق التحرير العادي لـ README.md:
"commits" : [ { "author" : { "name" : Priti Desai, "username" : pritidesai }, "timestamp" : 2017-03-20T12:54:41-07:00, "removed" : [ ], "modified" : [ README.md ], "added" : [ ], "message" : Update README.md, "committer" : { "name" : GitHub, "email" : noreply@github.com, "username" : web-flow } } ],
يمكنك معرفة وصف مفصل لكيفية نيران جيثب حرائق الويب هنا.
إشارات
يمكن تشغيل وظائف OpenWhisk بشكل دوري بواسطة إشارة داخلية (مثل مهام cron). دعنا نحاول إضافة مشغل شرطي للإشارة من خلال wskdeploy
.
الخطوة الأولى
لمعالجة الإشارات ، تحتاج إلى تحديد مصدر الحدث / /whisk.system/alarms/alarm
كشرط /whisk.system/alarms/alarm
في البيان:
packages: helloworld: actions: helloworld: location: src/hello.js runtime: nodejs:6 inputs: name: type: string description: name of a person place: type: string description: location of a person outputs: payload: type: string description: a simple greeting message, Hello World! triggers: Every12Hours: feed: /whisk.system/alarms/alarm rules: hellowroldOnCron: action: helloworld trigger: Every12Hours
الخطوة الثانية
قم بإنشاء ملف للنشر ، وحدد cron كشرط إدخال ، بقيمة Every12Hours . يمكنك أيضًا استخدام كلا الخيارين المستخدم في cron ، التقليدي والمحسن:
<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <Year>
محتويات ملف Publishing.yaml:
application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: inputs: name: Amy place: Paris triggers: Every12Hours: inputs: cron: "0 */12 * * *"
من الممكن تحديد الحمولة النافعة المرسلة عندما يتم تشغيل الإشارة (يتم إرسالها كمعلمة دالة في كل مرة يتم تشغيلها) كمعلمة trigger_payload :
application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: inputs: name: Amy place: Paris triggers: Every12Hours: inputs: cron: "0 */12 * * * *" trigger_payload: "{\"name\":\"Mark\", \"place\":\"Barcelona\"}"
الخطوة الثالثة
توسيع الوظيفة:
./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld bindings: * action: helloworld bindings: - name: name value: Amy - name: place value: Paris Triggers: * trigger: Every12Hours bindings: - name: cron value: 0 */12 * * * * annotations: - name: feed value: /whisk.system/alarms/alarm Rules * rule: helloworldEvery12Hours - trigger: Every12Hours - action: helloworld Do you really want to deploy this? (y/N): y Deploying package helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deploying trigger feed Every12Hours ... Done! Deploying rule helloworldEvery12Hours ... Done! Deployment completed successfully.
من الآن فصاعدًا ، لدى ac وظيفة hello world التي تعمل كل 12 ساعة. يمكننا التحقق من ذلك عن طريق تفعيل الشرط ، كما هو موضح سابقا
حزمة لسلاك
لإرسال رسائل إلى Slack ، يتم تقديم حزمة لـ Slack . دعونا ننظر في حزمة ملزمة باستخدام wskdeploy
.
الخطوة الأولى
إنشاء ملف بيان مع حزمة ملزمة لسلاك. للقيام بذلك ، /whisk.system/slack
تبعية ، /whisk.system/slack
قيمة الموقع على / /whisk.system/slack
/ /whisk.system/slack
:
packages: SlackPackage: dependencies: slack-package-to-post-messages: location: /whisk.system/slack inputs: username: $SLACK_USERNAME url: $SLACK_URL channel: $SLACK_CHANNEL
الرسائل: packages: SlackPackage: dependencies: slack-package-to-post-messages: location: /whisk.system/slack inputs: username: $SLACK_USERNAME url: $SLACK_URL channel: $SLACK_CHANNEL
بادئ ذي بدء ، نحن بحاجة إلى تكوين دعم لعربة الويب الواردة في بيئة عمل Slack الخاصة بنا. يمكنك تكوين خط بحث إلكتروني جديد لإرسال رسائل إلى قناة سلاك باستخدام تعليمات خطوة بخطوة .
نضيف الآن حزمة Slack إلى تطبيقنا لإرسال رسائل إلى قناة Slack كل ساعة باستخدام هذا البيان:
packages: SlackPackage: dependencies: slack-package-to-post-messages: location: /whisk.system/slack inputs: username: $SLACK_USERNAME url: $SLACK_URL channel: $SLACK_CHANNEL actions: post-to-slack: function: actions/post-to-slack.js runtime: nodejs:6 inputs: message: type: string description: message to post on slack slack_package: type: string description: slack package name triggers: everyhour: feed: /whisk.system/alarms/alarm rules: post-to-slack-every-hour: action: post-to-slack trigger: everyhour
الرسائل: packages: SlackPackage: dependencies: slack-package-to-post-messages: location: /whisk.system/slack inputs: username: $SLACK_USERNAME url: $SLACK_URL channel: $SLACK_CHANNEL actions: post-to-slack: function: actions/post-to-slack.js runtime: nodejs:6 inputs: message: type: string description: message to post on slack slack_package: type: string description: slack package name triggers: everyhour: feed: /whisk.system/alarms/alarm rules: post-to-slack-every-hour: action: post-to-slack trigger: everyhour
ساعة: packages: SlackPackage: dependencies: slack-package-to-post-messages: location: /whisk.system/slack inputs: username: $SLACK_USERNAME url: $SLACK_URL channel: $SLACK_CHANNEL actions: post-to-slack: function: actions/post-to-slack.js runtime: nodejs:6 inputs: message: type: string description: message to post on slack slack_package: type: string description: slack package name triggers: everyhour: feed: /whisk.system/alarms/alarm rules: post-to-slack-every-hour: action: post-to-slack trigger: everyhour
يمكن الاطلاع على محتويات وظيفة post-to-slack.js هنا .
الخطوة الثانية
قم بإنشاء ملف للنشر:
application: name: AppToPostToSlack packages: SlackPackage: actions: post-to-slack: inputs: message: "Hello from WskDeploy!" slack_package: slack-package-to-post-messages triggers: everyhour: inputs: cron: "0 */1 * * *"
الخطوة الثالثة
توسيع الوظيفة:
./wskdeploy -p ~/AppToPostToSlack/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \ __\/ |_| Packages: Name: SlackPackage bindings: * dependency: slack-package-to-post-messages location: /whisk.system/slack * action: post-to-slack bindings: - message : "Hello from WskDeploy!" - slack_package : "slack-package-to-post-messages" annotations: Triggers: * trigger: everyhour bindings: - cron : "0 */1 * * *" annotations: - name: feed value: /whisk.system/alarms/alarm Rules * rule: post-to-slack-every-hour - trigger: everyhour - action: SlackPackage/post-to-slack Do you really want to deploy this? (y/N): y Deployment completed successfully.
في هذه الخطوة ، لدينا بالفعل وظيفة ما بعد التباطؤ تعمل مرة واحدة في الساعة. يمكنك التحقق من ذلك عن طريق تفعيل الشرط ، كما هو موضح سابقا . بعد التنشيط ، يجب أن نتلقى رسالة جديدة على قناة سلاك:
Activation: post-to-slack (9909dd5229e84526bff9902a2cd860df) [ "2017-09-12T23:05:17.17872899Z stdout: Hello from WskDeploy!", "2017-09-12T23:05:17.549177677Z stdout: Posted message to slack" ]
مقالات دورة أخرى
الحوسبة بدون خادم على أساس OpenWhisk ، الجزء 1
الحوسبة بدون خادم على أساس OpenWhisk ، الجزء 2
الحوسبة بدون خادم على أساس OpenWhisk ، الجزء 3
الحوسبة بدون خادم على أساس OpenWhisk ، الجزء 4