Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Solved
  • Unsolved
  • Users
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Darkly)
  • No Skin
Collapse
brainCloud Forums
  1. Home
  2. Cloud Code
  3. Recurring scheduled tasks?

Recurring scheduled tasks?

Scheduled Pinned Locked Moved Unsolved Cloud Code
6 Posts 4 Posters 2.0k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    larrybiely
    wrote on last edited by
    #1

    I have a scheduled job that I set up in the Job Queue and it runs as expected, but is there a way to schedule it so that it runs weekly? Or tie it to a tournament so that it runs when the tournament starts?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JasonL
      bitHeads
      wrote on last edited by
      #2

      Here is an example of scheduling a weekly cloud code execution,

      "use strict";
      
      function main() {
          var response = {}
      
          bridge.logDebugJson("Script Inputs", data)
      
          const scriptName = data.scriptName
          const interval = data.args.interval
          const searchSpan = interval < 60 ? 60 : 60 * 24
      
          // schedule checking and re-schedule itself for the next week at 23:59 on the same day
          var scriptProxy = bridge.getScriptServiceProxy()
      
          var dateTimeSpanMinsFromNowInMillis = new Date().getTime() + (searchSpan * 60 * 1000)
          var result = scriptProxy.getScheduledCloudScripts(dateTimeSpanMinsFromNowInMillis)
      
          var nowTime = new Date().getTime()
      
          if ((result.status == 200) && (result.data !== null)) {
              for (var i = 0; i < result.data.scheduledJobs.length; i++) {
                  if (result.data.scheduledJobs[i].scriptName === scriptName && result.data.scheduledJobs[i].scheduledStartTime > nowTime) {
                      scriptProxy.cancelScheduledScript(result.data.scheduledJobs[i].jobId)
                  }
              }
          }
      
          const targetHour = 23
          const targetMinute = 59
          const minutesDifference = getMinutesDifference(targetHour, targetMinute)
          let minutesFromNow = minutesDifference + interval
          bridge.logInfo(`minutesFromNow between the script calling time and the time of 23:59 on a same day${minutesFromNow}`)
          response.scheduleJob = scriptProxy.scheduleRunScriptMinutes(scriptName, data, minutesFromNow)
      
          // the code for your script 
      
          return response
      }
      
      function getMinutesDifference(targetHour, targetMinute) {
          const now = new Date()
          const targetTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), targetHour, targetMinute)
          const differenceInMilliseconds = targetTime - now
          bridge.logInfo(`differenceInMilliseconds between the script calling time and the target time: ${differenceInMilliseconds}`)
          const differenceInMinutes = Math.floor(differenceInMilliseconds / (1000 * 60))
          return differenceInMinutes
      }
      
      main()
      

      The parameters for this script will be like below, the interval will be 10080 minutes (a week),

      {
        "scriptName": "thisScriptNameItself",
        "args": {
          "interval": 10080
        }
      }
      
      1 Reply Last reply
      0
      • L Offline
        L Offline
        larrybiely
        wrote on last edited by
        #3

        Thanks. So I create the script and it just runs? Do I need to do any other configuration or set up?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JasonL
          bitHeads
          wrote on last edited by
          #4

          No, after modifying the desired running time and adding your code, simply run this script without any further configuration.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            antony
            wrote on last edited by
            #5

            Thanks for the example, this might prove helpful for me too. But I do feel the API is lacking the ability to hook a script to the start and end of tournaments. It'd be nice to be able to extend the functionality of what happens when a tournament ends and rewards are paid out for example.

            1 Reply Last reply
            0
            • Paul WinterhalderP Offline
              Paul WinterhalderP Offline
              Paul Winterhalder
              brainCloudAdmin
              wrote on last edited by
              #6

              We agree with that @antony - we'll get that request to the devs.

              1 Reply Last reply
              0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Solved
              • Unsolved
              • Users