hk21702 / YA-GCal-Notion-Sync-Script

Script to synchronize between a Notion database and Google Calendar both ways. Uses Google App Script.
GNU General Public License v3.0
139 stars 10 forks source link

Duplicate Calendar Events in Notion #59

Closed cav212 closed 1 year ago

cav212 commented 1 year ago

I've got two triggers (#1) on time at 1 minute interval (#2) on calendar update. Sometimes when I change the time of the event on GCal, it creates another event in Notion with the same Event ID. What do you think is causing this?

hk21702 commented 1 year ago

Sounds like some weird stuff happening when two instances of the script are running at the same time? Can you check in your run history if there were infact two instances running at the same time, and if so, post the logs?

cav212 commented 1 year ago

Here's an example when I move the calendar event in Google Calendar and then I ran the script manually in order to get the execution log print out. It created a new database entry.

image

cav212 commented 1 year ago

Here's what is showing up in Notion. image

cav212 commented 1 year ago

Is this what you were looking for @hk21702 ? Thanks for your help!

hk21702 commented 1 year ago

Not exactly. On the left side, click the button that says my executions. It lists out all your previous runs, and why they were triggered. If you click on the execution you'll see the log. Show me all the logs of executions around when a duplication happened, and include the execution type in the ss.

You have a really low time period between auto runs for times events, so I'm suspecting a race condition when multiple instances are running at the same time, but I just wanna make sure.

cav212 commented 1 year ago

These events are when it just updated the "index zero" entry of the multiple Notion entries. image

image

image

image

cav212 commented 1 year ago

I just cleared the entire calendar of all events, then made a new event on GCal and it popped up on Notion. I dragged the GCal event on GCal to another time, and it created a duplicate event on Notion. Here are the executions from that: image

cav212 commented 1 year ago

This just happened after I changed the time of the same event in Notion and the subsequently changed on Time Trigger without making a duplicate event. image

hk21702 commented 1 year ago

Ok, I think I know what's going on. Your runs aren't happening at the same time, so I'm suspecting this right now. Basically, the script right now when checking for if an event already exists in Notion, asks for events that not only match the event ID, but also those that were last updated before a certain time. However, Notion's date property precision only goes down to the minute. So right now, you're executing the script within the same minute, so Notion isn't picking up on the events.

I will try and push an update to a testing branch in a moment. Hopefully it gets better with the fix I have in mind.

cav212 commented 1 year ago

Thanks!

hk21702 commented 1 year ago

Try out this branch: https://github.com/hk21702/YA-GCal-Notion-Sync-Script/tree/59-duplicate-calendar-events-in-notion

Only main.js had changes.

cav212 commented 1 year ago

Awesome! That fixed the duplicate issue! The only other issue I see right now occurs when I attempt to make more than 1 change to the time on my event entry in Notion, then GCal doesn't seem to sync - but I think it worked again when I changed the calendar event back to the original time and then changed it more than a minute after that sync'd. Any ideas? Thank you so much, this have been awesome!

cav212 commented 1 year ago

Also, I don't know if this is a clue or even a glitch at all, but in my execution log, it still says that a few executions of the script are still running from 30+ minutes ago. Is the status on the execution log just wrong? Is this going to eat into my runtime quota?

To clarify the last point, if I make a change in Notion within a minute of a recent sync of an inputted change in Notion, then the script never syncs from Notion to GCal, unless I change the Notion entry back to the original date/time, wait for it to sync, then change it again and it works.

hk21702 commented 1 year ago

Unfortunately the thing with the script not syncing from Notion to GCal when you make an edit right after the Notion entry was edited by the script is intended, albeit not ideal behavior. The script grabs a bunch of the most recently edited entries from the Notion database, but to make sure that it doesn't send a bunch of un-needed updates to GCal, it checks if the last time the entry was synced was before the last update. However, as I mentioned earlier, Notion rounds all their date/times down to the nearest minute. Even if the script instead of using the date object to store the last sync info and instead just used an integer like an unix timestamp, the last edited property of the entry is still rounded down to the nearest minute so it doesn't help much. If the script didn't do this, then it would end up having to do a lot more requests to GCal which can result in hitting rate limits.

Basically, you might just need to live with it, and don't expect things to sync to GCal if you make a change within the same minute of it having been updated on the Notion side already. If you want to force a sync, you can prob manually change the last synced time to be at least a minute before. Though really, all you truly need to do is just edit the entry again in the next minute, just so the last edited property ticks up and is no longer the same as the last sync date property.

As for the execution running for like absurd amounts of time, I'm not really sure whats going up with that. Usually Google Apps script doesn't let any script run longer than a few minutes in the first place so I don't know why its showing that instance has been running for 30+ mins. TBH, I would also recommend that you increase the time-intervals between syncs. 1 minute is really short and there's a good chance you'll hit a rate limit eventually.

cav212 commented 1 year ago

Thank you very much for all of your help!