effortless-aws 0.30.0 → 0.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-ISJC6CHC.js → chunk-HGSMOO4A.js} +60 -1
- package/dist/index.d.ts +319 -188
- package/dist/index.js +199 -54
- package/dist/index.js.map +1 -1
- package/dist/runtime/wrap-api.js +2 -2
- package/dist/runtime/wrap-bucket.js +2 -2
- package/dist/runtime/wrap-cron.js +40 -0
- package/dist/runtime/wrap-fifo-queue.js +4 -4
- package/dist/runtime/wrap-table-stream.js +5 -5
- package/dist/runtime/wrap-worker.js +109 -0
- package/package.json +2 -1
|
@@ -474,6 +474,62 @@ var createQueueClient = (queueName) => {
|
|
|
474
474
|
};
|
|
475
475
|
};
|
|
476
476
|
|
|
477
|
+
// src/runtime/worker-client.ts
|
|
478
|
+
import { SQS as SQS2 } from "@aws-sdk/client-sqs";
|
|
479
|
+
import { ECSClient, DescribeServicesCommand, UpdateServiceCommand } from "@aws-sdk/client-ecs";
|
|
480
|
+
var createWorkerClient = (depValue) => {
|
|
481
|
+
const lastColon = depValue.lastIndexOf(":");
|
|
482
|
+
const workerName = depValue.slice(0, lastColon);
|
|
483
|
+
const idleTimeoutMs = Number(depValue.slice(lastColon + 1)) * 1e3;
|
|
484
|
+
const queueName = `${workerName}-worker`;
|
|
485
|
+
const cluster = workerName.replace(/-[^-]+$/, "");
|
|
486
|
+
const service = workerName;
|
|
487
|
+
let sqsClient = null;
|
|
488
|
+
const getSqs = () => sqsClient ??= new SQS2({});
|
|
489
|
+
let ecsClient = null;
|
|
490
|
+
const getEcs = () => ecsClient ??= new ECSClient({});
|
|
491
|
+
let resolvedQueueUrl;
|
|
492
|
+
const getQueueUrl = async () => {
|
|
493
|
+
if (resolvedQueueUrl) return resolvedQueueUrl;
|
|
494
|
+
const result = await getSqs().getQueueUrl({ QueueName: queueName });
|
|
495
|
+
resolvedQueueUrl = result.QueueUrl;
|
|
496
|
+
return resolvedQueueUrl;
|
|
497
|
+
};
|
|
498
|
+
let awakeUntil = 0;
|
|
499
|
+
const ensureRunning = async () => {
|
|
500
|
+
if (Date.now() < awakeUntil) return;
|
|
501
|
+
const resp = await getEcs().send(new DescribeServicesCommand({ cluster, services: [service] }));
|
|
502
|
+
const svc = resp.services?.[0];
|
|
503
|
+
if (svc && svc.desiredCount === 0) {
|
|
504
|
+
await getEcs().send(new UpdateServiceCommand({ cluster, service, desiredCount: 1 }));
|
|
505
|
+
}
|
|
506
|
+
awakeUntil = Date.now() + idleTimeoutMs;
|
|
507
|
+
};
|
|
508
|
+
return {
|
|
509
|
+
async send(msg, options) {
|
|
510
|
+
const queueUrl = await getQueueUrl();
|
|
511
|
+
await getSqs().sendMessage({
|
|
512
|
+
QueueUrl: queueUrl,
|
|
513
|
+
MessageBody: JSON.stringify(msg),
|
|
514
|
+
...options?.delay ? { DelaySeconds: toSeconds(options.delay) } : {}
|
|
515
|
+
});
|
|
516
|
+
if (options?.start !== false && !options?.delay) {
|
|
517
|
+
await ensureRunning();
|
|
518
|
+
}
|
|
519
|
+
},
|
|
520
|
+
async status() {
|
|
521
|
+
const resp = await getEcs().send(new DescribeServicesCommand({ cluster, services: [service] }));
|
|
522
|
+
const svc = resp.services?.[0];
|
|
523
|
+
if (svc && svc.runningCount && svc.runningCount > 0) return "running";
|
|
524
|
+
return "idle";
|
|
525
|
+
},
|
|
526
|
+
async stop() {
|
|
527
|
+
await getEcs().send(new UpdateServiceCommand({ cluster, service, desiredCount: 0 }));
|
|
528
|
+
awakeUntil = 0;
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
};
|
|
532
|
+
|
|
477
533
|
// src/runtime/ssm-client.ts
|
|
478
534
|
import { SSM } from "@aws-sdk/client-ssm";
|
|
479
535
|
var client = null;
|
|
@@ -512,7 +568,8 @@ var DEP_FACTORIES = {
|
|
|
512
568
|
},
|
|
513
569
|
bucket: (name) => createBucketClient(name),
|
|
514
570
|
mailer: () => createEmailClient(),
|
|
515
|
-
queue: (name) => createQueueClient(name)
|
|
571
|
+
queue: (name) => createQueueClient(name),
|
|
572
|
+
worker: (name) => createWorkerClient(name)
|
|
516
573
|
};
|
|
517
574
|
var parseDepValue = (raw) => {
|
|
518
575
|
const idx = raw.indexOf(":");
|
|
@@ -678,5 +735,7 @@ export {
|
|
|
678
735
|
createTableClient,
|
|
679
736
|
AUTH_COOKIE_NAME,
|
|
680
737
|
createBucketClient,
|
|
738
|
+
buildDeps,
|
|
739
|
+
buildParams,
|
|
681
740
|
createHandlerRuntime
|
|
682
741
|
};
|