1+ // Copyright (c) Microsoft Corporation.
2+ // Licensed under the MIT License.
3+ using Microsoft . DurableTask ;
4+ using Microsoft . DurableTask . Client ;
5+ using Microsoft . DurableTask . Client . AzureManaged ;
6+ using Microsoft . DurableTask . ScheduledTasks ;
7+ using Microsoft . DurableTask . Worker ;
8+ using Microsoft . DurableTask . Worker . AzureManaged ;
9+ using Microsoft . Extensions . DependencyInjection ;
10+ using Microsoft . Extensions . Hosting ;
11+ using Microsoft . Extensions . Logging ;
12+ using ScheduleConsoleApp . Activities ;
13+
14+
15+ // Create the host builder
16+ IHost host = Host . CreateDefaultBuilder ( args )
17+ . ConfigureServices ( services =>
18+ {
19+ string connectionString = Environment . GetEnvironmentVariable ( "DURABLE_TASK_SCHEDULER_CONNECTION_STRING" )
20+ ?? throw new InvalidOperationException ( "Missing required environment variable 'DURABLE_TASK_SCHEDULER_CONNECTION_STRING'" ) ;
21+
22+ // Configure the worker
23+ _ = services . AddDurableTaskWorker ( builder =>
24+ {
25+ // Add the Schedule entity and demo orchestration
26+ builder . AddTasks ( r =>
27+ {
28+ // Add the orchestrator class
29+ r . AddOrchestrator < StockPriceOrchestrator > ( ) ;
30+
31+ // Add required activities
32+ r . AddActivity < GetStockPrice > ( ) ;
33+ } ) ;
34+
35+ // Enable scheduled tasks support
36+ builder . UseDurableTaskScheduler ( connectionString ) ;
37+ builder . EnableScheduledTasksSupport ( ) ;
38+ } ) ;
39+
40+ // Configure the client
41+ services . AddDurableTaskClient ( builder =>
42+ {
43+ builder . UseDurableTaskScheduler ( connectionString ) ;
44+ builder . EnableScheduledTasksSupport ( ) ;
45+ } ) ;
46+
47+ // Configure console logging
48+ services . AddLogging ( logging =>
49+ {
50+ logging . AddSimpleConsole ( options =>
51+ {
52+ options . SingleLine = true ;
53+ options . UseUtcTimestamp = true ;
54+ options . TimestampFormat = "yyyy-MM-ddTHH:mm:ss.fffZ " ;
55+ } ) ;
56+ } ) ;
57+ } )
58+ . Build ( ) ;
59+
60+ await host . StartAsync ( ) ;
61+
62+ IScheduledTaskClient scheduledTaskClient = host . Services . GetRequiredService < IScheduledTaskClient > ( ) ;
63+
64+ try
65+ {
66+ // list all schedules
67+ // Define the initial query with the desired page size
68+ ScheduleQuery query = new ScheduleQuery { PageSize = 100 } ;
69+
70+ // Retrieve the pageable collection of schedule IDs
71+ AsyncPageable < string > schedules = await scheduledTaskClient . ListScheduleIdsAsync ( query ) ;
72+
73+ // Initialize the continuation token
74+ string ? continuationToken = null ;
75+ await foreach ( Page < string > page in schedules . AsPages ( continuationToken ) )
76+ {
77+ foreach ( string scheduleId in page . Values )
78+ {
79+ // Obtain the schedule handle for the current scheduleId
80+ IScheduleHandle handle = scheduledTaskClient . GetScheduleHandle ( scheduleId ) ;
81+
82+ // Delete the schedule
83+ await handle . DeleteAsync ( ) ;
84+
85+ Console . WriteLine ( $ "Deleted schedule { scheduleId } ") ;
86+ }
87+
88+ // Update the continuation token for the next iteration
89+ continuationToken = page . ContinuationToken ;
90+
91+ // If there's no continuation token, we've reached the end of the collection
92+ if ( continuationToken == null )
93+ {
94+ break ;
95+ }
96+ }
97+
98+
99+ // Create schedule options that runs every 4 seconds
100+ ScheduleCreationOptions scheduleOptions = new ScheduleCreationOptions ( "demo-schedule101" , nameof ( StockPriceOrchestrator ) , TimeSpan . FromSeconds ( 4 ) )
101+ {
102+ StartAt = DateTimeOffset . UtcNow ,
103+ OrchestrationInput = "MSFT"
104+ } ;
105+
106+ // Create the schedule and get a handle to it
107+ ScheduleHandle scheduleHandle = await scheduledTaskClient . CreateScheduleAsync ( scheduleOptions ) ;
108+
109+ // Get the schedule description
110+ ScheduleDescription scheduleDescription = await scheduleHandle . DescribeAsync ( ) ;
111+
112+ // print the schedule description
113+ Console . WriteLine ( scheduleDescription . ToJsonString ( true ) ) ;
114+
115+ Console . WriteLine ( "" ) ;
116+ Console . WriteLine ( "" ) ;
117+ Console . WriteLine ( "" ) ;
118+
119+ // Pause the schedule
120+ Console . WriteLine ( "\n Pausing schedule..." ) ;
121+ await scheduleHandle . PauseAsync ( ) ;
122+ scheduleDescription = await scheduleHandle . DescribeAsync ( ) ;
123+ Console . WriteLine ( scheduleDescription . ToJsonString ( true ) ) ;
124+ Console . WriteLine ( "" ) ;
125+ Console . WriteLine ( "" ) ;
126+ Console . WriteLine ( "" ) ;
127+
128+
129+ // Resume the schedule
130+ Console . WriteLine ( "\n Resuming schedule..." ) ;
131+ await scheduleHandle . ResumeAsync ( ) ;
132+ scheduleDescription = await scheduleHandle . DescribeAsync ( ) ;
133+ Console . WriteLine ( scheduleDescription . ToJsonString ( true ) ) ;
134+
135+ Console . WriteLine ( "" ) ;
136+ Console . WriteLine ( "" ) ;
137+ Console . WriteLine ( "" ) ;
138+
139+ await Task . Delay ( TimeSpan . FromMinutes ( 30 ) ) ;
140+ //Console.WriteLine("\nPress any key to delete the schedule and exit...");
141+ //Console.ReadKey();
142+ }
143+ catch ( Exception ex )
144+ {
145+ Console . WriteLine ( $ "One of your schedule operations failed, please fix and rerun: { ex . Message } ") ;
146+ }
147+
148+ await host . StopAsync ( ) ;
0 commit comments