diff --git a/bun.lock b/bun.lock index 00c994f648..0fc4f60ca2 100755 --- a/bun.lock +++ b/bun.lock @@ -1135,9 +1135,6 @@ "storage/framework/core/queue": { "name": "@stacksjs/queue", "version": "0.68.2", - "dependencies": { - "@poppinss/defer": "^1.1.0", - }, "devDependencies": { "@stacksjs/development": "storage/framework/core/development", }, @@ -1205,6 +1202,7 @@ "croner": "^9.0.0", }, "devDependencies": { + "@stacksjs/actions": "storage/framework/core/actions", "@stacksjs/development": "storage/framework/core/development", }, }, @@ -2388,8 +2386,6 @@ "@polka/url": ["@polka/url@1.0.0-next.28", "", {}, "sha512-8LduaNlMZGwdZ6qWrKlfa+2M4gahzFkprZiAt2TF8uS0qQgBizKXpXURqvTJ4WtmupWxaLqjRb2UCTe72mu+Aw=="], - "@poppinss/defer": ["@poppinss/defer@1.1.0", "", { "dependencies": { "fastq": "^1.17.1" } }, "sha512-+hkwxhKdxbmfTNo9MJrY2Rz7GrMokgx71zSSnEv6/pftaCQ8+bF5vQkE7KCBIKUn+kCth1cI1d8LWDMvPUIRMw=="], - "@poppinss/macroable": ["@poppinss/macroable@1.0.3", "", {}, "sha512-B4iV6QxW//Fn17+qF1EMZRmoThIUJlCtcO85yoRDJnMyHeAthjz4ig9OTkfGGXKtQhcdPX0me75gU5K9J897+w=="], "@poppinss/utils": ["@poppinss/utils@6.8.3", "", { "dependencies": { "@lukeed/ms": "^2.0.2", "@types/bytes": "^3.1.4", "@types/pluralize": "^0.0.33", "bytes": "^3.1.2", "case-anything": "^3.1.0", "flattie": "^1.1.1", "pluralize": "^8.0.0", "safe-stable-stringify": "^2.5.0", "secure-json-parse": "^2.7.0", "slash": "^5.1.0", "slugify": "^1.6.6", "truncatise": "^0.0.8" } }, "sha512-YGeH7pIUm9ExONURNH3xN61dBZ0SXgVuPA9E76t7EHeZHXPNrmR8TlbXQaka6kd5n+cpBNcHG4VsVfYf59bZ7g=="], @@ -2712,7 +2708,7 @@ "@stacksjs/query-builder": ["@stacksjs/query-builder@workspace:storage/framework/core/query-builder", { "dependencies": { "@stacksjs/database": "storage/framework/core/database", "@stacksjs/types": "storage/framework/core/types", "kysely": "^0.27.5", "kysely-bun-worker": "^0.7.0", "mysql2": "^3.12.0", "pg": "^8.13.1" }, "devDependencies": { "@stacksjs/development": "storage/framework/core/development", "@types/pg": "^8.11.10" } }], - "@stacksjs/queue": ["@stacksjs/queue@workspace:storage/framework/core/queue", { "dependencies": { "@poppinss/defer": "^1.1.0" }, "devDependencies": { "@stacksjs/development": "storage/framework/core/development" } }], + "@stacksjs/queue": ["@stacksjs/queue@workspace:storage/framework/core/queue", { "devDependencies": { "@stacksjs/development": "storage/framework/core/development" } }], "@stacksjs/radio-group": ["@stacksjs/radio-group@workspace:storage/framework/core/components/radio-group", { "dependencies": { "@stacksjs/ui": "storage/framework/core/ui", "highlight.js": "^11.11.1" }, "devDependencies": { "@iconify-json/heroicons": "^1.2.2", "@microsoft/api-extractor": "^7.48.1", "@stacksjs/alias": "storage/framework/core/alias", "@stacksjs/development": "storage/framework/core/development", "@types/clean-css": "^4.2.11", "@vue/tsconfig": "^0.7.0", "clean-css": "^5.3.3", "unocss": "0.65.3", "unplugin-icons": "^0.22.0" } }], @@ -2726,7 +2722,7 @@ "@stacksjs/rpx": ["@stacksjs/rpx@0.9.1", "", { "bin": { "rpx": "dist/bin/cli.js", "reverse-proxy": "dist/bin/cli.js" } }, "sha512-Lus+Zvn4+0g7InjTtsIC6pZ4lR4sYY/oB4OrBDNNfIedEQOYLgSZoj3lynZlv6A3UJQzDtzZfFMMGoQfTgxXFw=="], - "@stacksjs/scheduler": ["@stacksjs/scheduler@workspace:storage/framework/core/scheduler", { "dependencies": { "croner": "^9.0.0" }, "devDependencies": { "@stacksjs/development": "storage/framework/core/development" } }], + "@stacksjs/scheduler": ["@stacksjs/scheduler@workspace:storage/framework/core/scheduler", { "dependencies": { "croner": "^9.0.0" }, "devDependencies": { "@stacksjs/actions": "storage/framework/core/actions", "@stacksjs/development": "storage/framework/core/development" } }], "@stacksjs/search-engine": ["@stacksjs/search-engine@workspace:storage/framework/core/search-engine", { "dependencies": { "@opensearch-project/opensearch": "^2.13.0", "meilisearch": "^0.47.0" }, "devDependencies": { "@stacksjs/config": "storage/framework/core/config", "@stacksjs/development": "storage/framework/core/development" } }], diff --git a/storage/framework/core/queue/package.json b/storage/framework/core/queue/package.json index 6b8529a2cb..9d70cf79da 100644 --- a/storage/framework/core/queue/package.json +++ b/storage/framework/core/queue/package.json @@ -42,9 +42,6 @@ "typecheck": "bun tsc --noEmit", "prepublishOnly": "bun run build" }, - "dependencies": { - "@poppinss/defer": "^1.1.0" - }, "devDependencies": { "@stacksjs/development": "workspace:*" } diff --git a/storage/framework/core/queue/src/index.ts b/storage/framework/core/queue/src/index.ts index 7a302a396c..f8faefc8b1 100644 --- a/storage/framework/core/queue/src/index.ts +++ b/storage/framework/core/queue/src/index.ts @@ -22,4 +22,4 @@ export class Job { } } -export { DeferQueue } from '@poppinss/defer' +export { runJob } from './run' diff --git a/storage/framework/core/queue/src/run.ts b/storage/framework/core/queue/src/run.ts new file mode 100644 index 0000000000..7ede92692e --- /dev/null +++ b/storage/framework/core/queue/src/run.ts @@ -0,0 +1,43 @@ +import { runAction } from '@stacksjs/actions' +import { log } from '@stacksjs/cli' +import { projectPath } from '@stacksjs/path' + +interface JobConfig { + name?: string + description?: string + tries?: number + backoff?: number + rate?: string + handle?: () => void | Promise + action?: string | (() => void | Promise) +} + +export async function runJob(name: string): Promise { + log.info(`Running job: ${name}`) + try { + const jobModule = await import(projectPath(`Jobs/${name}.ts`)) + const job = jobModule.default as JobConfig + + if (job.action) { + // If action is a string, run it via runAction + if (typeof job.action === 'string') { + await runAction(job.action) + } + // If action is a function, execute it directly + else if (typeof job.action === 'function') { + await job.action() + } + } + // If handle is defined, execute it + else if (job.handle) { + await job.handle() + } + else { + throw new Error(`Job ${name} must define either a handle function or an action`) + } + } + catch (error) { + log.error(`Job ${name} failed:`, error) + throw error + } +} diff --git a/storage/framework/core/scheduler/package.json b/storage/framework/core/scheduler/package.json index 277eeedb62..32f8a80fe2 100644 --- a/storage/framework/core/scheduler/package.json +++ b/storage/framework/core/scheduler/package.json @@ -37,6 +37,7 @@ "croner": "^9.0.0" }, "devDependencies": { + "@stacksjs/actions": "workspace:*", "@stacksjs/development": "workspace:*" } } diff --git a/storage/framework/core/scheduler/src/schedule.ts b/storage/framework/core/scheduler/src/schedule.ts index 32c810bc8f..cdf2c89104 100644 --- a/storage/framework/core/scheduler/src/schedule.ts +++ b/storage/framework/core/scheduler/src/schedule.ts @@ -1,6 +1,8 @@ import type { CatchCallbackFn, CronOptions } from './' import type { Timezone } from './types' +import { runAction } from '@stacksjs/actions' import { log, runCommand } from '@stacksjs/cli' +import { runJob } from '@stacksjs/queue' import { Cron } from './' // Base interface for common methods @@ -212,7 +214,7 @@ export class Schedule implements UntimedSchedule { return new Schedule(async () => { log.info(`Running job: ${name}`) try { - await runCommand(`node path/to/jobs/${name}.js`) + await runJob(name) } catch (error) { log.error(`Job ${name} failed:`, error) @@ -225,7 +227,7 @@ export class Schedule implements UntimedSchedule { return new Schedule(async () => { log.info(`Running action: ${name}`) try { - await runCommand(`node path/to/actions/${name}.js`) + await runAction(name) } catch (error) { log.error(`Action ${name} failed:`, error)