Skip to content

Commit 2574cac

Browse files
files added
1 parent 82878db commit 2574cac

File tree

7 files changed

+188
-0
lines changed

7 files changed

+188
-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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
## OCI Queue example in NodeJS with GitHub Actions
2+
3+
### OCI Queue
4+
5+
Create Queue and copy the Queue OCID and Enpoint:
6+
7+
<p>
8+
<img src="endpoint.png" width="800" />
9+
10+
<p>
11+
When using another <code>region</code> than <code>EU_FRANKFURT_1</code> please modify the
12+
<a href="https://github.com/mikarinneoracle/oci-queue-node/blob/main/index.js#L10">
13+
<code>index.js</code></a> Line #10 accordingly:
14+
15+
<pre>
16+
const region = common.Region.EU_FRANKFURT_1;
17+
</pre>
18+
19+
### Policies
20+
21+
Setup policies for your user in the tenancy
22+
23+
e.g. <code>Allow &lt;USER_GROUP&gt; to manage queues in compartment &lt;COMPARTMENT&gt;</code>
24+
25+
More on OCI Queue IAM policies: https://docs.oracle.com/en-us/iaas/Content/queue/policy-reference.htm
26+
27+
### Secrets
28+
29+
Setup secrets to run this example with GitHub Actions:
30+
31+
<ul>
32+
<li><b>OCI_TENANCY</b>: OCI Tenancy OCID</li>
33+
<li><b>OCI_USER</b>: OCI User OCID</li>
34+
<li><b>OCI_FINGERPRINT</b>: OCI User Fingerprint</li>
35+
<li><b>OCI_KEY</b>: OCI User Private Key</li>
36+
<li><b>OCI_PASSPHRASE</b>: OCI User Private Key passphrase (optional)</li>
37+
<li><b>Q_ID</b>: OCI Queue OCID</li>
38+
<li><b>Q_ENDPOINT</b>OCI Queue Endpoint</li>
39+
</ul>
40+
41+
### Run Action
42+
43+
Monitor the GitHub action to run:
44+
45+
<p>
46+
<img src="action.png" width="800" />
47+
48+
<p>
49+
This example will poll for messages in the queue and finally writes a new message to it.
50+
51+
### View messages in the Queue
52+
53+
<img src="messages.png" width="800" />
54+
55+
### Running locally
56+
57+
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
58+
with Queue details :
59+
60+
<pre>
61+
// Use this locally instead of env vars and region:
62+
const provider = new common.ConfigFileAuthenticationDetailsProvider();
63+
64+
// Q settings
65+
const queueId = 'ocid1.queue.oc1.eu-frankfurt-1.ama....a5z4ic2tslq';
66+
const endpoint = 'https://cell-1.queue.messaging.eu-frankfurt-1.oci.oraclecloud.com';
67+
</pre>
68+
69+
Then run:
70+
71+
<pre>
72+
npm install
73+
node index.js
74+
</pre>
75+
76+
### Sending messages to Queue
77+
78+
You can manually send messages to the queue using the OCI Queue Console
79+
<code>Actions/Send Message</code> and then see them being received by re-running the build.
80+
81+
<p>
82+
You can also play with the queue's <code>Dead Letter Queue</code> settings to see how many
83+
times the same message is being received (default is 5). To do this comment the
84+
<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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const queue = require("oci-queue");
2+
const core = require("oci-core");
3+
const identity = require("oci-identity");
4+
const common = require("oci-common");
5+
const os = require("oci-objectstorage");
6+
7+
// Use this locally instead of env vars and region:
8+
//const provider = new common.ConfigFileAuthenticationDetailsProvider();
9+
10+
const region = common.Region.EU_FRANKFURT_1;
11+
const provider = new common.SimpleAuthenticationDetailsProvider(
12+
process.env.OCI_TENANCY,
13+
process.env.OCI_USER,
14+
process.env.OCI_FINGERPRINT,
15+
process.env.OCI_KEY,
16+
process.env.OCI_PASSPHRASE ? process.env.OCI_PASSPHRASE : '',
17+
region
18+
);
19+
20+
// Q settings
21+
const queueId = process.env.Q_ID;
22+
const endpoint = process.env.Q_ENDPOINT;
23+
24+
(async () => {
25+
var res = "";
26+
try {
27+
28+
const statsReq = {
29+
queueId: queueId
30+
};
31+
32+
const getReq = {
33+
queueId: queueId,
34+
timeoutInSeconds: 2
35+
};
36+
37+
const client = new queue.QueueClient({
38+
authenticationDetailsProvider: provider
39+
});
40+
41+
client.endpoint = endpoint;
42+
43+
console.log("Getting Queue stats .. ");
44+
var statsRes = await client.getStats(statsReq).catch(error => {
45+
console.log(error);
46+
});
47+
console.log(statsRes);
48+
49+
console.log("Polling .. ");
50+
var getRes = await client.getMessages(getReq).catch(error => {
51+
console.log(error);
52+
});
53+
while(getRes && getRes.getMessages && getRes.getMessages.messages.length)
54+
{
55+
getRes.getMessages.messages.forEach(function(msg) {
56+
console.log(msg);
57+
var delReq = {
58+
queueId: queueId,
59+
messageReceipt: msg.receipt
60+
};
61+
client.deleteMessage(delReq);
62+
});
63+
console.log("Polling .. ");
64+
getRes = await client.getMessages(getReq).catch(error => {
65+
console.log(error);
66+
});
67+
}
68+
69+
const d = new Date();
70+
console.log("Writing .. ");
71+
const putReq = {
72+
queueId: queueId,
73+
putMessagesDetails: { messages : [ { content: 'hello @ ' + d } ] }
74+
};
75+
76+
const putRes = await client.putMessages(putReq);
77+
console.log(putRes);
78+
79+
} catch (error) {
80+
console.log("Error: " + error);
81+
res = "error";
82+
} finally {
83+
return res;
84+
}
85+
}) ();
418 KB
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
"author": "[email protected]",
10+
"license": "ISC",
11+
"dependencies": {
12+
"oci-common": "^2.52.0",
13+
"oci-core": "^2.52.0",
14+
"oci-identity": "^2.52.0",
15+
"oci-objectstorage": "^2.52.0",
16+
"oci-queue": "^2.52.0"
17+
}
18+
}

0 commit comments

Comments
 (0)