Guides

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 expires
Converted
Activate paid plan
Not converted
Downgrade · send offer
1
use orcher::prelude::*;
2
use anyhow::Result;
3
use std::time::Duration;
4
5
#[derive(Debug, Clone, Serialize, Deserialize)]
6
struct TrialUser {
7
user_id: String,
8
email: String,
9
trial_days: u64,
10
plan: String,
11
}
12
13
#[derive(Debug, Clone, Serialize, Deserialize)]
14
struct TrialResult {
15
user_id: String,
16
converted: bool,
17
cancelled: bool,
18
reminders_sent: u32,
19
}
20
21
#[task]
22
async fn send_trial_reminder(user_id: String, stage: String) -> Result<()> {
23
// Send trial reminder email
24
Ok(())
25
}
26
27
#[task]
28
async fn check_user_status(user_id: String) -> Result<bool> {
29
// Check if user upgraded during trial
30
Ok(false)
31
}
32
33
#[task]
34
async fn downgrade_to_free(user_id: String) -> Result<()> {
35
// Downgrade user to free plan
36
Ok(())
37
}
38
39
// Trial period workflow with reminders
40
#[workflow(name = "trial_period", version = "1.0.0")]
41
pub 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);
46
47
let mut reminders_sent = 0;
48
let trial_duration = Duration::from_secs(user.trial_days * 86400);
49
50
// Send reminder at 50% of trial
51
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;
54
55
// Send reminder at 80% of trial
56
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;
59
60
// Send final reminder 1 day before end
61
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;
64
65
// Wait for remaining trial period
66
ctx.sleep(Duration::from_secs(86400)).await?;
67
68
// Check if user upgraded during trial
69
let upgraded: bool = ctx
70
.execute_task(check_user_status, user.user_id.clone())
71
.await?;
72
73
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?;
84
85
Ok(TrialResult {
86
user_id: user.user_id,
87
converted: false,
88
cancelled: false,
89
reminders_sent,
90
})
91
}
92
}

Next Steps