Skip to content

Commit

Permalink
chore: More processRecurrence tracing (#9736)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dschoordsch committed May 8, 2024
1 parent 7c75eb1 commit 881546c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 70 deletions.
32 changes: 17 additions & 15 deletions packages/server/dataloader/customLoaderMakers.ts
@@ -1,4 +1,5 @@
import DataLoader from 'dataloader'
import tracer from 'dd-trace'
import {Selectable, SqlBool, sql} from 'kysely'
import {PARABOL_AI_USER_ID} from '../../client/utils/constants'
import getRethink, {RethinkSchema} from '../database/rethinkDriver'
Expand Down Expand Up @@ -630,21 +631,22 @@ export const activeMeetingsByMeetingSeriesId = (parent: RootDataLoader) => {

export const lastMeetingByMeetingSeriesId = (parent: RootDataLoader) => {
return new DataLoader<number, AnyMeeting | null, string>(
async (keys) => {
const r = await getRethink()
const res = await (
r
.table('NewMeeting')
.getAll(r.args(keys), {index: 'meetingSeriesId'})
.group('meetingSeriesId') as RDatum
)
.orderBy(r.desc('createdAt'))
.nth(0)
.default(null)
.ungroup()('reduction')
.run()
return normalizeResults(keys, res as AnyMeeting[], 'meetingSeriesId')
},
async (keys) =>
tracer.trace('lastMeetingByMeetingSeriesId', async () => {
const r = await getRethink()
const res = await (
r
.table('NewMeeting')
.getAll(r.args(keys), {index: 'meetingSeriesId'})
.group('meetingSeriesId') as RDatum
)
.orderBy(r.desc('createdAt'))
.nth(0)
.default(null)
.ungroup()('reduction')
.run()
return normalizeResults(keys, res as AnyMeeting[], 'meetingSeriesId')
}),
{
...parent.dataLoaderOptions
}
Expand Down
110 changes: 55 additions & 55 deletions packages/server/graphql/private/mutations/processRecurrence.ts
Expand Up @@ -158,67 +158,67 @@ const processRecurrence: MutationResolvers['processRecurrence'] = async (_source
)
await tracer.trace('processRecurrence.startActiveMeetingSeries', async () =>
Promise.allSettled(
activeMeetingSeries.map(async (meetingSeries) =>
tracer.trace('processRecurrence.startActiveMeetingSeries.meeting', async (span) => {
span?.setTag('meetingSeriesId', meetingSeries.id)
const seriesTeam = await dataLoader.get('teams').loadNonNull(meetingSeries.teamId)
if (seriesTeam.isArchived || !seriesTeam.isPaid) {
return
}

const [seriesOrg, lastMeeting] = await Promise.all([
dataLoader.get('organizations').load(seriesTeam.orgId),
dataLoader.get('lastMeetingByMeetingSeriesId').load(meetingSeries.id)
])

// remove this check after 2024-05-05
if (
lastMeeting?.meetingSeriesId !== meetingSeries.id ||
lastMeeting.teamId !== meetingSeries.teamId
) {
const error = new Error(
'lastMeetingByMeetingSeriesId returned a meeting that does not match the series'
)
sendToSentry(error)
throw error
}

if (seriesOrg.lockedAt) {
return
}

// For meetings that should still be active, start the meeting and set its end time.
// Any subscriptions are handled by the shared meeting start code
const rrule = RRule.fromString(meetingSeries.recurrenceRule)
// technically, RRULE should never return NaN here but there's a bug in the library
// https://github.com/jakubroztocil/rrule/issues/321
if (isNaN(rrule.options.interval)) {
return
}

// Only get meetings that should currently be active, i.e. meetings that should have started
// within the last 24 hours, started after the last meeting in the series, and started before
// 'now'.
const fromDate = lastMeeting
? new Date(
Math.max(lastMeeting.createdAt.getTime() + ms('10m'), now.getTime() - ms('24h'))
)
: new Date(0)
const newMeetingsStartTimes = rrule.between(
getRRuleDateFromJSDate(fromDate),
getRRuleDateFromJSDate(now)
activeMeetingSeries.map(async (meetingSeries) => {
const seriesTeam = await dataLoader.get('teams').loadNonNull(meetingSeries.teamId)
if (seriesTeam.isArchived || !seriesTeam.isPaid) {
return
}

const [seriesOrg, lastMeeting] = await Promise.all([
dataLoader.get('organizations').load(seriesTeam.orgId),
dataLoader.get('lastMeetingByMeetingSeriesId').load(meetingSeries.id)
])

// remove this check after 2024-05-05
if (
lastMeeting?.meetingSeriesId !== meetingSeries.id ||
lastMeeting.teamId !== meetingSeries.teamId
) {
const error = new Error(
'lastMeetingByMeetingSeriesId returned a meeting that does not match the series'
)
for (const startTime of newMeetingsStartTimes) {
const err = await startRecurringMeeting(
sendToSentry(error)
throw error
}

if (seriesOrg.lockedAt) {
return
}

// For meetings that should still be active, start the meeting and set its end time.
// Any subscriptions are handled by the shared meeting start code
const rrule = RRule.fromString(meetingSeries.recurrenceRule)
// technically, RRULE should never return NaN here but there's a bug in the library
// https://github.com/jakubroztocil/rrule/issues/321
if (isNaN(rrule.options.interval)) {
return
}

// Only get meetings that should currently be active, i.e. meetings that should have started
// within the last 24 hours, started after the last meeting in the series, and started before
// 'now'.
const fromDate = lastMeeting
? new Date(
Math.max(lastMeeting.createdAt.getTime() + ms('10m'), now.getTime() - ms('24h'))
)
: new Date(0)
const newMeetingsStartTimes = rrule.between(
getRRuleDateFromJSDate(fromDate),
getRRuleDateFromJSDate(now)
)
for (const startTime of newMeetingsStartTimes) {
const err = await tracer.trace('startRecurringMeeting', async (span) => {
span?.addTags({meetingSeriesId: meetingSeries.id})
return startRecurringMeeting(
meetingSeries,
getJSDateFromRRuleDate(startTime),
dataLoader,
subOptions
)
if (!err) meetingsStarted++
}
})
)
})
if (!err) meetingsStarted++
}
})
)
)

Expand Down

0 comments on commit 881546c

Please sign in to comment.