Scheduled Script Proxy API Calls
-
Hi--I added a scriptProxy that calls scheduleRunScriptUTC. It's working fine but it's generating over 12k API calls per day. Is there a more efficient way to schedule a daily or weekly script execution?
-
The API calls should come from your script, not from the scheduled task itself, right? Here’s an example of how to set up a daily execution script for your reference -- https://github.com/getbraincloud/examples-unity/tree/master/Invaders#schedule-a-cloud-code-script-to-protect-replays-from-deletion-for-top-10-and-featured-players
-
The only change I made to the script was to add this so it will run every night, and API usage went from about 3,400 API calls per month to ~350k per month. var scriptProxy = bridge.getScriptServiceProxy();
var scriptName = "tournaments/SeedDailyLeaderboard";
var scriptData = {};
var midnightPSTinUTC = 1759561200000;
scriptProxy.scheduleRunScriptUTC(scriptName, scriptData, midnightPSTinUTC); -
@larrybiely over 12K API calls a day? Hmm - I'll take a look at that script...
-
Here it is in its entirety:
"use strict"; function main() { //Schedule Nightly var scriptProxy = bridge.getScriptServiceProxy(); var scriptName = "tournaments/SeedDailyLeaderboard"; var scriptData = {}; var midnightPSTinUTC = 1759561200000; // Calculate for your target date scriptProxy.scheduleRunScriptUTC(scriptName, scriptData, midnightPSTinUTC); //end schedule const profileIds = [ "b7d353e8-3bef-4e01-9d42-a92e31d076c1", "4bfd2215-45bb-4ad0-96f9-97fa487926a7", "841abac7-86a5-4f31-8e33-0da986e42216", "75d97f20-fd76-46a9-aae1-70398bdc590e", "655d7882-5bee-44b0-aeb9-3a9cf404858e", "ef5061f7-2086-465e-bfb2-8df8bdc30b0d", "817560b6-6a87-454d-aef9-c7c3f7eb7963", "56570e93-8912-4d24-8289-9046faa6fdb2", "4180d878-256f-4ea1-a26b-65ff757d962b", "84d5509c-14c7-4e1f-8fd0-6a3ba432af17", "2bc2dd6b-3e0f-45d0-bea6-13bfc01bbbc4", "b6ad5fa9-ccf5-43be-ab6b-b44eeec02d90", "792712e6-efc0-4310-9f12-7abbcdab1bb1" ]; function generateRandomScore() { const min = 300; const max = 1250; const randomNum = Math.floor(Math.random() * (max - min + 1)) + min; return randomNum * 100; } let results = { success: [], errors: [] }; // Process one profile at a time let currentIndex = 0; function processNextProfile() { if (currentIndex >= profileIds.length) { return results; } const profileId = profileIds[currentIndex]; const score = generateRandomScore(); // Generate new score for each profile try { const otherSession = bridge.getSessionForProfile(profileId); //const lbProxy = bridge.getLeaderboardServiceProxy(otherSession); const tournamentProxy = bridge.getTournamentServiceProxy(otherSession); var postResult = tournamentProxy.joinTournament("DailyHighScore", "DailyHighScore", score); // lbProxy.postScoreToLeaderboard( // "WeeklyHighScore", // score, // {} // ); // Wait for the first operation to complete before starting the next //bridge.callAPI("Leaderboard", "POST_SCORE", { // leaderboardId: "WeeklyHighScore", // score: score, // data: {} //}); results.success.push({ profileId: profileId, score: score }); } catch (error) { results.errors.push({ profileId: profileId, error: error.message || "Unknown error" }); } currentIndex++; return processNextProfile(); } return processNextProfile(); } main();
-
As I mentioned in the support chat - the issue is that
midnightPSTinUTC
isn't dynamically calculated for midnight today -- it's midnight PST on Oct 4th. [i.e. over a week ago].What's happening is the call is accepting that past date - which puts the script in the front of our queue to be run ('cause we think we're late!) - and it gets run in the next 60 second job interval. And then reschedules itself in the past again.
So basically this script is running roughly 60*24 = 1440 times a day.
Anyway - I'll have the devs look into it - we should return an error if you are scheduling the script too far into the past... [i.e. a second or two in the past is probably fine - and best to do in case the scheduling script has delays of some sort... but clearly 10 days in the past is too far! Probably 60 mins in the past is too far!
]