1- import schedule from "node-schedule" ;
2-
31import prisma from "@calcom/prisma" ;
42import { WebhookTriggerEvents } from "@calcom/prisma/enums" ;
53
@@ -9,45 +7,32 @@ export async function scheduleTrigger(
97 subscriber : { id : string ; appId : string | null }
108) {
119 try {
12- //schedule job to call subscriber url at the end of meeting
13- // FIXME: in-process scheduling - job will vanish on server crash / restart
14- const job = schedule . scheduleJob (
15- `${ subscriber . appId } _${ subscriber . id } ` ,
16- booking . endTime ,
17- async function ( ) {
18- const body = JSON . stringify ( { triggerEvent : WebhookTriggerEvents . MEETING_ENDED , ...booking } ) ;
19- await fetch ( subscriberUrl , {
20- method : "POST" ,
21- body,
22- } ) ;
10+ const payload = JSON . stringify ( { triggerEvent : WebhookTriggerEvents . MEETING_ENDED , ...booking } ) ;
11+ const jobName = `${ subscriber . appId } _${ subscriber . id } ` ;
2312
24- //remove scheduled job from bookings once triggered
25- const updatedScheduledJobs = booking . scheduledJobs . filter ( ( scheduledJob ) => {
26- return scheduledJob !== `${ subscriber . appId } _${ subscriber . id } ` ;
27- } ) ;
28-
29- await prisma . booking . update ( {
30- where : {
31- id : booking . id ,
32- } ,
33- data : {
34- scheduledJobs : updatedScheduledJobs ,
35- } ,
36- } ) ;
37- }
38- ) ;
13+ // add scheduled job to database
14+ const createTrigger = prisma . webhookScheduledTriggers . create ( {
15+ data : {
16+ jobName,
17+ payload,
18+ startAfter : booking . endTime ,
19+ subscriberUrl,
20+ } ,
21+ } ) ;
3922
4023 //add scheduled job name to booking
41- await prisma . booking . update ( {
24+ const updateBooking = prisma . booking . update ( {
4225 where : {
4326 id : booking . id ,
4427 } ,
4528 data : {
4629 scheduledJobs : {
47- push : job . name ,
30+ push : jobName ,
4831 } ,
4932 } ,
5033 } ) ;
34+
35+ await prisma . $transaction ( [ createTrigger , updateBooking ] ) ;
5136 } catch ( error ) {
5237 console . error ( "Error cancelling scheduled jobs" , error ) ;
5338 }
@@ -64,16 +49,20 @@ export async function cancelScheduledJobs(
6449 const promises = booking . scheduledJobs . map ( async ( scheduledJob ) => {
6550 if ( appId ) {
6651 if ( scheduledJob . startsWith ( appId ) ) {
67- if ( schedule . scheduledJobs [ scheduledJob ] ) {
68- schedule . scheduledJobs [ scheduledJob ] . cancel ( ) ;
69- }
52+ await prisma . webhookScheduledTriggers . deleteMany ( {
53+ where : {
54+ jobName : scheduledJob ,
55+ } ,
56+ } ) ;
7057 scheduledJobs = scheduledJobs ?. filter ( ( job ) => scheduledJob !== job ) || [ ] ;
7158 }
7259 } else {
7360 //if no specific appId given, delete all scheduled jobs of booking
74- if ( schedule . scheduledJobs [ scheduledJob ] ) {
75- schedule . scheduledJobs [ scheduledJob ] . cancel ( ) ;
76- }
61+ await prisma . webhookScheduledTriggers . deleteMany ( {
62+ where : {
63+ jobName : scheduledJob ,
64+ } ,
65+ } ) ;
7766 scheduledJobs = [ ] ;
7867 }
7968
0 commit comments