I refer to your link code and write a WebJob
with QueueTrigger
, then upload it and stop it after it has run a while. My output log shows it works fine. Maybe you can refer to it.
public static string ShutDownFilePath
{
get
{
return shutDownFile;
}
}
public static void ShutdownMonitorJob(
[QueueTrigger("myqueue")] string message,
TextWriter log,
CancellationToken cancellationToken)
{
new Thread(new ThreadStart(() =>
{
log.WriteLine("From thread: In about 10 seconds, the job will be signaled to stop");
Thread.Sleep(10000);
// Modify the shutdown file
File.WriteAllText(shutDownFile, string.Empty);
})).Start();
log.WriteLine("From function: Received a message: " + message);
while (!cancellationToken.IsCancellationRequested)
{
log.WriteLine("From function: Cancelled: No");
Thread.Sleep(2000);
}
// Perform the graceful shutdown logic here
log.WriteLine("From function: Cancelled: Yes");
}
And this is my output log.The cancelled status is changed to yes.
[12/18/2018 02:14:07 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:07 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO]
[12/18/2018 02:14:09 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:09 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO]
[12/18/2018 02:14:09 > dd4ec8: SYS INFO] Detected WebJob file/s were updated,
refreshing
WebJob
[12/18/2018 02:14:10 > dd4ec8: SYS INFO] Status changed to Stopping
[12/18/2018 02:14:11 > dd4ec8: INFO] From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO] From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:11 > dd4ec8: INFO] From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO]
[12/18/2018 02:14:11 > dd4ec8: INFO] Executed 'Functions.ShutdownMonitorJob'
(Succeeded, Id=9ce21c43-c113-4794-b565-81cb615a29ab)
[12/18/2018 02:14:11 > dd4ec8: INFO] info: Host.Results[0]
[12/18/2018 02:14:11 > dd4ec8: INFO] Executed 'Functions.ShutdownMonitorJob'
(Succeeded, Id=9ce21c43-c113-4794-b565-81cb615a29ab)
[12/18/2018 02:14:11 > dd4ec8: INFO] Job host stopped
I did more test with ServiceBus Trigger.
Here is my Program.cs content.
static void Main(string[] args)
{
JobHostConfiguration config = new JobHostConfiguration();
config.UseServiceBus();
JobHost host = new JobHost(config);
host.RunAndBlock();
}
And here is my Function content.
private static string shutDownFile = Path.GetTempFileName();
public static string ShutDownFilePath
{
get
{
return shutDownFile;
}
}
public static void ShutdownMonitorJob(
[ServiceBusTrigger("myqueue")]
string myQueueItem,
TextWriter log,
CancellationToken cancellationToken)
{
new Thread(new ThreadStart(() =>
{
log.WriteLine("From thread: In about 10 seconds, the job will be signaled to stop");
Thread.Sleep(10000);
// Modify the shutdown file
File.WriteAllText(shutDownFile, string.Empty);
})).Start();
log.WriteLine("From function: Received a message: " + myQueueItem);
while (!cancellationToken.IsCancellationRequested)
{
log.WriteLine("From function: Cancelled: No");
Thread.Sleep(2000);
}
// Perform the graceful shutdown logic here
log.WriteLine("From function: Cancelled: Yes");
}
}
And this my new log pic.
I stopped the webjob
and the status change normally.