Chore/framework 6/n (#8356)

**What**
Refactor and improve job loader as well as move it to the framework

FIXES FRMW-2626
This commit is contained in:
Adrien de Peretti
2024-07-31 09:17:17 +00:00
committed by GitHub
parent 838eb0e34a
commit 6ccf83128c
13 changed files with 215 additions and 126 deletions
@@ -1,21 +0,0 @@
import {
IDistributedSchedulerStorage,
SchedulerOptions,
} from "@medusajs/orchestration"
export class MockSchedulerStorage implements IDistributedSchedulerStorage {
async schedule(
jobDefinition: string | { jobId: string },
schedulerOptions: SchedulerOptions
): Promise<void> {
return Promise.resolve()
}
async remove(jobId: string): Promise<void> {
return Promise.resolve()
}
async removeAll(): Promise<void> {
return Promise.resolve()
}
}
@@ -1,11 +0,0 @@
import { MedusaContainer } from "@medusajs/types"
export default async function handler(container: MedusaContainer) {
console.log(`You have received 5 orders today`)
}
export const config = {
name: "summarize-orders",
schedule: "* * * * * *",
numberOfExecutions: 2,
}
@@ -1,19 +0,0 @@
import path from "path"
import { registerJobs } from "../helpers/register-jobs"
import { WorkflowManager, WorkflowScheduler } from "@medusajs/orchestration"
import { MockSchedulerStorage } from "../__fixtures__/mock-scheduler-storage"
describe("register jobs", () => {
WorkflowScheduler.setStorage(new MockSchedulerStorage())
it("registers jobs from plugins", async () => {
await registerJobs([
{ resolve: path.join(__dirname, "../__fixtures__/plugin") },
])
const workflow = WorkflowManager.getWorkflow("job-summarize-orders")
expect(workflow).toBeDefined()
expect(workflow?.options.schedule).toEqual({
cron: "* * * * * *",
numberOfExecutions: 2,
})
})
})
@@ -1,101 +0,0 @@
import {
createStep,
createWorkflow,
StepResponse,
} from "@medusajs/workflows-sdk"
import { glob } from "glob"
import { logger } from "@medusajs/framework"
import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils"
export const registerJobs = async (plugins) => {
await Promise.all(
plugins.map(async (pluginDetails) => {
const files = glob.sync(
`${pluginDetails.resolve}/jobs/*.{ts,js,mjs,mts}`,
{
ignore: ["**/*.d.ts", "**/*.map"],
}
)
logger.debug(
`Registering ${files.length} jobs from ${pluginDetails.resolve}`
)
const jobs = await Promise.all(
files.map(async (file) => {
const module_ = await import(file)
const input = {
config: module_.config,
handler: module_.default,
}
validateConfig(input.config)
return input
})
)
const res = await Promise.all(jobs.map(createJob))
logger.debug(
`Registered ${res.length} jobs from ${pluginDetails.resolve}`
)
return res
})
)
}
const createJob = async ({ config, handler }) => {
const workflowName = `job-${config.name}`
const step = createStep(
`${config.name}-as-step`,
async (stepInput, stepContext) => {
const { container } = stepContext
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
try {
const res = await handler(container)
return new StepResponse(res, res)
} catch (error) {
logger.error(
`Scheduled job ${config.name} failed with error: ${error.message}`
)
throw error
}
}
)
createWorkflow(
{
name: workflowName,
schedule: {
cron: config.schedule,
numberOfExecutions: config.numberOfExecutions,
},
},
() => {
return step()
}
)
}
const validateConfig = (config) => {
if (!config) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Config is required for scheduled"
)
}
if (!config.schedule) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Cron schedule definition is required for scheduled jobs"
)
}
if (!config.name) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Job name is required for scheduled jobs"
)
}
}
+13 -9
View File
@@ -3,7 +3,7 @@ import { ConfigModule, MedusaContainer, PluginDetails } from "@medusajs/types"
import { ContainerRegistrationKeys, promiseAll } from "@medusajs/utils"
import { asValue } from "awilix"
import { Express, NextFunction, Request, Response } from "express"
import path, { join } from "path"
import { join } from "path"
import requestIp from "request-ip"
import { v4 } from "uuid"
import adminLoader from "./admin"
@@ -13,11 +13,11 @@ import {
container,
expressLoader,
featureFlagsLoader,
JobLoader,
logger,
pgConnectionLoader,
SubscriberLoader,
} from "@medusajs/framework"
import { registerJobs } from "./helpers/register-jobs"
import { registerWorkflows } from "./helpers/register-workflows"
import { getResolvedPlugins } from "./helpers/resolve-plugins"
import { resolvePluginsLinks } from "./helpers/resolve-plugins-links"
@@ -43,7 +43,7 @@ async function subscribersLoader(plugins: PluginDetails[]) {
/**
* Load subscribers from the medusa/medusa package
*/
await new SubscriberLoader(path.join(__dirname, "../subscribers")).load()
await new SubscriberLoader(join(__dirname, "../subscribers")).load()
/**
* Load subscribers from all the plugins.
@@ -51,7 +51,7 @@ async function subscribersLoader(plugins: PluginDetails[]) {
await Promise.all(
plugins.map(async (pluginDetails) => {
await new SubscriberLoader(
path.join(pluginDetails.resolve, "subscribers"),
join(pluginDetails.resolve, "subscribers"),
pluginDetails.options
).load()
})
@@ -59,11 +59,15 @@ async function subscribersLoader(plugins: PluginDetails[]) {
}
async function jobsLoader(plugins: PluginDetails[]) {
/**
* Load jobs from the medusa/medusa package. Remove once the medusa core is converted to a plugin
*/
await registerJobs([{ resolve: path.join(__dirname, "../") }])
await registerJobs(plugins)
const pluginJobSourcePaths = [
/**
* Load jobs from the medusa/medusa package. Remove once the medusa core is converted to a plugin
*/
join(__dirname, "../jobs"),
].concat(plugins.map((plugin) => join(plugin.resolve, "jobs")))
const jobLoader = new JobLoader(pluginJobSourcePaths)
await jobLoader.load()
}
async function loadEntrypoints(