-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Usage of Sticky, Labels, and Durable (#152)
* Ensure Labels and Sticky are Usable within v2 Functions * Cursed Example * Fixes --------- Co-authored-by: srhinos <[email protected]>
- Loading branch information
Showing
7 changed files
with
126 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import asyncio | ||
|
||
from dotenv import load_dotenv | ||
|
||
from hatchet_sdk import Context, StickyStrategy, WorkerLabelComparator | ||
from hatchet_sdk.v2.callable import DurableContext | ||
from hatchet_sdk.v2.hatchet import Hatchet | ||
|
||
load_dotenv() | ||
|
||
hatchet = Hatchet(debug=True) | ||
|
||
|
||
@hatchet.durable( | ||
sticky=StickyStrategy.HARD, | ||
desired_worker_labels={ | ||
"running_workflow": { | ||
"value": "True", | ||
"required": True, | ||
"comparator": WorkerLabelComparator.NOT_EQUAL, | ||
}, | ||
}, | ||
) | ||
async def my_durable_func(context: DurableContext): | ||
try: | ||
ref = await context.aio.spawn_workflow( | ||
"StickyChildWorkflow", {}, options={"sticky": True} | ||
) | ||
result = await ref.result() | ||
except Exception as e: | ||
result = str(e) | ||
|
||
await context.worker.async_upsert_labels({"running_workflow": "False"}) | ||
return {"worker_result": result} | ||
|
||
|
||
@hatchet.workflow(on_events=["sticky:child"], sticky=StickyStrategy.HARD) | ||
class StickyChildWorkflow: | ||
@hatchet.step( | ||
desired_worker_labels={ | ||
"running_workflow": { | ||
"value": "True", | ||
"required": True, | ||
"comparator": WorkerLabelComparator.NOT_EQUAL, | ||
}, | ||
}, | ||
) | ||
async def child(self, context: Context): | ||
await context.worker.async_upsert_labels({"running_workflow": "True"}) | ||
|
||
print(f"Heavy work started on {context.worker.id()}") | ||
await asyncio.sleep(15) | ||
print(f"Finished Heavy work on {context.worker.id()}") | ||
|
||
return {"worker": context.worker.id()} | ||
|
||
|
||
def main(): | ||
|
||
worker = hatchet.worker( | ||
"sticky-worker", | ||
max_runs=10, | ||
labels={"running_workflow": "False"}, | ||
) | ||
|
||
worker.register_workflow(StickyChildWorkflow()) | ||
|
||
worker.start() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters