Search code examples
ibm-cloud-infrastructure

Schedule Data format for AutoScale


I've got schedule data from Repeating Service. How can I parse the format as user friendly way? This is the sample schedule format. 0 13 ? * MON * Please let me know how I can get the definition of the schedule format.

Thank you.

            Repeating.Service rService = Repeating.service(client, trigger.getId());
            Repeating repeat = rService.getObjectForRepeating();

            System.out.println("repeat schedule : " + repeat.getSchedule());

Solution

  • The format of the schedule attribute is in Cron Format.

    schedule attribute:

    The cron-formatted schedule. This is run in the UTC timezone. Required For Create Type: string

    The next java script could help you.

    /**
    * This script retrieves the Repeating trigger policies for a SoftLayer_Scale_Policy, then 
    * it gets the schedule attribute and parses the value (Cron format) to human readable format. 
    *
    * Important manual pages:
    * @see http://sldn.softlayer.com/reference/services/SoftLayer_Scale_Policy/getRepeatingTriggers
    * @see http://sldn.softlayer.com/reference/datatypes/SoftLayer_Scale_Policy_Trigger_Repeating
    *
    * @license <http://sldn.softlayer.com/wiki/index.php/License>
    * @author SoftLayer Technologies, Inc. <[email protected]>
    */
    package SoftLayer_Java_Scripts.Examples;
    
    import com.softlayer.api.*;
    import com.softlayer.api.service.scale.Policy;
    import com.softlayer.api.service.scale.policy.trigger.Repeating;
    
    import java.util.Date;
    import java.util.List;
    
    import org.quartz.CronExpression;
    
    public class GetObjectScalePolicyTriggerRepeating
    {
        public static void main( String[] args )
        {
          // Fill with your valid data here.
          String user = "set me";
          String apiKey = "set me";
    
          long scalePolicyId = 1234;
    
          ApiClient client = new RestApiClient().withCredentials(user, apiKey);
    
          Policy.Service service = Policy.service(client, scalePolicyId);
    
          try
          {
            List<Repeating> result = service.getRepeatingTriggers();
            for(Repeating rep : result) {
              String cronValue = rep.getSchedule();
              System.out.println("Original value: " + cronValue);
              // Fixing format in order to display the schedule. 
              String patch = "* " + cronValue;
              // Using the next library: http://www.quartz-scheduler.org/api/2.2.1/org/quartz/CronExpression.html
              CronExpression cron = new CronExpression(patch);
              // Returns the next date/time after the given date/time which satisfies the cron expression.
              // (i.e. the next time it's going to be triggered)
              System.out.println("Parsed value: " + cron.getNextValidTimeAfter(new Date()));
            }
          }
          catch(Exception e)
          {
            System.out.println("Script failed, review the next message for further details: " + e); 
          }
        }
    }
    

    The next links contain information on cron format:

    http://www.nncron.ru/help/EN/working/cron-format.htm

    http://www.quartz-scheduler.org/api/2.2.1/org/quartz/CronExpression.html