diff --git a/application-development/cloud-native/oci-queue/.gitignore b/application-development/cloud-native/oci-queue/.gitignore new file mode 100644 index 000000000..c2658d7d1 --- /dev/null +++ b/application-development/cloud-native/oci-queue/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/application-development/cloud-native/oci-queue/README.md b/application-development/cloud-native/oci-queue/README.md new file mode 100644 index 000000000..714aad00f --- /dev/null +++ b/application-development/cloud-native/oci-queue/README.md @@ -0,0 +1,122 @@ + + +## OCI Queue example in NodeJS with GitHub Actions + +### OCI Queue + +Create Queue and copy the Queue OCID and Enpoint: + +

+ + +

+When using another region than EU_FRANKFURT_1 please modify the + +index.js Line #10 accordingly: + +

+const region = common.Region.EU_FRANKFURT_1;
+
+ +### Policies + +Setup policies for your user in the tenancy + +e.g. Allow <USER_GROUP> to manage queues in compartment <COMPARTMENT> + +More on OCI Queue IAM policies: https://docs.oracle.com/en-us/iaas/Content/queue/policy-reference.htm + +### Secrets + +Setup secrets to run this example with GitHub Actions: + + + +### Run Action + +Monitor the GitHub action to run: + +

+ + +

+This example will poll for messages in the queue and finally writes a new message to it. + +### View messages in the Queue + + + +### Running locally + +Clone this repo, setup npm and oci cli and modify index.js line 8 by uncommenting it and removing/commenting lines 10-18 and replace lines 21-22 +with Queue details : + +

+// Use this locally instead of env vars and region:
+const provider = new common.ConfigFileAuthenticationDetailsProvider();
+
+// Q settings
+const queueId = 'ocid1.queue.oc1.eu-frankfurt-1.ama....a5z4ic2tslq';
+const endpoint = 'https://cell-1.queue......oci.oraclecloud.com';
+
+ +Then run: + +
+npm install
+node index.js
+
+ +### Sending messages to Queue + +You can manually send messages to the queue using the OCI Queue Console +Actions/Send Message and then see them being received by re-running the build. + +

+You can also play with the queue's Dead Letter Queue settings to see how many +times the same message is being received (default is 5). To do this comment the +line 61 of the index.js and re-run the build using commit. diff --git a/application-development/cloud-native/oci-queue/action.png b/application-development/cloud-native/oci-queue/action.png new file mode 100644 index 000000000..c06ba1b4f Binary files /dev/null and b/application-development/cloud-native/oci-queue/action.png differ diff --git a/application-development/cloud-native/oci-queue/endpoint.png b/application-development/cloud-native/oci-queue/endpoint.png new file mode 100644 index 000000000..2acd508c5 Binary files /dev/null and b/application-development/cloud-native/oci-queue/endpoint.png differ diff --git a/application-development/cloud-native/oci-queue/index.js b/application-development/cloud-native/oci-queue/index.js new file mode 100644 index 000000000..d5e77bab8 --- /dev/null +++ b/application-development/cloud-native/oci-queue/index.js @@ -0,0 +1,123 @@ +/* +Copyright (c) 2021 Oracle and/or its affiliates. + +The Universal Permissive License (UPL), Version 1.0 + +Subject to the condition set forth below, permission is hereby granted to any +person obtaining a copy of this software, associated documentation and/or data +(collectively the "Software"), free of charge and under any and all copyright +rights in the Software, and any and all patent rights owned or freely +licensable by each licensor hereunder covering either (i) the unmodified +Software as contributed to or provided by such licensor, or (ii) the Larger +Works (as defined below), to deal in both + +(a) the Software, and +(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if +one is included with the Software (each a "Larger Work" to which the Software +is contributed by such licensors), + +without restriction, including without limitation the rights to copy, create +derivative works of, display, perform, and distribute the Software and make, +use, sell, offer for sale, import, export, have made, and have sold the +Software and the Larger Work(s), and to sublicense the foregoing rights on +either these or other terms. + +This license is subject to the following condition: +The above copyright notice and either this complete permission notice or at +a minimum a reference to the UPL must be included in all copies or +substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +const queue = require("oci-queue"); +const core = require("oci-core"); +const identity = require("oci-identity"); +const common = require("oci-common"); +const os = require("oci-objectstorage"); + +// Use this locally instead of env vars and region: +//const provider = new common.ConfigFileAuthenticationDetailsProvider(); + +const region = common.Region.EU_FRANKFURT_1; +const provider = new common.SimpleAuthenticationDetailsProvider( + process.env.OCI_TENANCY, + process.env.OCI_USER, + process.env.OCI_FINGERPRINT, + process.env.OCI_KEY, + process.env.OCI_PASSPHRASE ? process.env.OCI_PASSPHRASE : '', + region +); + +// Q settings +const queueId = process.env.Q_ID; +const endpoint = process.env.Q_ENDPOINT; + +(async () => { + var res = ""; + try { + + const statsReq = { + queueId: queueId + }; + + const getReq = { + queueId: queueId, + timeoutInSeconds: 2 + }; + + const client = new queue.QueueClient({ + authenticationDetailsProvider: provider + }); + + client.endpoint = endpoint; + + console.log("Getting Queue stats .. "); + var statsRes = await client.getStats(statsReq).catch(error => { + console.log(error); + }); + console.log(statsRes); + + console.log("Polling .. "); + var getRes = await client.getMessages(getReq).catch(error => { + console.log(error); + }); + while(getRes && getRes.getMessages && getRes.getMessages.messages.length) + { + getRes.getMessages.messages.forEach(function(msg) { + console.log(msg); + var delReq = { + queueId: queueId, + messageReceipt: msg.receipt + }; + client.deleteMessage(delReq); + }); + console.log("Polling .. "); + getRes = await client.getMessages(getReq).catch(error => { + console.log(error); + }); + } + + const d = new Date(); + console.log("Writing .. "); + const putReq = { + queueId: queueId, + putMessagesDetails: { messages : [ { content: 'hello @ ' + d } ] } + }; + + const putRes = await client.putMessages(putReq); + console.log(putRes); + + } catch (error) { + console.log("Error: " + error); + res = "error"; + } finally { + return res; + } +}) (); \ No newline at end of file diff --git a/application-development/cloud-native/oci-queue/messages.png b/application-development/cloud-native/oci-queue/messages.png new file mode 100644 index 000000000..ca351dfc0 Binary files /dev/null and b/application-development/cloud-native/oci-queue/messages.png differ diff --git a/application-development/cloud-native/oci-queue/package.json b/application-development/cloud-native/oci-queue/package.json new file mode 100644 index 000000000..1f52be6ac --- /dev/null +++ b/application-development/cloud-native/oci-queue/package.json @@ -0,0 +1,16 @@ +{ + "name": "oci-queue", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "dependencies": { + "oci-common": "^2.52.0", + "oci-core": "^2.52.0", + "oci-identity": "^2.52.0", + "oci-objectstorage": "^2.52.0", + "oci-queue": "^2.52.0" + } +}