Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.triggerFromQueue = exports.queueFromEndpoint = exports.queueNameForEndpoint = exports.setEnqueuer = exports.getIamPolicy = exports.setIamPolicy = exports.deleteQueue = exports.purgeQueue = exports.upsertQueue = exports.updateQueue = exports.getQueue = exports.createQueue = exports.DEFAULT_SETTINGS = void 0;
  4. const proto = require("./proto");
  5. const apiv2_1 = require("../apiv2");
  6. const api_1 = require("../api");
  7. const functional_1 = require("../functional");
  8. const API_VERSION = "v2";
  9. const client = new apiv2_1.Client({
  10. urlPrefix: api_1.cloudTasksOrigin,
  11. auth: true,
  12. apiVersion: API_VERSION,
  13. });
  14. exports.DEFAULT_SETTINGS = {
  15. rateLimits: {
  16. maxConcurrentDispatches: 1000,
  17. maxDispatchesPerSecond: 500,
  18. },
  19. state: "RUNNING",
  20. retryConfig: {
  21. maxDoublings: 16,
  22. maxAttempts: 3,
  23. maxBackoff: "3600s",
  24. minBackoff: "0.100s",
  25. },
  26. };
  27. async function createQueue(queue) {
  28. const path = queue.name.substring(0, queue.name.lastIndexOf("/"));
  29. const res = await client.post(path, queue);
  30. return res.body;
  31. }
  32. exports.createQueue = createQueue;
  33. async function getQueue(name) {
  34. const res = await client.get(name);
  35. return res.body;
  36. }
  37. exports.getQueue = getQueue;
  38. async function updateQueue(queue) {
  39. const res = await client.patch(queue.name, queue, {
  40. queryParams: { updateMask: proto.fieldMasks(queue).join(",") },
  41. });
  42. return res.body;
  43. }
  44. exports.updateQueue = updateQueue;
  45. async function upsertQueue(queue) {
  46. var _a, _b;
  47. try {
  48. const existing = await module.exports.getQueue(queue.name);
  49. if (JSON.stringify(queue) === JSON.stringify(existing)) {
  50. return false;
  51. }
  52. if (existing.state === "DISABLED") {
  53. await module.exports.purgeQueue(queue.name);
  54. }
  55. await module.exports.updateQueue(queue);
  56. return false;
  57. }
  58. catch (err) {
  59. if (((_b = (_a = err === null || err === void 0 ? void 0 : err.context) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.statusCode) === 404) {
  60. await module.exports.createQueue(queue);
  61. return true;
  62. }
  63. throw err;
  64. }
  65. }
  66. exports.upsertQueue = upsertQueue;
  67. async function purgeQueue(name) {
  68. await client.post(`${name}:purge`);
  69. }
  70. exports.purgeQueue = purgeQueue;
  71. async function deleteQueue(name) {
  72. await client.delete(name);
  73. }
  74. exports.deleteQueue = deleteQueue;
  75. async function setIamPolicy(name, policy) {
  76. const res = await client.post(`${name}:setIamPolicy`, {
  77. policy,
  78. });
  79. return res.body;
  80. }
  81. exports.setIamPolicy = setIamPolicy;
  82. async function getIamPolicy(name) {
  83. const res = await client.post(`${name}:getIamPolicy`);
  84. return res.body;
  85. }
  86. exports.getIamPolicy = getIamPolicy;
  87. const ENQUEUER_ROLE = "roles/cloudtasks.enqueuer";
  88. async function setEnqueuer(name, invoker, assumeEmpty = false) {
  89. var _a, _b;
  90. let existing;
  91. if (assumeEmpty) {
  92. existing = {
  93. bindings: [],
  94. etag: "",
  95. version: 3,
  96. };
  97. }
  98. else {
  99. existing = await module.exports.getIamPolicy(name);
  100. }
  101. const [, project] = name.split("/");
  102. const invokerMembers = proto.getInvokerMembers(invoker, project);
  103. while (true) {
  104. const policy = {
  105. bindings: existing.bindings.filter((binding) => binding.role !== ENQUEUER_ROLE),
  106. etag: existing.etag,
  107. version: existing.version,
  108. };
  109. if (invokerMembers.length) {
  110. policy.bindings.push({ role: ENQUEUER_ROLE, members: invokerMembers });
  111. }
  112. if (JSON.stringify(policy) === JSON.stringify(existing)) {
  113. return;
  114. }
  115. try {
  116. await module.exports.setIamPolicy(name, policy);
  117. return;
  118. }
  119. catch (err) {
  120. if (((_b = (_a = err === null || err === void 0 ? void 0 : err.context) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.statusCode) === 429) {
  121. existing = await module.exports.getIamPolicy(name);
  122. continue;
  123. }
  124. throw err;
  125. }
  126. }
  127. }
  128. exports.setEnqueuer = setEnqueuer;
  129. function queueNameForEndpoint(endpoint) {
  130. return `projects/${endpoint.project}/locations/${endpoint.region}/queues/${endpoint.id}`;
  131. }
  132. exports.queueNameForEndpoint = queueNameForEndpoint;
  133. function queueFromEndpoint(endpoint) {
  134. const queue = Object.assign(Object.assign({}, JSON.parse(JSON.stringify(exports.DEFAULT_SETTINGS))), { name: queueNameForEndpoint(endpoint) });
  135. if (endpoint.taskQueueTrigger.rateLimits) {
  136. proto.copyIfPresent(queue.rateLimits, endpoint.taskQueueTrigger.rateLimits, "maxConcurrentDispatches", "maxDispatchesPerSecond");
  137. }
  138. if (endpoint.taskQueueTrigger.retryConfig) {
  139. proto.copyIfPresent(queue.retryConfig, endpoint.taskQueueTrigger.retryConfig, "maxAttempts", "maxDoublings");
  140. proto.convertIfPresent(queue.retryConfig, endpoint.taskQueueTrigger.retryConfig, "maxRetryDuration", "maxRetrySeconds", (0, functional_1.nullsafeVisitor)(proto.durationFromSeconds));
  141. proto.convertIfPresent(queue.retryConfig, endpoint.taskQueueTrigger.retryConfig, "maxBackoff", "maxBackoffSeconds", (0, functional_1.nullsafeVisitor)(proto.durationFromSeconds));
  142. proto.convertIfPresent(queue.retryConfig, endpoint.taskQueueTrigger.retryConfig, "minBackoff", "minBackoffSeconds", (0, functional_1.nullsafeVisitor)(proto.durationFromSeconds));
  143. }
  144. return queue;
  145. }
  146. exports.queueFromEndpoint = queueFromEndpoint;
  147. function triggerFromQueue(queue) {
  148. const taskQueueTrigger = {};
  149. if (queue.rateLimits) {
  150. taskQueueTrigger.rateLimits = {};
  151. proto.copyIfPresent(taskQueueTrigger.rateLimits, queue.rateLimits, "maxConcurrentDispatches", "maxDispatchesPerSecond");
  152. }
  153. if (queue.retryConfig) {
  154. taskQueueTrigger.retryConfig = {};
  155. proto.copyIfPresent(taskQueueTrigger.retryConfig, queue.retryConfig, "maxAttempts", "maxDoublings");
  156. proto.convertIfPresent(taskQueueTrigger.retryConfig, queue.retryConfig, "maxRetrySeconds", "maxRetryDuration", (0, functional_1.nullsafeVisitor)(proto.secondsFromDuration));
  157. proto.convertIfPresent(taskQueueTrigger.retryConfig, queue.retryConfig, "maxBackoffSeconds", "maxBackoff", (0, functional_1.nullsafeVisitor)(proto.secondsFromDuration));
  158. proto.convertIfPresent(taskQueueTrigger.retryConfig, queue.retryConfig, "minBackoffSeconds", "minBackoff", (0, functional_1.nullsafeVisitor)(proto.secondsFromDuration));
  159. }
  160. return taskQueueTrigger;
  161. }
  162. exports.triggerFromQueue = triggerFromQueue;