-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(medusa): Alternative Subscriber API and new ScheduledJobs API #5624
Conversation
🦋 Changeset detectedLatest commit: b80ccbd The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 3 Ignored Deployments
|
/snapshot-this |
🚀 A snapshot release has been made for this PRTest the snapshots by updating your yarn add @medusajs/admin-ui@2.1.8-snapshot-20231115070542 yarn add @medusajs/medusa@1.17.5-snapshot-20231115070542 yarn add medusa-source-shopify@1.2.10-snapshot-20231115070542 yarn add @medusajs/medusa-oas-cli@0.2.25-snapshot-20231115070542 yarn add @medusajs/utils@1.11.0-snapshot-20231115070542 yarn add @medusajs/workflows@0.3.0-snapshot-20231115070542
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incredibly clean PR @kasperkristensen. Great work. I didn't have much to add.
I would love to see us convert our core subscribers to use the new DX in case people browse the repo (and to dogfood the feature).
We could add the required loader and convert them in a follow up PR. What do you think? Maybe ask someone else from the team to do this, so we get it tested.
packages/medusa/src/loaders/helpers/subscribers/__tests__/index.spec.ts
Outdated
Show resolved
Hide resolved
packages/medusa/src/loaders/helpers/subscribers/__tests__/index.spec.ts
Outdated
Show resolved
Hide resolved
/snapshot-this |
🚀 A snapshot release has been made for this PRTest the snapshots by updating your yarn add @medusajs/admin-ui@2.1.8-snapshot-20231116082935 yarn add @medusajs/medusa@1.17.5-snapshot-20231116082935 yarn add medusa-source-shopify@1.2.10-snapshot-20231116082935 yarn add @medusajs/medusa-oas-cli@0.2.25-snapshot-20231116082935 yarn add @medusajs/pricing@0.1.4-snapshot-20231116082935 yarn add @medusajs/types@1.11.7-snapshot-20231116082935 yarn add @medusajs/utils@1.11.0-snapshot-20231116082935 yarn add @medusajs/workflows@0.3.0-snapshot-20231116082935
|
…js/medusa into feat/subscribers-jobs-api
@kasperkristensen should we merge this badboy? |
What
Subscriber API
This PR introduces a new API for loading subscribers in Medusa. The API uses the same syntax as the new API Routes, and is meant to simplify the process of setting up Subscribers in projects.
Subscribers are still loaded through the folder
/src/subscribers
, and an example of the new syntax could look like this:Here it is important to notice that the file has one default export (the subscriber handler), and one named const export (the config).
The handler takes an argument of
SubscriberArgs
that can be destructures to:data
which is the data that is passed when the subscribed event is emittedeventName
which allows the user to tell which event is currently being handled which is useful if a handler is subscribed to multiple eventscontainer
which allows developers to resolve services, repositories, etc.pluginOptions
which allows the developer to access the plugin options when adding subscribers to a plugin.The config (of type
SubscriberConfig
) has two fields:event
that the subscriber should subscribe to, this can be either astring
or anstring[]
.context
which is an optional object of type{ subscriberId?: string } & Record<string, unknown>
. Which can be used to pass any context the user wants to the eventBusService. If acontext.subscriberId
is not provided the loader will attempt to infer it from the handler name, and if the handler is an anonymous function then from the file name.When using
event-bus-local
event-bus-local
, so setting acontext.subscriberId
of theinferIdentifier
won't have any effect in that case.eventName
passed to the handler will be undefined when usingevent-bus-local
as it does not pass it properly.Scheduled Jobs API
The new Scheduled Jobs API is also aimed at making it easier for users to setup scheduled jobs (cron jobs) in Medusa. Currently, the developer needs to create a loader, and within that loader they need to resolve the
jobSchedulerService
and create their job. With this new PR Scheduled Jobs can be created directly in the new/src/jobs
folder.The syntax is as follows:
Again we need a default export of type function which is the job handler, and a named export for the config.
The handler takes one argument of type ScheduledJobArgs, that can be destructured to the following:
container
, the Medusa container.data
, an optional object that can be passed in theconfig
. (This field does not make a lot of sense with this syntax, but I have kept it to keep this syntax aligned with the old approach)pluginOptions
, the options of the plugin that the job was scheduled from.The config of type
ScheduledJobConfig
has the following fields:name
, the name that is assigned to the job.schedule
, the cron schedule that determines when the job should run.data
, an optional object that will be passed on to the handler.Both
src/subscribers/products
to organize related subscribers.Resolves CORE-1547, CORE-1548