Scheduled Events

I am attempting to create an app with a scheduled sync of data pulls on a recurring schedule. I can get the handler to trigger when running locally, but I cannot get the event to run once the app is installed. I am just simply consoling the data so I can see when the app runs right now, nothing too crazy yet. The code snippet I have in Server.js is below:

createSchedule: async function(args) {
    console.log(args)
    try {
      let data = await $schedule.create({
        name: "ticket_reminder",
        data: {
          ticket_id: 10001
        },
        schedule_at: "2024-01-15T23:03:00.860Z",
        repeat: {
          time_unit: "minutes",
          frequency: 5
        }
      });
      console.log(data)
    }
    catch (error) {
      console.log(error)
    }
  },

  onScheduledEventHandler: function(payload) {
    console.log(payload)
    console.log(new Date);
    
  }

What am I missing?

HI @Kiska_Sanchez ,

Could you please let me know if you are trying to trigger the recursive schedule at a specific time or when the app is installed?

Based on the snippet, it appears to be a static timestamp. Could you please try the following snippet?

 createSchedule: async function(args) {
    console.log(args)

  const date = new Date();

    try {
      let data = await $schedule.create({
        name: "ticket_reminder",
        data: {
          ticket_id: 10001
        },
        schedule_at: date.toISOString(),
        repeat: {
          time_unit: "minutes",
          frequency: 5
        }
      });
      console.log(data)
    }
    catch (error) {
      console.log(error)
    }
  },

Hello @Anish,

I made the changes recommended and added 5 minutes to the scheduled at time to account for the fact that scheduled events must be scheduled in more than 5 mins from installation, however, it still did not trigger in the UI. This will be a recurring schedule end state. Just trying to get the schedule to trigger in the UI in general.

createSchedule: async function(args) {
    console.log(args)
    var currentDate = new Date();
    var schDate = new Date(currentDate.getTime() + 300000);
    // console.log(date.toISOString())
    try {
      let data = await $schedule.create({
        name: "ticket_reminder",
        data: {
          ticket_id: 10001
        },
        schedule_at:  schDate.toISOString(),
        repeat: {
          time_unit: "minutes",
          frequency: 5
        }
      }); 
      console.log(data)
    }
    catch (error) {
      console.log(error)
    }
  }

If your purpose is only to run the scheduler,

As mentioned, the scheduler would have been triggered. Since schedulers are used in the serverless application, you can find the logs in the “View Log” section.

Click on “Settings” and then select “View Log.” Kindly refer to the screenshots below for reference.

image

Hey @Kiska_Sanchez,

Like @Anish mentioned this is a serverless event and whenever the scheduler is triggered you would find a log of the console.log(payload) as your handler function is using it.

Let me know if you can see the logs or if you are having any trouble.

Hello @Anish and @zach_jones_noel,

The issue is that there is not a payload in the logs at all, usually I can receive the args or payloads in the logs with other handlers but not this one. With the code above this is this logs for the app as of this morning (the console should be happening every 5 mins based on the code above):

Again, I can get it to trigger when hosted locally, but I cannot get anything to trigger when the app is uploaded. Why is this not working for me? Am I missing something?

Here is my manifest.json

{
  "platform-version": "2.3",
  "product": {
    "freshservice": {
      "events": {
        "onTicketCreate": {
          "handler": "onTicketCreateHandler"
        },
        "onScheduledEvent": {
          "handler": "onScheduledEventHandler"
        }
      }
    }
  },
  "engines": {
    "node": "18.17.1",
    "fdk": "9.0.7"
  }
}

Hi @Kiska_Sanchez

I hope this topic will help you.

Hi @Kiska_Sanchez ,

Could you please try adding 6 minutes from the event is triggered

const date = new Date();
date.setMinutes(date.getMinutes() + 6);

Hey @Kiska_Sanchez,

Can you confirm if the scheduled event is getting created when you install the app, and also can you share the snippet which is invoking createSchedule method?

Hello,

I was working off the assumption the create schedule needed to be it’s own handler. I added this to my code and it resolved the issue. Thank you!

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.