Skip to content

Pause and resume

Sessions can be paused to temporarily halt execution and save costs, then resumed later to continue work. This is particularly useful for long-running workflows, cost optimization during idle periods, or when you need to free up resources temporarily.

Benefits

  • 💰 Cost Savings: Reduce costs during pause periods
  • 🔄 Resource Management: Free up resources for other tasks while preserving session state
  • 🕐 Long-running Workflows: Pause sessions overnight or during weekends, resume when needed
  • 🛠️ Maintenance Windows: Safely pause sessions before scheduled maintenance
  • 📦 State Preservation: All session state and data are maintained across pause/resume cycles

Pausing Sessions

Pause a session to put it in a dormant state:

python
from agb import AGB

agb = AGB()
# Assume you have an active session

# Basic pause
pause_result = agb.pause(session)
if pause_result.success:
    print(f"Session paused successfully")
    print(f"Request ID: {pause_result.request_id}")
else:
    print(f"Failed to pause: {pause_result.error_message}")

Resuming Sessions

Resume a paused session to restore it to active state:

python
# Basic resume
resume_result = agb.resume(session)
if resume_result.success:
    print(f"Session resumed successfully")
    print(f"Status: {resume_result.status}")
else:
    print(f"Failed to resume: {resume_result.error_message}")

Custom Timeout and Polling

You can customize the timeout and polling interval for pause/resume operations:

python
# Pause with custom parameters
pause_result = agb.pause(
    session,
    timeout=300,        # Maximum wait time in seconds
    poll_interval=5.0   # Check status every 5 seconds
)

# Resume with custom parameters
resume_result = agb.resume(
    session,
    timeout=300,        # Maximum wait time in seconds
    poll_interval=5.0   # Check status every 5 seconds
)

Async Pause and Resume

For async applications, use the async versions:

python
import asyncio
from agb import AGB

async def pause_resume_async():
    agb = AGB()
    # Assume you have an active session

    # Async pause
    pause_result = await agb.pause_async(session)
    if pause_result.success:
        print("Session paused")

    # Async resume
    resume_result = await agb.resume_async(session)
    if resume_result.success:
        print("Session resumed")

# Run async function
asyncio.run(pause_resume_async())

Checking Session Status

After pause or resume operations, verify the session status:

python
# Get session status
get_result = agb.get_session(session.session_id)
if get_result.success and hasattr(get_result.data, 'status'):
    print(f"Current status: {get_result.data.status}")
    # Possible statuses: RUNNING, PAUSED, PAUSING, RESUMING

Complete Workflow Example

python
from agb import AGB
from agb.session_params import CreateSessionParams

agb = AGB()

# 1. Create session
params = CreateSessionParams(
    image_id="agb-linux-test-5",
    labels={"project": "batch-job", "cost-optimization": "enabled"}
)
result = agb.create(params)

if result.success:
    session = result.session
    # 2. Do some work
    session.command.execute_command("echo 'Processing data...'")

    # 3. Pause during idle period
    print("Pausing session to save costs...")
    pause_result = agb.pause(session)
    if pause_result.success:
        print("Session paused successfully")

    # 4. Resume when needed
    print("Resuming session...")
    resume_result = agb.resume(session, timeout=120)
    if resume_result.success:
        print("Session resumed, continuing work...")

    # 5. Continue work
    session.command.execute_command("echo 'Continuing processing...'")
else:
    print(f"Failed to create session: {result.error_message}")
    exit(1)

finally:
    # Clean up
    agb.delete(session)

Important Notes

  • State Preservation: All session state, files, and environment variables are preserved during pause
  • Paused Sessions and Timeouts: Paused sessions still count toward your session limits but incur reduced costs
  • Cannot Delete Paused Sessions: Currently, paused sessions cannot be deleted directly. Resume the session first, then delete it
  • Pause/Resume Cycles: You can pause and resume the same session multiple times
  • Network Interruption: If pause/resume is interrupted, the operation can be retried safely