UNPKG

5.64 kBMarkdownView Raw
1# LSK.js – rabbit
2
3> @lskjs/rabbit – LSK module for rabbit.
4
5[![LSK logo](https://badgen.net/badge/icon/MADE%20BY%20LSK?icon=zeit\&label\&color=red\&labelColor=red)](https://github.com/lskjs)
6[![NPM version](https://badgen.net/npm/v/@lskjs/rabbit)](https://www.npmjs.com/package/@lskjs/rabbit)
7[![NPM downloads](https://badgen.net/npm/dt/@lskjs/rabbit)](https://www.npmjs.com/package/@lskjs/rabbit)
8[![NPM Dependency count](https://badgen.net/bundlephobia/dependency-count/@lskjs/rabbit)](https://bundlephobia.com/result?p=@lskjs/rabbit)
9[![Have TypeScript types](https://badgen.net/npm/types/@lskjs/rabbit)](https://www.npmjs.com/package/@lskjs/rabbit)
10[![Have tree shaking](https://badgen.net/bundlephobia/tree-shaking/@lskjs/rabbit)](https://bundlephobia.com/result?p=@lskjs/rabbit)
11[![NPM Package size](https://badgen.net/bundlephobia/minzip/@lskjs/rabbit)](https://bundlephobia.com/result?p=@lskjs/rabbit)
12[![Package size](https://badgen.net//github/license/lskjs/lskjs)](https://github.com/lskjs/lskjs/blob/master/LICENSE)
13[![Ask us in Telegram](https://img.shields.io/badge/Ask%20us%20in-Telegram-brightblue.svg)](https://t.me/lskjschat)
14
15<!-- template file="scripts/templates/preview.md" start -->
16
17<!-- template end -->
18
19***
20
21<!-- # 📒 Table of contents -->
22
23# Table of contents
24
25* [⌨️ Install](#️-install)
26* [📖 License](#-license)
27* [👥 Contributors](#-contributors)
28* [👏 Contributing](#-contributing)
29* [📮 Any questions? Always welcome :)](#-any-questions-always-welcome-)
30
31# ⌨️ Install
32
33```sh
34# yarn
35yarn i @lskjs/rabbit axios bluebird lodash
36
37# npm
38npm i @lskjs/rabbit axios bluebird lodash
39```
40
41***
42
43```js
44async startDynamicPrefetch() {
45 const { rabbit } = this;
46 const { messageCount } = await rabbit.assertQueue(this.queue);
47 if (messageCount > 10000) {
48 rabbit.listenChannel.prefetch(100);
49 } else {
50 rabbit.listenChannel.prefetch(10);
51 }
52 setTimeout(() => this.startDynamicPrefetch(), 1000);
53}
54async run() {
55 await super.run();
56 this.startDynamicPrefetch();
57}
58```
59
60```js
61const rabbit = await this.module('rabbit');
62await rabbit.assertExchange('test', 'headers'); // создание exchange
63await rabbit.bindQueue('test_ch', 'test', '', { type: 'ch' }); // присоединение queue к exchange
64await rabbit.publish('test', '', { _id: 1 }, { headers: { type: 'ch' } }); // отправка сообщения
65```
66
67```js
68await rabbit.bindQueue('test_es_ch', 'test', '', { es: true, ch: true }); // пример с несколькими headers
69await rabbit.publish('test', '', { _id: 1 }, { headers: { es: true, ch: true } });
70```
71
72 в конфиге есть поле queueOptions - это глобальные options для всех задач которые пишутся в rabbit
73
74```json
75"rabbit": {
76 "uri": "localhost:15672",
77 "queueOptions": {
78 "persistent": true,
79 "expiration": 683576835
80 }
81}
82```
83
84 в конфиге с очередями(queues.js) можно указать options для каждой очереди отдельно
85
86```json
87 queue1: {
88 queue: 'queue1',
89 options: {
90 persistent: true,
91 headers: {},
92 priority: 5,
93 replyTo: 'test',
94 },
95 },
96 queue2: {
97 queue: 'queue2',
98 limit: million,
99 options: {
100 persistent: true,
101 headers: {
102 custom: 'header',
103 },
104 priority: 5,
105 replyTo: 'test2',
106 },
107 },
108 queue3: {
109 queue: 'queue3',
110 options: {
111 persistent: true,
112 headers: {},
113 priority: 5,
114 replyTo: 'test3',
115 },
116 },
117 queue4: {
118 queue: 'queue4',
119 options: {
120 persistent: true, headers: {}, priority: 5, replyTo: 'test4',
121 },
122 },
123```
124
125 options можно доопределить с помощью 3 аргумента при постановки задачи
126
127```js
128await rabbit.sendToQueue('test', { _id: 1 }, { persistent: true }); // отправка сообщения с options
129```
130
131 options для задачи генерируются из
132
133 1) options которые пробросили при постановки задачи
134 2) из конфига queues.js для конкретной очереди
135 3) из глобального конфига
136
137 3 этих объекта мерджутся между собой именно в таком порядке
138
139# 📖 License
140
141This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
142
143# 👥 Contributors
144
145<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
146
147<!-- prettier-ignore-start -->
148
149<!-- markdownlint-disable -->
150
151<table>
152 <tr>
153 <td align="center"><a href="https://isuvorov.com"><img src="https://avatars2.githubusercontent.com/u/1056977?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Igor Suvorov</b></sub></a><br /><a href="lskjs/lskjs///commits?author=isuvorov" title="Code">💻</a> <a href="#design-isuvorov" title="Design">🎨</a> <a href="#ideas-isuvorov" title="Ideas, Planning, & Feedback">🤔</a></td>
154 </tr>
155</table>
156<!-- markdownlint-restore -->
157<!-- prettier-ignore-end -->
158<!-- ALL-CONTRIBUTORS-LIST:END -->
159
160# 👏 Contributing
161
1621. Fork it (<https://github.com/yourname/yourproject/fork>)
1632. Create your feature branch (`git checkout -b features/fooBar`)
1643. Commit your changes (`git commit -am 'feat(image): Add some fooBar'`)
1654. Push to the branch (`git push origin feature/fooBar`)
1665. Create a new Pull Request
167
168# 📮 Any questions? Always welcome :)
169
170* [Email](mailto:hi@isuvorov.com)
171* [LSK.news – Telegram channel](https://t.me/lskjs)
172* [Спроси нас в телеграме ;)](https://t.me/lskjschat)