Skip to content
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

Bot cron job Part 1/2 #1743

Merged
merged 19 commits into from
Apr 3, 2023
Merged

Bot cron job Part 1/2 #1743

merged 19 commits into from
Apr 3, 2023

Conversation

jamestouri
Copy link
Contributor

@jamestouri jamestouri commented Mar 29, 2023

Here is an example of a bot scheduled on the cron job. It was running every minute, then stopped it, then ran every minute again.

Screen Shot 2023-04-03 at 1 11 22 PM

Next steps after PR

  • Change TimingInput.tsx to allow minutes
  • Change props for TimingInput.tsx to hide delayed start

@jamestouri jamestouri requested a review from a team as a code owner March 29, 2023 23:03
@vercel
Copy link

vercel bot commented Mar 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
medplum-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 3, 2023 11:11pm
medplum-www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 3, 2023 11:11pm

@reshmakh reshmakh added the bots Features and fixes related to bots and automation label Mar 29, 2023
@coveralls
Copy link

coveralls commented Mar 30, 2023

Coverage Status

Coverage: 93.953% (-0.05%) from 93.998% when pulling 035d78b on james-bot-cron-job into 773dbb4 on main.

Copy link
Member

@rahul1 rahul1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As part of this PR, can you add a short section on our "Bot basics" page to indicate that Bos can be run on cron timers (under the "Executing a Bot" section)? https://www.medplum.com/docs/bots/bot-basics#executing-a-bot

Copy link
Member

@codyebberson codyebberson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job @jamestouri ! Structurally, this looks very solid, and I think it's close to ready for beta testing.

The main feedback I have on the PR as it stands is to rename scheduledtiming.ts to cron.ts and rename the exported methods accordingly.

There are a few other nit comments.

Then there is one more thing we need to do before we can push to prod: We should add a feature flag check inside addScheduledTimingJobs / addCronJobs for Project.features.

Here is an example of how we do feature flag checks: https://github.com/medplum/medplum/blob/main/packages/server/src/fhir/operations/execute.ts#L129-L137

export async function isBotEnabled(bot: Bot): Promise<boolean> {
  const project = await systemRepo.readResource<Project>('Project', bot.meta?.project as string);
  return !!project.features?.includes('bots');
}

We can introduce a similar feature flag named cron for this.

Otherwise, this looks great, and will be an awesome feature.

packages/server/src/workers/index.ts Outdated Show resolved Hide resolved
packages/server/src/workers/index.ts Outdated Show resolved Hide resolved
packages/server/src/workers/index.ts Outdated Show resolved Hide resolved
packages/server/src/workers/index.ts Outdated Show resolved Hide resolved
packages/server/src/workers/scheduledtiming.test.ts Outdated Show resolved Hide resolved
queue = new Queue<ScheduledTimingJobData>(queueName, {
...defaultOptions,
defaultJobOptions: {
attempts: 3,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oy, retry policy. This seems like a very sensible default, and a good place to start 👍

I filed a separate ticket to track this: #1749

if (resource.cronTiming) {
cron = convertTimingToCron(resource.cronTiming);
if (!cron) {
logger.debug('cronTiming had the wrong format for a timed cron job');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good design question -- how can we provide feedback about invalid cron expressions.

I think this is an ok starting point, because we're still early in the cron development.

In the future, we may want to explore leveraging capabilities in ElementDefinition.constraint to make it a write-time check: https://hl7.org/fhir/elementdefinition-definitions.html#ElementDefinition.constraint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just filed a ticket to track this: #1751

packages/server/src/workers/scheduledtiming.ts Outdated Show resolved Hide resolved
packages/server/src/workers/scheduledtiming.ts Outdated Show resolved Hide resolved
packages/server/src/workers/scheduledtiming.ts Outdated Show resolved Hide resolved
Copy link
Member

@rahul1 rahul1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there! Just add an entry on the list on line 139 as well (to make sure readers don't miss this!

Edit: talked offline. Decision was to add an admonition

@sonarcloud
Copy link

sonarcloud bot commented Apr 3, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

88.9% 88.9% Coverage
0.0% 0.0% Duplication

Copy link
Member

@codyebberson codyebberson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@jamestouri jamestouri merged commit facf9e7 into main Apr 3, 2023
@jamestouri jamestouri deleted the james-bot-cron-job branch April 3, 2023 23:26
This was referenced Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bots Features and fixes related to bots and automation
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

5 participants