# Schedules

Info

Support for configuring schedules through UI and TF provider interfaces will be available in an upcoming release.

## Overview

The **Schedules** feature allows administrators to automate tasks around infrastructure deployment, infrastructure shut down, or custom workflow execution at the **Environment Template** level using cron expressions. Administrators can schedule tasks such as provisioning environments every Monday at 6:00 AM or destroying them every Friday at 10:00 PM. The default timezone is UTC unless a specific timezone is provided. Users have the flexibility to opt out of predefined schedules based on configurable rules and approval workflows.

Did you know ?

Schedule Jobs can be managed through various methods:

- [UI](https://docs.rafay.co/env_manager/templates/environment_templates/#schedules)
- [Terraform](https://registry.terraform.io/providers/RafaySystems/rafay/latest/docs/resources/environment_template)
- [API](https://docs.rafay.co/env_manager/templates/cron_jobs/#api-configuration-for-scheduling-tasks)
- [GitOps System Sync](https://docs.rafay.co/gitops/stages/system_sync/)
- [CLI](https://docs.rafay.co/env_manager/templates/cron_jobs/#config-specification)

### Limitations

- Users cannot override the schedule but can opt out if allowed
- Any changes made to schedules in draft template versions will not be automatically considered by all associated environments; users need to manually save or reapply the environment to reflect these changes

## API Configuration for Scheduling Tasks

Below are a few examples of an API configuration spec with three (3) schedule types - deploy / destroy / workflow execution:

### **_destroy_** - For destroying VM instances

- Schedule: "remove-instance"
- Description: Removes the specified virtual machine (VM) instance.
- Type: destroy  
  - Purpose: Indicates that the primary action is to delete or remove a specified resource, which in this case is a VM instance  
  - Action: Executes the destruction of the VM instance when triggered according to the configuration  
  - Context: Used for tasks that involve terminating or cleaning up resources that are no longer needed
- Cron Expression: "45 07 * * *": This schedule triggers every day at 7:45 AM UTC  
Opt-out Options: Allow Opt-out: Users can opt out of this task if needed.  
  - Max Allowed Duration: "24h" – Defines the maximum duration (24 hours) during which users can opt out of the scheduled task  
- Approval Process: Requires an internal approval task named "approval-task" managed by the "default-agent"

```
{
  "apiVersion": "eaas.envmgmt.io/v1",
  "kind": "EnvironmentTemplate",
  "metadata": {
    "name": "default-envtemplate",
    "description": "Default Environment Template",
    "createdBy": {
      "id": "user-id",
      "username": "user@example.com"
    },
    "modifiedBy": {
      "id": "user-id",
      "username": "user@example.com"
    }
  },
  "spec": {
    "version": "v1",
    "resources": [
      {
        "type": "dynamic",
        "kind": "resourcetemplate",
        "name": "default-resource",
        "resourceOptions": {
          "version": "v1"
        }
      }
    ],
    "schedules": [
      {
        "name": "remove-instance",
        "description": "Remove the specified virtual machine instance.",
        "type": "destroy",
        "cadence": {
          "cronExpression": "45 07 * * *"
        },
        "optOutOptions": {
          "allowOptOut": true,
          "maxAllowedDuration": "24h",
          "maxAllowedTimes": 3,
          "approval": {
            "tasks": [
              {
                "name": "approval-task",
                "type": "approval",
                "agents": [
                  {
                    "name": "default-agent"
                  }
                ],
                "options": {
                  "approval": {
                    "type": "internal"
                  }
                }
              }
            ]
          }
        }
      }
    ],
    "versionState": "draft"
  }
}
```

### **_workflows_** - For executing HTTP requests

- Schedule: "http-request-job"
- Description: Executes an HTTP request to a specified endpoint
- Type: workflows  
  - Purpose: Specifies that the schedule is for executing workflows, including tasks such as making HTTP requests  
  - Action: Performs the HTTP request to the given endpoint according to the schedule  
  - Context: Used for tasks that involve interaction with external services or systems via HTTP requests
- Cron Expression: "40 07 * * *": This schedule triggers every day at 7:40 AM UTC  
- Workflows: Executes an HTTP GET request to " [https://example.com/endpoint](https://example.com/endpoint)" via the "http-request-task" driver

```
{
  "apiVersion": "eaas.envmgmt.io/v1",
  "kind": "EnvironmentTemplate",
  "metadata": {
    "name": "default-envtemplate",
    "description": "Default Environment Template",
    "createdBy": {
      "id": "user-id",
      "username": "user@example.com"
    },
    "modifiedBy": {
      "id": "user-id",
      "username": "user@example.com"
    }
  },
  "spec": {
    "version": "v1",
    "resources": [
      {
        "type": "dynamic",
        "kind": "resourcetemplate",
        "name": "default-resource",
        "resourceOptions": {
          "version": "v1"
        }
      }
    ],
    "schedules": [
      {
        "name": "http-request-job",
        "description": "Executes an HTTP request to a specified endpoint.",
        "type": "workflows",
        "cadence": {
          "cronExpression": "40 07 * * *"
        },
        "workflows": {
          "tasks": [
            {
              "name": "http-request-task",
              "type": "driver",
              "driver": {
                "data": {
                  "config": {
                    "type": "http",
                    "http": {
                      "endpoint": "https://example.com/endpoint",
                      "method": "GET"
                    }
                  }
                }
              }
            }
          ]
        }
      }
    ],
    "versionState": "draft"
  }
}
```

### **_deploy_** - For Managing Node Pools

- Schedule: "destroy-nodepools"
- Description: Destroys all node pools except the default one
- Type: deploy  
  - Purpose: Indicates that the primary action is to perform deployment tasks, including the removal of specified resources (node pools)  
  - Action: Executes the deployment to remove node pools, excluding the default one, when triggered according to the configuration  
  - Context: Used for tasks that involve managing and modifying cluster resources
- Cron Expression: "55 11 18 7 *": This schedule triggers once a year on July 18th at 11:55 AM UTC  
- Opt-out Options: Users can opt out of this task for up to 24 hours, with a maximum of 3 allowed opt-outs

```
{
  "apiVersion": "eaas.envmgmt.io/v1",
  "kind": "EnvironmentTemplate",
  "metadata": {
    "name": "gke-np-envtemplate",
    "description": "GKE with node pool environment template",
    "projectID": "1ky4gkz"
  },
  "spec": {
    "version": "v1",
    "resources": [
      {
        "type": "dynamic",
        "kind": "resourcetemplate",
        "name": "gke-np-template",
        "resourceOptions": {
          "version": "v1"
        }
      }
    ],
    "schedules": [
      {
        "name": "deploy-nodepools",
        "description": "Deploy or update node pools except the default one.",
        "type": "deploy",
        "cadence": {
          "cronExpression": "55 11 18 7 *"
        },
        "optOutOptions": {
          "allowOptOut": true,
          "maxAllowedDuration": "24h",
          "maxAllowedTimes": 3
        },
        "context": {
          "data": {
            "variables": [
              {
                "name": "node_pools",
                "valueType": "json",
                "value": "[{\"disk_size_gb\":100,\"disk_type\":\"pd-standard\",\"image_type\":\"COS_CONTAINERD\",\"initial_node_count\":1,\"max_count\":100,\"min_count\":1,\"name\":\"inception-node-pool\",\"node_count\":3,\"node_locations\":\"us-central1-a,us-central1-b\"}]"
              }
            ]
          }
        }
      }
    ],
    "versionState": "draft"
  }
}
```

## Config Specification

The following Environment Template spec configures GCP resource management with scheduled tasks, such as destroying VM instances and executing HTTP requests. It includes cron-based scheduling, approval workflows, and opt-out options for flexible automation.

```
apiVersion: eaas.envmgmt.io/v1
kind: EnvironmentTemplate
metadata:
  name: gcp-envtemplate
  description: Environment Template for GCP Resources
spec:
  version: v1
  resources:
  - type: dynamic
    kind: resourcetemplate
    name: gcp-vpc-instance
    resourceOptions:
      version: v1
  schedules:
  - name: destroy-vm-instance
    description: Destroy the virtual machine instance
    type: destroy
    cadence:
      cronExpression: 45 11 * * *
      cronTimezone: IST
    optOutOptions:
      allowOptOut: true
      maxAllowedDuration: 24h
      maxAllowedTimes: 3
      approval:
        tasks:
        - name: mock-approval
          type: approval
          agents:
          - name: envmgr-agent-feb
          options:
            approval:
              type: internal
  - name: force-destroy-vm-instance
    description: Destroy the virtual machine instance
    type: destroy
    cadence:
      cronExpression: 0 0 * * 5
  - name: execute-http-request
    description: Execute Http Request
    type: workflows
    cadence:
      cronExpression: 40 07 * * *
    workflows:
      tasks:
      - name: mock-get-clientip
        type: driver
        driver:
          data:
            config:
              type: http
              http:
                endpoint: https://mocktarget.apigee.net/ip
                method: GET
  versionState: draft
```

### Staggered Cadence

The staggered field allows a scheduled job to be executed in a staggered manner across multiple targets instead of triggering all at once. This helps distribute the load and avoid spikes in resource consumption.

- `enabled`: Set to true to activate staggered execution for this schedule.
- `maxInterval`: Specifies the maximum interval (in minutes or seconds, e.g., 7m, 30s) between each staggered execution. The system ensures that all executions are distributed within this specified interval.

```
cadence:
  cronExpression: "*/5 * * * *"
  cronTimezone: "Europe/London"
  staggered:
    enabled: true
    maxInterval: "7m"
```
