|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "hatchet-sdk" |
| 4 | + |
| 5 | +HATCHET = Hatchet::Client.new(debug: true) unless defined?(HATCHET) |
| 6 | + |
| 7 | +EVICTION_TTL_SECONDS = 5 |
| 8 | +LONG_SLEEP_SECONDS = 15 |
| 9 | +CAPACITY_SLEEP_SECONDS = 20 |
| 10 | +EVENT_KEY = "durable-eviction:event" |
| 11 | + |
| 12 | +EVICTION_POLICY = Hatchet::EvictionPolicy.new( |
| 13 | + ttl: EVICTION_TTL_SECONDS, |
| 14 | + allow_capacity_eviction: true, |
| 15 | + priority: 0, |
| 16 | +) |
| 17 | + |
| 18 | +CAPACITY_EVICTION_POLICY = Hatchet::EvictionPolicy.new( |
| 19 | + ttl: nil, |
| 20 | + allow_capacity_eviction: true, |
| 21 | + priority: 0, |
| 22 | +) |
| 23 | + |
| 24 | +NON_EVICTABLE_POLICY = Hatchet::EvictionPolicy.new( |
| 25 | + ttl: nil, |
| 26 | + allow_capacity_eviction: false, |
| 27 | + priority: 0, |
| 28 | +) |
| 29 | + |
| 30 | +CHILD_TASK = HATCHET.task(name: "child_task", execution_timeout: 60) do |_input, _ctx| |
| 31 | + sleep LONG_SLEEP_SECONDS |
| 32 | + { "child_status" => "completed" } |
| 33 | +end |
| 34 | + |
| 35 | +BULK_CHILD_TASK = HATCHET.task(name: "bulk_child_task", execution_timeout: 60) do |input, _ctx| |
| 36 | + sleep_for = (input["sleep_for"] || 0).to_i |
| 37 | + sleep sleep_for |
| 38 | + { "sleep_for" => sleep_for, "status" => "completed" } |
| 39 | +end |
| 40 | + |
| 41 | +EVICTABLE_SLEEP = HATCHET.durable_task( |
| 42 | + name: "evictable_sleep", |
| 43 | + execution_timeout: 300, |
| 44 | + eviction_policy: EVICTION_POLICY, |
| 45 | +) do |_input, ctx| |
| 46 | + ctx.sleep_for(duration: LONG_SLEEP_SECONDS) |
| 47 | + { "status" => "completed" } |
| 48 | +end |
| 49 | + |
| 50 | +EVICTABLE_WAIT_FOR_EVENT = HATCHET.durable_task( |
| 51 | + name: "evictable_wait_for_event", |
| 52 | + execution_timeout: 300, |
| 53 | + eviction_policy: EVICTION_POLICY, |
| 54 | +) do |_input, ctx| |
| 55 | + ctx.wait_for( |
| 56 | + EVENT_KEY, |
| 57 | + Hatchet::UserEventCondition.new(event_key: EVENT_KEY, expression: "true"), |
| 58 | + ) |
| 59 | + { "status" => "completed" } |
| 60 | +end |
| 61 | + |
| 62 | +EVICTABLE_CHILD_SPAWN = HATCHET.durable_task( |
| 63 | + name: "evictable_child_spawn", |
| 64 | + execution_timeout: 300, |
| 65 | + eviction_policy: EVICTION_POLICY, |
| 66 | +) do |_input, _ctx| |
| 67 | + child_result = CHILD_TASK.run |
| 68 | + { "child" => child_result, "status" => "completed" } |
| 69 | +end |
| 70 | + |
| 71 | +EVICTABLE_CHILD_BULK_SPAWN = HATCHET.durable_task( |
| 72 | + name: "evictable_child_bulk_spawn", |
| 73 | + execution_timeout: 300, |
| 74 | + eviction_policy: EVICTION_POLICY, |
| 75 | +) do |_input, _ctx| |
| 76 | + items = Array.new(3) do |i| |
| 77 | + BULK_CHILD_TASK.create_bulk_run_item( |
| 78 | + input: { "sleep_for" => (EVICTION_TTL_SECONDS + 5) * (i + 1) }, |
| 79 | + key: "child#{i}", |
| 80 | + ) |
| 81 | + end |
| 82 | + child_results = BULK_CHILD_TASK.run_many(items) |
| 83 | + { "child_results" => child_results } |
| 84 | +end |
| 85 | + |
| 86 | +MULTIPLE_EVICTION = HATCHET.durable_task( |
| 87 | + name: "multiple_eviction", |
| 88 | + execution_timeout: 300, |
| 89 | + eviction_policy: EVICTION_POLICY, |
| 90 | +) do |_input, ctx| |
| 91 | + ctx.sleep_for(duration: LONG_SLEEP_SECONDS) |
| 92 | + ctx.sleep_for(duration: LONG_SLEEP_SECONDS) |
| 93 | + { "status" => "completed" } |
| 94 | +end |
| 95 | + |
| 96 | +CAPACITY_EVICTABLE_SLEEP = HATCHET.durable_task( |
| 97 | + name: "capacity_evictable_sleep", |
| 98 | + execution_timeout: 300, |
| 99 | + eviction_policy: CAPACITY_EVICTION_POLICY, |
| 100 | +) do |_input, ctx| |
| 101 | + ctx.sleep_for(duration: CAPACITY_SLEEP_SECONDS) |
| 102 | + { "status" => "completed" } |
| 103 | +end |
| 104 | + |
| 105 | +NON_EVICTABLE_SLEEP = HATCHET.durable_task( |
| 106 | + name: "non_evictable_sleep", |
| 107 | + execution_timeout: 300, |
| 108 | + eviction_policy: NON_EVICTABLE_POLICY, |
| 109 | +) do |_input, ctx| |
| 110 | + ctx.sleep_for(duration: 10) |
| 111 | + { "status" => "completed" } |
| 112 | +end |
| 113 | + |
| 114 | +def main |
| 115 | + worker = HATCHET.worker( |
| 116 | + "eviction-worker", |
| 117 | + workflows: [ |
| 118 | + EVICTABLE_SLEEP, |
| 119 | + EVICTABLE_WAIT_FOR_EVENT, |
| 120 | + EVICTABLE_CHILD_SPAWN, |
| 121 | + EVICTABLE_CHILD_BULK_SPAWN, |
| 122 | + MULTIPLE_EVICTION, |
| 123 | + CAPACITY_EVICTABLE_SLEEP, |
| 124 | + NON_EVICTABLE_SLEEP, |
| 125 | + CHILD_TASK, |
| 126 | + BULK_CHILD_TASK, |
| 127 | + ], |
| 128 | + ) |
| 129 | + worker.start |
| 130 | +end |
| 131 | + |
| 132 | +main if __FILE__ == $PROGRAM_NAME |
0 commit comments