Skip to content

Add oci queue repo by mrinne #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions application-development/cloud-native/oci-queue/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
122 changes: 122 additions & 0 deletions application-development/cloud-native/oci-queue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<!--
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.
-->

## OCI Queue example in NodeJS with GitHub Actions

### OCI Queue

Create Queue and copy the Queue OCID and Enpoint:

<p>
<img src="endpoint.png" width="800" />

<p>
When using another <code>region</code> than <code>EU_FRANKFURT_1</code> please modify the
<a href="https://github.com/mikarinneoracle/oci-queue-node/blob/main/index.js#L10">
<code>index.js</code></a> Line #10 accordingly:

<pre>
const region = common.Region.EU_FRANKFURT_1;
</pre>

### Policies

Setup policies for your user in the tenancy

e.g. <code>Allow &lt;USER_GROUP&gt; to manage queues in compartment &lt;COMPARTMENT&gt;</code>

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:

<ul>
<li><b>OCI_TENANCY</b>: OCI Tenancy OCID</li>
<li><b>OCI_USER</b>: OCI User OCID</li>
<li><b>OCI_FINGERPRINT</b>: OCI User Fingerprint</li>
<li><b>OCI_KEY</b>: OCI User Private Key</li>
<li><b>OCI_PASSPHRASE</b>: OCI User Private Key passphrase (optional)</li>
<li><b>Q_ID</b>: OCI Queue OCID</li>
<li><b>Q_ENDPOINT</b>OCI Queue Endpoint</li>
</ul>

### Run Action

Monitor the GitHub action to run:

<p>
<img src="action.png" width="800" />

<p>
This example will poll for messages in the queue and finally writes a new message to it.

### View messages in the Queue

<img src="messages.png" width="800" />

### Running locally

Clone this repo, setup npm and <code>oci cli</code> and modify <a href="https://github.com/mikarinneoracle/oci-queue-node/blob/main/index.js#L8">index.js</a> line 8 by uncommenting it and removing/commenting lines 10-18 and replace lines 21-22
with Queue details :

<pre>
// 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';
</pre>

Then run:

<pre>
npm install
node index.js
</pre>

### Sending messages to Queue

You can manually send messages to the queue using the OCI Queue Console
<code>Actions/Send Message</code> and then see them being received by re-running the build.

<p>
You can also play with the queue's <code>Dead Letter Queue</code> settings to see how many
times the same message is being received (default is 5). To do this comment the
<a href="index.js#L61">line 61 of the index.js</a> and re-run the build using commit.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
123 changes: 123 additions & 0 deletions application-development/cloud-native/oci-queue/index.js
Original file line number Diff line number Diff line change
@@ -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;
}
}) ();
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions application-development/cloud-native/oci-queue/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}