Skip to content

Commit 1568924

Browse files
authored
Merge pull request #39 from oracle-devrel/add-oci-queue-repo-mrinne
Add oci queue repo by mrinne
2 parents 49a0b59 + ac8d5e8 commit 1568924

File tree

7 files changed

+262
-0
lines changed

7 files changed

+262
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!--
2+
Copyright (c) 2021 Oracle and/or its affiliates.
3+
4+
The Universal Permissive License (UPL), Version 1.0
5+
6+
Subject to the condition set forth below, permission is hereby granted to any
7+
person obtaining a copy of this software, associated documentation and/or data
8+
(collectively the "Software"), free of charge and under any and all copyright
9+
rights in the Software, and any and all patent rights owned or freely
10+
licensable by each licensor hereunder covering either (i) the unmodified
11+
Software as contributed to or provided by such licensor, or (ii) the Larger
12+
Works (as defined below), to deal in both
13+
14+
(a) the Software, and
15+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
one is included with the Software (each a "Larger Work" to which the Software
17+
is contributed by such licensors),
18+
19+
without restriction, including without limitation the rights to copy, create
20+
derivative works of, display, perform, and distribute the Software and make,
21+
use, sell, offer for sale, import, export, have made, and have sold the
22+
Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
either these or other terms.
24+
25+
This license is subject to the following condition:
26+
The above copyright notice and either this complete permission notice or at
27+
a minimum a reference to the UPL must be included in all copies or
28+
substantial portions of the Software.
29+
30+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
SOFTWARE.
37+
-->
38+
39+
## OCI Queue example in NodeJS with GitHub Actions
40+
41+
### OCI Queue
42+
43+
Create Queue and copy the Queue OCID and Enpoint:
44+
45+
<p>
46+
<img src="endpoint.png" width="800" />
47+
48+
<p>
49+
When using another <code>region</code> than <code>EU_FRANKFURT_1</code> please modify the
50+
<a href="https://github.com/mikarinneoracle/oci-queue-node/blob/main/index.js#L10">
51+
<code>index.js</code></a> Line #10 accordingly:
52+
53+
<pre>
54+
const region = common.Region.EU_FRANKFURT_1;
55+
</pre>
56+
57+
### Policies
58+
59+
Setup policies for your user in the tenancy
60+
61+
e.g. <code>Allow &lt;USER_GROUP&gt; to manage queues in compartment &lt;COMPARTMENT&gt;</code>
62+
63+
More on OCI Queue IAM policies: https://docs.oracle.com/en-us/iaas/Content/queue/policy-reference.htm
64+
65+
### Secrets
66+
67+
Setup secrets to run this example with GitHub Actions:
68+
69+
<ul>
70+
<li><b>OCI_TENANCY</b>: OCI Tenancy OCID</li>
71+
<li><b>OCI_USER</b>: OCI User OCID</li>
72+
<li><b>OCI_FINGERPRINT</b>: OCI User Fingerprint</li>
73+
<li><b>OCI_KEY</b>: OCI User Private Key</li>
74+
<li><b>OCI_PASSPHRASE</b>: OCI User Private Key passphrase (optional)</li>
75+
<li><b>Q_ID</b>: OCI Queue OCID</li>
76+
<li><b>Q_ENDPOINT</b>OCI Queue Endpoint</li>
77+
</ul>
78+
79+
### Run Action
80+
81+
Monitor the GitHub action to run:
82+
83+
<p>
84+
<img src="action.png" width="800" />
85+
86+
<p>
87+
This example will poll for messages in the queue and finally writes a new message to it.
88+
89+
### View messages in the Queue
90+
91+
<img src="messages.png" width="800" />
92+
93+
### Running locally
94+
95+
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
96+
with Queue details :
97+
98+
<pre>
99+
// Use this locally instead of env vars and region:
100+
const provider = new common.ConfigFileAuthenticationDetailsProvider();
101+
102+
// Q settings
103+
const queueId = 'ocid1.queue.oc1.eu-frankfurt-1.ama....a5z4ic2tslq';
104+
const endpoint = 'https://cell-1.queue......oci.oraclecloud.com';
105+
</pre>
106+
107+
Then run:
108+
109+
<pre>
110+
npm install
111+
node index.js
112+
</pre>
113+
114+
### Sending messages to Queue
115+
116+
You can manually send messages to the queue using the OCI Queue Console
117+
<code>Actions/Send Message</code> and then see them being received by re-running the build.
118+
119+
<p>
120+
You can also play with the queue's <code>Dead Letter Queue</code> settings to see how many
121+
times the same message is being received (default is 5). To do this comment the
122+
<a href="index.js#L61">line 61 of the index.js</a> and re-run the build using commit.
375 KB
Loading
1.03 MB
Loading
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
Copyright (c) 2021 Oracle and/or its affiliates.
3+
4+
The Universal Permissive License (UPL), Version 1.0
5+
6+
Subject to the condition set forth below, permission is hereby granted to any
7+
person obtaining a copy of this software, associated documentation and/or data
8+
(collectively the "Software"), free of charge and under any and all copyright
9+
rights in the Software, and any and all patent rights owned or freely
10+
licensable by each licensor hereunder covering either (i) the unmodified
11+
Software as contributed to or provided by such licensor, or (ii) the Larger
12+
Works (as defined below), to deal in both
13+
14+
(a) the Software, and
15+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
one is included with the Software (each a "Larger Work" to which the Software
17+
is contributed by such licensors),
18+
19+
without restriction, including without limitation the rights to copy, create
20+
derivative works of, display, perform, and distribute the Software and make,
21+
use, sell, offer for sale, import, export, have made, and have sold the
22+
Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
either these or other terms.
24+
25+
This license is subject to the following condition:
26+
The above copyright notice and either this complete permission notice or at
27+
a minimum a reference to the UPL must be included in all copies or
28+
substantial portions of the Software.
29+
30+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
SOFTWARE.
37+
*/
38+
39+
const queue = require("oci-queue");
40+
const core = require("oci-core");
41+
const identity = require("oci-identity");
42+
const common = require("oci-common");
43+
const os = require("oci-objectstorage");
44+
45+
// Use this locally instead of env vars and region:
46+
//const provider = new common.ConfigFileAuthenticationDetailsProvider();
47+
48+
const region = common.Region.EU_FRANKFURT_1;
49+
const provider = new common.SimpleAuthenticationDetailsProvider(
50+
process.env.OCI_TENANCY,
51+
process.env.OCI_USER,
52+
process.env.OCI_FINGERPRINT,
53+
process.env.OCI_KEY,
54+
process.env.OCI_PASSPHRASE ? process.env.OCI_PASSPHRASE : '',
55+
region
56+
);
57+
58+
// Q settings
59+
const queueId = process.env.Q_ID;
60+
const endpoint = process.env.Q_ENDPOINT;
61+
62+
(async () => {
63+
var res = "";
64+
try {
65+
66+
const statsReq = {
67+
queueId: queueId
68+
};
69+
70+
const getReq = {
71+
queueId: queueId,
72+
timeoutInSeconds: 2
73+
};
74+
75+
const client = new queue.QueueClient({
76+
authenticationDetailsProvider: provider
77+
});
78+
79+
client.endpoint = endpoint;
80+
81+
console.log("Getting Queue stats .. ");
82+
var statsRes = await client.getStats(statsReq).catch(error => {
83+
console.log(error);
84+
});
85+
console.log(statsRes);
86+
87+
console.log("Polling .. ");
88+
var getRes = await client.getMessages(getReq).catch(error => {
89+
console.log(error);
90+
});
91+
while(getRes && getRes.getMessages && getRes.getMessages.messages.length)
92+
{
93+
getRes.getMessages.messages.forEach(function(msg) {
94+
console.log(msg);
95+
var delReq = {
96+
queueId: queueId,
97+
messageReceipt: msg.receipt
98+
};
99+
client.deleteMessage(delReq);
100+
});
101+
console.log("Polling .. ");
102+
getRes = await client.getMessages(getReq).catch(error => {
103+
console.log(error);
104+
});
105+
}
106+
107+
const d = new Date();
108+
console.log("Writing .. ");
109+
const putReq = {
110+
queueId: queueId,
111+
putMessagesDetails: { messages : [ { content: 'hello @ ' + d } ] }
112+
};
113+
114+
const putRes = await client.putMessages(putReq);
115+
console.log(putRes);
116+
117+
} catch (error) {
118+
console.log("Error: " + error);
119+
res = "error";
120+
} finally {
121+
return res;
122+
}
123+
}) ();
418 KB
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "oci-queue",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"dependencies": {
10+
"oci-common": "^2.52.0",
11+
"oci-core": "^2.52.0",
12+
"oci-identity": "^2.52.0",
13+
"oci-objectstorage": "^2.52.0",
14+
"oci-queue": "^2.52.0"
15+
}
16+
}

0 commit comments

Comments
 (0)