Class: Hatchet::Features::Cron

Inherits:
Object
  • Object
show all
Defined in:
lib/hatchet/features/cron.rb

Overview

Cron client for managing cron workflow triggers within Hatchet

This class provides a high-level interface for creating, deleting, listing, and retrieving cron workflow triggers.

Examples:

Creating a cron trigger

cron = cron_client.create(
  workflow_name: "my-workflow",
  cron_name: "daily-run",
  expression: "0 0 * * *",
  input: { key: "value" },
  additional_metadata: { source: "api" }
)

Since:

  • 0.1.0

Constant Summary collapse

CRON_ALIASES =

Since:

  • 0.1.0

%w[@yearly @annually @monthly @weekly @daily @hourly].freeze

Instance Method Summary collapse

Constructor Details

#initialize(rest_client, config) ⇒ void

Initializes a new Cron client instance

Parameters:

  • rest_client (Object)

    The configured REST client for API communication

  • config (Hatchet::Config)

    The Hatchet configuration containing tenant_id and other settings

Since:

  • 0.1.0



29
30
31
32
33
34
# File 'lib/hatchet/features/cron.rb', line 29

def initialize(rest_client, config)
  @rest_client = rest_client
  @config = config
  @workflow_api = HatchetSdkRest::WorkflowApi.new(rest_client)
  @workflow_run_api = HatchetSdkRest::WorkflowRunApi.new(rest_client)
end

Instance Method Details

#create(workflow_name:, cron_name:, expression:, input: {}, additional_metadata: {}, priority: nil) ⇒ Object

Create a new workflow cron trigger

Examples:

cron = cron_client.create(
  workflow_name: "my-workflow",
  cron_name: "hourly-run",
  expression: "0 * * * *",
  input: { key: "value" },
  additional_metadata: { source: "api" }
)

Parameters:

  • workflow_name (String)

    The name of the workflow to trigger (namespace will be applied)

  • cron_name (String)

    The name of the cron trigger

  • expression (String)

    The cron expression defining the schedule

  • input (Hash) (defaults to: {})

    The input data for the cron workflow

  • additional_metadata (Hash) (defaults to: {})

    Additional metadata associated with the cron trigger

  • priority (Integer, nil) (defaults to: nil)

    The priority of the cron workflow trigger

Returns:

  • (Object)

    The created cron workflow instance

Raises:

Since:

  • 0.1.0



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hatchet/features/cron.rb', line 55

def create(workflow_name:, cron_name:, expression:, input: {}, additional_metadata: {}, priority: nil)
  validated_expression = validate_cron_expression(expression)

  request = HatchetSdkRest::CreateCronWorkflowTriggerRequest.new(
    cron_name: cron_name,
    cron_expression: validated_expression,
    input: input,
    additional_metadata: ,
    priority: priority,
  )

  @workflow_run_api.cron_workflow_trigger_create(
    @config.tenant_id,
    @config.apply_namespace(workflow_name),
    request,
  )
end

#delete(cron_id) ⇒ void

This method returns an undefined value.

Delete a workflow cron trigger

Examples:

cron_client.delete("cron-123")

Parameters:

  • cron_id (String)

    The ID of the cron trigger to delete

Raises:

Since:

  • 0.1.0



80
81
82
# File 'lib/hatchet/features/cron.rb', line 80

def delete(cron_id)
  @workflow_api.workflow_cron_delete(@config.tenant_id, cron_id.to_s)
end

#get(cron_id) ⇒ Object

Retrieve a specific workflow cron trigger by ID

Examples:

cron = cron_client.get("cron-123")

Parameters:

  • cron_id (String)

    The cron trigger ID to retrieve

Returns:

  • (Object)

    The requested cron workflow instance

Raises:

Since:

  • 0.1.0



122
123
124
# File 'lib/hatchet/features/cron.rb', line 122

def get(cron_id)
  @workflow_api.workflow_cron_get(@config.tenant_id, cron_id.to_s)
end

#list(offset: nil, limit: nil, workflow_id: nil, additional_metadata: nil, order_by_field: nil, order_by_direction: nil, workflow_name: nil, cron_name: nil) ⇒ Object

List cron workflow triggers matching the specified criteria

Examples:

crons = cron_client.list(limit: 10, workflow_name: "my-workflow")

Parameters:

  • offset (Integer, nil) (defaults to: nil)

    The offset to start the list from

  • limit (Integer, nil) (defaults to: nil)

    The maximum number of items to return

  • workflow_id (String, nil) (defaults to: nil)

    The ID of the workflow to filter by

  • additional_metadata (Hash, nil) (defaults to: nil)

    Filter by additional metadata keys

  • order_by_field (String, nil) (defaults to: nil)

    The field to order the list by

  • order_by_direction (String, nil) (defaults to: nil)

    The direction to order the list by

  • workflow_name (String, nil) (defaults to: nil)

    The name of the workflow to filter by

  • cron_name (String, nil) (defaults to: nil)

    The name of the cron trigger to filter by

Returns:

  • (Object)

    A list of cron workflows

Raises:

Since:

  • 0.1.0



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/hatchet/features/cron.rb', line 98

def list(offset: nil, limit: nil, workflow_id: nil, additional_metadata: nil,
         order_by_field: nil, order_by_direction: nil, workflow_name: nil, cron_name: nil)
  @workflow_api.cron_workflow_list(
    @config.tenant_id,
    {
      offset: offset,
      limit: limit,
      workflow_id: workflow_id,
      additional_metadata: (),
      order_by_field: order_by_field,
      order_by_direction: order_by_direction,
      workflow_name: workflow_name,
      cron_name: cron_name,
    },
  )
end