Trial Period Management
Trial Period Management
SaaS trial periods are a natural fit for long-running workflows. The workflow sleeps through the trial duration, waking at key milestones to send reminder emails, and at the end checks whether the user has converted to a paid plan or needs to be downgraded.
14-Day Trial Workflowper user
Day 0
Start trialProvision account · enable features💤sleep(3 days)
Day 3
Engagement checkHas user created a workflow?💤sleep(4 days)
Day 7
Midpoint reminderSend tips email · 7 days left💤sleep(5 days)
Day 12
Urgency reminderSend upgrade prompt · 2 days left💤sleep(2 days)
Day 14
Trial expiresConverted
Activate paid plan
Not converted
Downgrade · send offer
1use orcher::prelude::*;2use anyhow::Result;3use std::time::Duration;45#[derive(Debug, Clone, Serialize, Deserialize)]6struct TrialUser {7 user_id: String,8 email: String,9 trial_days: u64,10 plan: String,11}1213#[derive(Debug, Clone, Serialize, Deserialize)]14struct TrialResult {15 user_id: String,16 converted: bool,17 cancelled: bool,18 reminders_sent: u32,19}2021#[task]22async fn send_trial_reminder(user_id: String, stage: String) -> Result<()> {23 // Send trial reminder email24 Ok(())25}2627#[task]28async fn check_user_status(user_id: String) -> Result<bool> {29 // Check if user upgraded during trial30 Ok(false)31}3233#[task]34async fn downgrade_to_free(user_id: String) -> Result<()> {35 // Downgrade user to free plan36 Ok(())37}3839// Trial period workflow with reminders40#[workflow(name = "trial_period", version = "1.0.0")]41pub async fn trial_period(42 ctx: WorkflowContext,43 user: TrialUser,44) -> Result<TrialResult> {45 println!("Trial started for user {}: {} days", user.user_id, user.trial_days);4647 let mut reminders_sent = 0;48 let trial_duration = Duration::from_secs(user.trial_days * 86400);4950 // Send reminder at 50% of trial51 ctx.sleep(trial_duration / 2).await?;52 ctx.execute_task(send_trial_reminder, (user.user_id.clone(), "halfway".to_string())).await?;53 reminders_sent += 1;5455 // Send reminder at 80% of trial56 ctx.sleep(trial_duration * 3 / 10).await?;57 ctx.execute_task(send_trial_reminder, (user.user_id.clone(), "ending_soon".to_string())).await?;58 reminders_sent += 1;5960 // Send final reminder 1 day before end61 ctx.sleep(trial_duration / 10 - Duration::from_secs(86400)).await?;62 ctx.execute_task(send_trial_reminder, (user.user_id.clone(), "last_day".to_string())).await?;63 reminders_sent += 1;6465 // Wait for remaining trial period66 ctx.sleep(Duration::from_secs(86400)).await?;6768 // Check if user upgraded during trial69 let upgraded: bool = ctx70 .execute_task(check_user_status, user.user_id.clone())71 .await?;7273 if upgraded {74 println!("User {} converted to paid plan", user.user_id);75 Ok(TrialResult {76 user_id: user.user_id,77 converted: true,78 cancelled: false,79 reminders_sent,80 })81 } else {82 println!("Trial expired for user {} - downgrading to free plan", user.user_id);83 ctx.execute_task(downgrade_to_free, user.user_id.clone()).await?;8485 Ok(TrialResult {86 user_id: user.user_id,87 converted: false,88 cancelled: false,89 reminders_sent,90 })91 }92}