@stacksjs/cloud 0.58.55 → 0.58.57

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/index.js CHANGED
@@ -1795,107 +1795,168 @@ class ComputeStack {
1795
1795
 
1796
1796
  // src/cloud/queue.ts
1797
1797
  import {aws_ec2 as ec24} from "aws-cdk-lib";
1798
+ import {pascalCase, slug as slug2} from "@stacksjs/strings";
1799
+ import {fs} from "@stacksjs/storage";
1800
+ import {path as path6} from "@stacksjs/path";
1798
1801
  import {Rule, Schedule} from "aws-cdk-lib/aws-events";
1799
1802
  import {EcsTask} from "aws-cdk-lib/aws-events-targets";
1800
1803
 
1801
1804
  class QueueStack {
1805
+ scope;
1806
+ props;
1802
1807
  constructor(scope, props) {
1803
- const rule = new Rule(scope, "QueueRule", {
1804
- ruleName: `${props.appName}-${props.appEnv}-queue-rule`,
1805
- schedule: Schedule.cron({ minute: "*", hour: "*", month: "*", weekDay: "*", year: "*" })
1806
- });
1807
- rule.addTarget(new EcsTask({
1808
- cluster: props.cluster,
1809
- taskDefinition: props.taskDefinition,
1810
- containerOverrides: [
1811
- {
1812
- containerName: `${props.appName}-${props.appEnv}-api`,
1813
- environment: [
1814
- {
1815
- name: "QUEUE_WORKER",
1816
- value: "true"
1817
- },
1818
- {
1819
- name: "JOB",
1820
- value: "DummyJob.ts"
1808
+ this.scope = scope;
1809
+ this.props = props;
1810
+ }
1811
+ async init() {
1812
+ const jobsDir = path6.jobsPath();
1813
+ try {
1814
+ const files = await fs.readdir(jobsDir);
1815
+ for (const file of files) {
1816
+ if (file.endsWith(".ts")) {
1817
+ const filePath = path6.jobsPath(file);
1818
+ const jobModule = await this.loadJobModule(filePath);
1819
+ const rate = jobModule.default.rate || "* * * * *";
1820
+ const schedule = Schedule.cron(this.cronScheduleFromRate(rate));
1821
+ const id = `QueueRule${pascalCase(file.replace(".ts", ""))}`;
1822
+ const rule = new Rule(this.scope, id, {
1823
+ ruleName: `${this.props.appName}-${this.props.appEnv}-queue-rule-${slug2(file.replace(".ts", ""))}`,
1824
+ schedule
1825
+ });
1826
+ rule.addTarget(new EcsTask({
1827
+ cluster: this.props.cluster,
1828
+ taskDefinition: this.props.taskDefinition,
1829
+ containerOverrides: [
1830
+ {
1831
+ containerName: `${this.props.appName}-${this.props.appEnv}-api`,
1832
+ environment: [
1833
+ {
1834
+ name: "QUEUE_WORKER",
1835
+ value: "true"
1836
+ },
1837
+ {
1838
+ name: "JOB",
1839
+ value: file
1840
+ }
1841
+ ]
1842
+ }
1843
+ ],
1844
+ retryAttempts: 3,
1845
+ subnetSelection: {
1846
+ subnetType: ec24.SubnetType.PUBLIC
1821
1847
  }
1822
- ]
1848
+ }));
1823
1849
  }
1824
- ],
1825
- retryAttempts: 3,
1826
- subnetSelection: {
1827
- subnetType: ec24.SubnetType.PUBLIC
1828
1850
  }
1829
- }));
1851
+ } catch (err2) {
1852
+ console.error("Error reading the jobs directory:", err2);
1853
+ }
1854
+ }
1855
+ cronScheduleFromRate(rate) {
1856
+ const parts = rate.split(" ");
1857
+ return {
1858
+ minute: parts[0],
1859
+ hour: parts[1],
1860
+ month: parts[2],
1861
+ weekDay: parts[3],
1862
+ year: parts[4]
1863
+ };
1864
+ }
1865
+ async loadJobModule(filePath) {
1866
+ const jobModule = await import(filePath);
1867
+ return jobModule;
1830
1868
  }
1831
1869
  }
1832
1870
 
1833
1871
  // src/cloud/index.ts
1834
1872
  class Cloud extends Stack2 {
1873
+ dns;
1874
+ security;
1875
+ storage;
1876
+ network;
1877
+ fileSystem;
1878
+ jumpBox;
1879
+ docs;
1880
+ email;
1881
+ redirects;
1882
+ permissions;
1883
+ ai;
1884
+ cli;
1885
+ api;
1886
+ cdn;
1887
+ queue;
1888
+ deployment;
1889
+ scope;
1890
+ props;
1835
1891
  constructor(scope, id, props) {
1836
1892
  super(scope, id, props);
1837
- const dns2 = new DnsStack(this, props);
1838
- const security2 = new SecurityStack(this, {
1893
+ this.scope = scope;
1894
+ this.props = props;
1895
+ this.dns = new DnsStack(this, props);
1896
+ this.security = new SecurityStack(this, {
1839
1897
  ...props,
1840
- zone: dns2.zone
1898
+ zone: this.dns.zone
1841
1899
  });
1842
- const storage5 = new StorageStack(this, {
1900
+ this.storage = new StorageStack(this, {
1843
1901
  ...props,
1844
- kmsKey: security2.kmsKey
1902
+ kmsKey: this.security.kmsKey
1845
1903
  });
1846
- const network2 = new NetworkStack(this, props);
1847
- const fileSystem = new FileSystemStack(this, {
1904
+ this.network = new NetworkStack(this, props);
1905
+ this.fileSystem = new FileSystemStack(this, {
1848
1906
  ...props,
1849
- vpc: network2.vpc
1907
+ vpc: this.network.vpc
1850
1908
  });
1851
- new JumpBoxStack(this, {
1909
+ this.jumpBox = new JumpBoxStack(this, {
1852
1910
  ...props,
1853
- vpc: network2.vpc,
1854
- fileSystem: fileSystem.fileSystem
1911
+ vpc: this.network.vpc,
1912
+ fileSystem: this.fileSystem.fileSystem
1855
1913
  });
1856
- const docs2 = new DocsStack(this, props);
1857
- new EmailStack(this, {
1914
+ this.docs = new DocsStack(this, props);
1915
+ this.email = new EmailStack(this, {
1858
1916
  ...props,
1859
- zone: dns2.zone
1917
+ zone: this.dns.zone
1860
1918
  });
1861
- new RedirectsStack(this, props);
1862
- new PermissionsStack(this);
1863
- let api;
1919
+ this.redirects = new RedirectsStack(this, props);
1920
+ this.permissions = new PermissionsStack(this);
1921
+ this.ai = new AiStack(this, props);
1922
+ this.cli = new CliStack(this, props);
1923
+ }
1924
+ async init() {
1864
1925
  if (config20.cloud.api?.deploy) {
1865
- api = new ComputeStack(this, {
1926
+ const props = this.props;
1927
+ this.api = new ComputeStack(this, {
1866
1928
  ...props,
1867
- vpc: network2.vpc,
1868
- fileSystem: fileSystem.fileSystem,
1869
- zone: dns2.zone,
1870
- certificate: security2.certificate
1929
+ vpc: this.network.vpc,
1930
+ fileSystem: this.fileSystem.fileSystem,
1931
+ zone: this.dns.zone,
1932
+ certificate: this.security.certificate
1871
1933
  });
1872
- new QueueStack(this, {
1934
+ this.queue = new QueueStack(this, {
1873
1935
  ...props,
1874
- cluster: api.cluster,
1875
- taskDefinition: api.taskDefinition
1936
+ cluster: this.api.cluster,
1937
+ taskDefinition: this.api.taskDefinition
1938
+ });
1939
+ await this.queue.init();
1940
+ this.cdn = new CdnStack(this, {
1941
+ ...props,
1942
+ publicBucket: this.storage.publicBucket,
1943
+ logBucket: this.storage.logBucket,
1944
+ certificate: this.security.certificate,
1945
+ firewall: this.security.firewall,
1946
+ originRequestFunction: this.docs.originRequestFunction,
1947
+ zone: this.dns.zone,
1948
+ cliSetupUrl: this.cli.cliSetupUrl,
1949
+ askAiUrl: this.ai.askAiUrl,
1950
+ summarizeAiUrl: this.ai.summarizeAiUrl,
1951
+ lb: this.api?.lb
1952
+ });
1953
+ this.deployment = new DeploymentStack(this, {
1954
+ ...props,
1955
+ publicBucket: this.storage.publicBucket,
1956
+ privateBucket: this.storage.privateBucket,
1957
+ cdn: this.cdn.distribution
1876
1958
  });
1877
1959
  }
1878
- const ai2 = new AiStack(this, props);
1879
- const cli2 = new CliStack(this, props);
1880
- const cdn2 = new CdnStack(this, {
1881
- ...props,
1882
- publicBucket: storage5.publicBucket,
1883
- logBucket: storage5.logBucket,
1884
- certificate: security2.certificate,
1885
- firewall: security2.firewall,
1886
- originRequestFunction: docs2.originRequestFunction,
1887
- zone: dns2.zone,
1888
- cliSetupUrl: cli2.cliSetupUrl,
1889
- askAiUrl: ai2.askAiUrl,
1890
- summarizeAiUrl: ai2.summarizeAiUrl,
1891
- lb: api?.lb
1892
- });
1893
- new DeploymentStack(this, {
1894
- ...props,
1895
- publicBucket: storage5.publicBucket,
1896
- privateBucket: storage5.privateBucket,
1897
- cdn: cdn2.distribution
1898
- });
1899
1960
  }
1900
1961
  }
1901
1962
  export {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/cloud",
3
3
  "type": "module",
4
- "version": "0.58.55",
4
+ "version": "0.58.57",
5
5
  "description": "The Stacks cloud/serverless integration & implementation.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aws-sdk-layer",
3
- "version": "0.58.55",
3
+ "version": "0.58.57",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "license": "ISC",
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-new */
2
1
  import { Stack } from 'aws-cdk-lib'
3
2
  import type { Construct } from 'constructs'
4
3
  import { config } from '@stacksjs/config'
@@ -23,87 +22,114 @@ import { QueueStack } from './queue'
23
22
  // import { DashboardStack } from './dashboard'
24
23
 
25
24
  export class Cloud extends Stack {
25
+ dns: DnsStack
26
+ security: SecurityStack
27
+ storage: StorageStack
28
+ network: NetworkStack
29
+ fileSystem: FileSystemStack
30
+ jumpBox: JumpBoxStack
31
+ docs: DocsStack
32
+ email: EmailStack
33
+ redirects: RedirectsStack
34
+ permissions: PermissionsStack
35
+ ai: AiStack
36
+ cli: CliStack
37
+ api!: ComputeStack
38
+ cdn!: CdnStack
39
+ queue!: QueueStack
40
+ deployment!: DeploymentStack
41
+ scope: Construct
42
+ props: CloudOptions
43
+
26
44
  constructor(scope: Construct, id: string, props: CloudOptions) {
27
45
  super(scope, id, props)
28
- // please beware: be careful changing the order of the stack creations below
29
- const dns = new DnsStack(this, props)
46
+ this.scope = scope
47
+ this.props = props
30
48
 
31
- const security = new SecurityStack(this, {
49
+ // please beware: be careful changing the order of the stack creations below
50
+ this.dns = new DnsStack(this, props)
51
+ this.security = new SecurityStack(this, {
32
52
  ...props,
33
- zone: dns.zone,
53
+ zone: this.dns.zone,
34
54
  })
35
55
 
36
- const storage = new StorageStack(this, {
56
+ this.storage = new StorageStack(this, {
37
57
  ...props,
38
- kmsKey: security.kmsKey,
58
+ kmsKey: this.security.kmsKey,
39
59
  })
40
60
 
41
- const network = new NetworkStack(this, props)
61
+ this.network = new NetworkStack(this, props)
42
62
 
43
- const fileSystem = new FileSystemStack(this, {
63
+ this.fileSystem = new FileSystemStack(this, {
44
64
  ...props,
45
- vpc: network.vpc,
65
+ vpc: this.network.vpc,
46
66
  })
47
67
 
48
- new JumpBoxStack(this, {
68
+ this.jumpBox = new JumpBoxStack(this, {
49
69
  ...props,
50
- vpc: network.vpc,
51
- fileSystem: fileSystem.fileSystem,
70
+ vpc: this.network.vpc,
71
+ fileSystem: this.fileSystem.fileSystem,
52
72
  })
53
73
 
54
- const docs = new DocsStack(this, props)
74
+ this.docs = new DocsStack(this, props)
55
75
 
56
- new EmailStack(this, {
76
+ this.email = new EmailStack(this, {
57
77
  ...props,
58
- zone: dns.zone,
78
+ zone: this.dns.zone,
59
79
  })
60
80
 
61
- new RedirectsStack(this, props)
81
+ this.redirects = new RedirectsStack(this, props)
62
82
 
63
- new PermissionsStack(this)
83
+ this.permissions = new PermissionsStack(this)
64
84
 
65
85
  // new DashboardStack(this)
66
86
 
67
- let api
87
+ this.ai = new AiStack(this, props)
88
+
89
+ this.cli = new CliStack(this, props)
90
+ }
91
+
92
+ // we use an async init() method here because we need to wait for the
93
+
94
+ async init() {
68
95
  if (config.cloud.api?.deploy) {
69
- api = new ComputeStack(this, {
96
+ const props = this.props
97
+ this.api = new ComputeStack(this, {
70
98
  ...props,
71
- vpc: network.vpc,
72
- fileSystem: fileSystem.fileSystem,
73
- zone: dns.zone,
74
- certificate: security.certificate,
99
+ vpc: this.network.vpc,
100
+ fileSystem: this.fileSystem.fileSystem,
101
+ zone: this.dns.zone,
102
+ certificate: this.security.certificate,
75
103
  })
76
104
 
77
- new QueueStack(this, {
105
+ this.queue = new QueueStack(this, {
78
106
  ...props,
79
- cluster: api.cluster,
80
- taskDefinition: api.taskDefinition,
107
+ cluster: this.api.cluster,
108
+ taskDefinition: this.api.taskDefinition,
81
109
  })
82
- }
83
-
84
- const ai = new AiStack(this, props)
85
110
 
86
- const cli = new CliStack(this, props)
111
+ await this.queue.init()
87
112
 
88
- const cdn = new CdnStack(this, {
89
- ...props,
90
- publicBucket: storage.publicBucket,
91
- logBucket: storage.logBucket,
92
- certificate: security.certificate,
93
- firewall: security.firewall,
94
- originRequestFunction: docs.originRequestFunction,
95
- zone: dns.zone,
96
- cliSetupUrl: cli.cliSetupUrl,
97
- askAiUrl: ai.askAiUrl,
98
- summarizeAiUrl: ai.summarizeAiUrl,
99
- lb: api?.lb,
100
- })
113
+ this.cdn = new CdnStack(this, {
114
+ ...props,
115
+ publicBucket: this.storage.publicBucket,
116
+ logBucket: this.storage.logBucket,
117
+ certificate: this.security.certificate,
118
+ firewall: this.security.firewall,
119
+ originRequestFunction: this.docs.originRequestFunction,
120
+ zone: this.dns.zone,
121
+ cliSetupUrl: this.cli.cliSetupUrl,
122
+ askAiUrl: this.ai.askAiUrl,
123
+ summarizeAiUrl: this.ai.summarizeAiUrl,
124
+ lb: this.api?.lb,
125
+ })
101
126
 
102
- new DeploymentStack(this, {
103
- ...props,
104
- publicBucket: storage.publicBucket,
105
- privateBucket: storage.privateBucket,
106
- cdn: cdn.distribution,
107
- })
127
+ this.deployment = new DeploymentStack(this, {
128
+ ...props,
129
+ publicBucket: this.storage.publicBucket,
130
+ privateBucket: this.storage.privateBucket,
131
+ cdn: this.cdn.distribution,
132
+ })
133
+ }
108
134
  }
109
135
  }
@@ -1,6 +1,9 @@
1
- import type { Cluster, TaskDefinition } from 'aws-cdk-lib'
1
+ import type { Cluster, TaskDefinition } from 'aws-cdk-lib/aws-ecs'
2
2
  import { aws_ec2 as ec2 } from 'aws-cdk-lib'
3
3
  import type { Construct } from 'constructs'
4
+ import { pascalCase, slug } from '@stacksjs/strings'
5
+ import { fs } from '@stacksjs/storage'
6
+ import { path } from '@stacksjs/path'
4
7
  import { Rule, Schedule } from 'aws-cdk-lib/aws-events'
5
8
  import { EcsTask } from 'aws-cdk-lib/aws-events-targets'
6
9
  import type { NestedCloudProps } from '../types'
@@ -11,37 +14,91 @@ export interface QueueStackProps extends NestedCloudProps {
11
14
  }
12
15
 
13
16
  export class QueueStack {
17
+ scope: Construct
18
+ props: QueueStackProps
19
+
14
20
  constructor(scope: Construct, props: QueueStackProps) {
15
- const rule = new Rule(scope, 'QueueRule', {
16
- // schedule to run every second
17
- ruleName: `${props.appName}-${props.appEnv}-queue-rule`,
18
- schedule: Schedule.cron({ minute: '*', hour: '*', month: '*', weekDay: '*', year: '*' }),
19
- })
20
-
21
- rule.addTarget(new EcsTask({
22
- cluster: props.cluster,
23
- taskDefinition: props.taskDefinition,
24
- containerOverrides: [
25
- {
26
- containerName: `${props.appName}-${props.appEnv}-api`,
27
- environment: [
28
- {
29
- name: 'QUEUE_WORKER',
30
- value: 'true',
31
- },
32
- {
33
- name: 'JOB',
34
- value: 'DummyJob.ts',
21
+ this.scope = scope
22
+ this.props = props
23
+ }
24
+
25
+ async init() {
26
+ const jobsDir = path.jobsPath()
27
+
28
+ try {
29
+ const files = await fs.readdir(jobsDir)
30
+
31
+ for (const file of files) {
32
+ if (file.endsWith('.ts')) {
33
+ const filePath = path.jobsPath(file)
34
+
35
+ // Await the loading of the job module
36
+ const jobModule = await this.loadJobModule(filePath)
37
+
38
+ // Now you can safely access jobModule.default.rate
39
+ const rate = jobModule.default.rate || '* * * * *'
40
+
41
+ // Rest of your logic here...
42
+ // Convert the rate to a Schedule object
43
+ const schedule = Schedule.cron(this.cronScheduleFromRate(rate))
44
+
45
+ const id = `QueueRule${pascalCase(file.replace('.ts', ''))}`
46
+ // Perform operations with the jobModule.default as needed
47
+ const rule = new Rule(this.scope, id, {
48
+ // schedule to run every second
49
+ ruleName: `${this.props.appName}-${this.props.appEnv}-queue-rule-${slug(file.replace('.ts', ''))}`,
50
+ schedule,
51
+ })
52
+
53
+ rule.addTarget(new EcsTask({
54
+ cluster: this.props.cluster,
55
+ taskDefinition: this.props.taskDefinition,
56
+ containerOverrides: [
57
+ {
58
+ containerName: `${this.props.appName}-${this.props.appEnv}-api`,
59
+ environment: [
60
+ {
61
+ name: 'QUEUE_WORKER',
62
+ value: 'true',
63
+ },
64
+ {
65
+ name: 'JOB',
66
+ value: file,
67
+ },
68
+ ],
69
+ },
70
+ ],
71
+
72
+ retryAttempts: 3,
73
+
74
+ subnetSelection: {
75
+ subnetType: ec2.SubnetType.PUBLIC, // SubnetType.PRIVATE_WITH_EGRESS
35
76
  },
36
- ],
37
- },
38
- ],
77
+ }))
78
+ }
79
+ }
80
+ }
81
+ catch (err) {
82
+ console.error('Error reading the jobs directory:', err)
83
+ }
84
+ }
85
+
86
+ // Helper function to convert a rate string to a cron object for AWS Schedule
87
+ cronScheduleFromRate(rate: string): { minute?: string, hour?: string, month?: string, weekDay?: string, year?: string } {
88
+ // Assuming the rate is in standard cron format, split it to map to the AWS Schedule.cron() parameters
89
+ const parts = rate.split(' ')
90
+ return {
91
+ minute: parts[0],
92
+ hour: parts[1],
93
+ month: parts[2],
94
+ weekDay: parts[3],
95
+ year: parts[4],
96
+ }
97
+ }
39
98
 
40
- retryAttempts: 3,
99
+ async loadJobModule(filePath: string) {
100
+ const jobModule = await import(filePath)
41
101
 
42
- subnetSelection: {
43
- subnetType: ec2.SubnetType.PUBLIC, // SubnetType.PRIVATE_WITH_EGRESS
44
- },
45
- }))
102
+ return jobModule
46
103
  }
47
104
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stacks-router-layer",
3
- "version": "0.58.55",
3
+ "version": "0.58.57",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "license": "MIT",
@@ -10,6 +10,6 @@
10
10
  "test": "echo \"Error: no test specified\" && exit 1"
11
11
  },
12
12
  "dependencies": {
13
- "@stacksjs/router": "^0.58.53"
13
+ "@stacksjs/router": "^0.58.56"
14
14
  }
15
15
  }