Class: Fractor::Workflow::DependencyResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/fractor/workflow/execution/dependency_resolver.rb

Overview

Computes the execution order for workflow jobs using topological sort. Jobs are grouped into levels where all jobs in a level can be executed in parallel (their dependencies are satisfied).

Caches execution order based on job structure to avoid recomputing topological sort for static workflow definitions.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jobs, enable_cache: true) ⇒ DependencyResolver

Initialize the resolver with a workflow's jobs.

Parameters:

  • jobs (Hash)

    Hash of job_name => Job objects

  • enable_cache (Boolean) (defaults to: true)

    Whether to use cached execution order (default: true)



41
42
43
44
45
# File 'lib/fractor/workflow/execution/dependency_resolver.rb', line 41

def initialize(jobs, enable_cache: true)
  @jobs = jobs
  @enable_cache = enable_cache
  @signature = compute_signature if enable_cache
end

Class Attribute Details

.cacheObject (readonly)

Returns the value of attribute cache.



21
22
23
# File 'lib/fractor/workflow/execution/dependency_resolver.rb', line 21

def cache
  @cache
end

Class Method Details

.clear_cacheObject

Clear the entire execution order cache. Useful for testing or when workflows are dynamically modified.



25
26
27
# File 'lib/fractor/workflow/execution/dependency_resolver.rb', line 25

def clear_cache
  @mutex.synchronize { @cache.clear }
end

.clear_cache_for(workflow_signature) ⇒ Object

Clear cache entries for a specific workflow.

Parameters:

  • workflow_signature (String)

    The workflow signature to clear



32
33
34
# File 'lib/fractor/workflow/execution/dependency_resolver.rb', line 32

def clear_cache_for(workflow_signature)
  @mutex.synchronize { @cache.delete(workflow_signature) }
end

Instance Method Details

#execution_orderArray<Array<String>>

Compute the execution order using topological sort. Returns an array of arrays, where each inner array contains job names that can be executed in parallel (their dependencies are satisfied).

Results are cached based on the workflow's job structure (job names and their dependencies). This provides significant performance benefits for workflows that are executed multiple times.

Returns:

  • (Array<Array<String>>)

    Execution order as grouped job names



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fractor/workflow/execution/dependency_resolver.rb', line 56

def execution_order
  # Try to get from cache first
  if @enable_cache && @signature && cached_execution_order
    return cached_execution_order
  end

  # Compute the execution order
  order = compute_order

  # Cache the result
  cache_execution_order(order) if @enable_cache && @signature

  order
end

#invalidate_cacheObject

Invalidate the cache for this workflow's execution order. Call this if the workflow definition changes dynamically.



73
74
75
76
77
78
# File 'lib/fractor/workflow/execution/dependency_resolver.rb', line 73

def invalidate_cache
  return unless @enable_cache && @signature

  self.class.clear_cache_for(@signature)
  @cached = false
end