Morning. I'm looking for a way to manually trigger...
# ask-community
t
Morning. I'm looking for a way to manually trigger a Cached state from within a task, is it at all possible?
k
Hi @tash lai, could you give me a bit more details about what you’re trying to do?
t
@Kevin Kho well basically it's just a way to save memory
Copy code
@task
def download_and_process_big_json(url, mongo_url):
    client = MongoClient(mongo_url)
    db = client.get_default_database()
    data = db['big_json'].find_one({'url': url})
    if data:
        <set task state to Cached>
    else:
        data = requests.get(url).json()
        db['big_json'].insert_one({'url': url, **data})
    client.close()
    process_big_json(data)
So i want to simulate output caching using mongo but without actually returning anything from the task. Of course i can live without changing the task state to Cached and it will work fine but i kinda like to see if the data did already exist in the database via the prefect UI, you know Cached: 198 Success: 300 etc
k
Got it, you can try
raise signal_from_state(Cached())
. Link is for where
signal_from_state
is
t
tried that, it says something like "cannot find signal for state "Cached""
k
Ok will look more.
I think the best thing to do here is to
raise SUCCESS
with a custom message.
z
Hey @tash lai -- this doesn't look possible right now because the task runner coerces Cached states (which are Success subclasses) into Success states. I'm not quite sure what the actual mechanism is here so we'll have to look into it. I'll open an issue to track supporting this.
@Marvin open "Allow tasks to return
Success
state subclasses"