Background tasks not finishing

I am running a server code as a background task. If the user navigates or logs out of the app before it finishes. Then the task doesn’t complete. I thought that background tasks would keep running. Maybe I am wrong. Any help would be great.

A background task doesn’t know what’s going on on the client side. It is impossible for a background task to be affected by it.

And a background task doesn’t care about users. It can work without problems in an app without user service.

You need to focus on the background task and figure out what’s going on.

What do you mean by “the task doesn’t complete”?

So I have a couple server calls.

  1. Create a pdf.
  2. Store the pdf to a data table.
  3. Email a copy of the pdf.
  4. Delete all items in the orders data table that are connected to the user.
  5. Notification your order has been submitted.

Number 4 is the one that doesn’t complete if I close the page before it finishes. I would go back in and go to the cart and the items would still be there.

So is #4 done in a background task or just a normal server call from the client? You never answered @stefano.menci’s question about that. True background tasks know nothing about the client’s state.

I am just loading it up to take a snippet of the code.

this is the server code.
@anvil.server.background_task
def delete_items(user_name):
user = anvil.users.get_user()
if user is not None:
user_name = user[‘name’]

    requested_items = app_tables.requested_items.search(User=user_name)
    reorder_list = app_tables.reorder_list
    
    for requested_item in requested_items:
        description = requested_item['Requseted_Description']
        item_number = requested_item['Requested_Item_Number']
        quantity = requested_item['Requested_Quantity']
        cost = requested_item['Requested_Cost']
        color = requested_item['Requested_Color']
        year = requested_item['Requested_Year']
        subtotal = requested_item['Requested_Subtotal']
        inventory_item = app_tables.inventory.get(Item_number=item_number)
        
        if inventory_item is not None:
            # Add a new row to reorder_list
            reorder_list.add_row(
                Description=description,
                Item_Number=item_number,
                Quantity=quantity,
                Cost="${:.2f}".format(float(cost)),
                Subtotal=subtotal,
                Color=color,
                Year=year
            )
            
            # Delete the requested item from the requested_items table
            requested_item.delete()
    
    # Delete only the user's order rows from the orders table
    rows = app_tables.orders.search(User=user_name)
    for row in rows:
        item_number = row['Item_Number']
        quantity = row['Quantity']
        cost = row['Cost']
        row.delete()

@anvil.server.callable
def delete_ordered_items(user):
anvil.server.launch_background_task(‘delete_items’, user[‘name’])

It could be that the background task is never getting launched if the server function that starts it never starts. Try adding a print statement before the line launching the background task to see if it has even made it that far.

thanks i will try that.

Try printing something before and after each step, then look at the logs

1 Like

2 posts were split to a new topic: Help with drop down

3 posts were split to a new topic: Multi language app