Stored Tasks
Products: FastReport Cloud, FastReport Corporate Server, FastReport Publisher
As mentioned in the previous section, Service solutions has the ability to create tasks that are stored in the database and can be run later. These tasks are called stored tasks.
Let's take a closer look at how to create, update, and run stored tasks using the C# SDK.
About ViewModels and Types
When working, you will pass different ViewModels (View Models) to SDK methods. Let's consider which view models can be used.
View Models for Creating Tasks
View Models for creating transformer tasks:
- CreatePrepareTaskVM
- CreateExportTemplateTaskVM
- CreateExportReportTaskVM
View Models for creating transport tasks:
- CreateEmailTaskVM
- CreateWebhookTaskVM
- CreateFTPUploadTaskVM
- CreateS3UploadTaskVM
View Models for creating other tasks:
- CreateFetchTaskVM
View Models for Updating Properties of Saved Tasks
View Models for updating transformer tasks:
- UpdatePrepareTaskVM
- UpdateExportTemplateTaskVM
- UpdateExportReportTaskVM
View Models for updating transport tasks:
- UpdateEmailTaskVM
- UpdateWebhookTaskVM
- UpdateFTPUploadTaskVM
- UpdateS3UploadTaskVM
View Models for updating other tasks:
- UpdateFetchTaskVM
View Model for Updating Task Permissions:
- UpdateTaskPermissionsVM
Creating a Task
To create tasks, you should use the CreateTask or CreateTaskAsync method. It can accept any class that inherits from TaskBaseVM:
- CreatePrepareTaskVM
- CreateExportTemplateTaskVM
- CreateExportReportTaskVM
- CreateFetchTaskVM
- CreateEmailTaskVM
- CreateWebhookTaskVM
- CreateFTPUploadTaskVM
- CreateS3UploadTaskVM.
Let's consider creating a report preparation task, saving it to a folder and subsequent export to PDF with saving to a folder:
await tasksClient.CreateTaskAsync(new CreatePrepareTaskVM
{
Name = "My First Task",
Type = TaskType.Prepare,
InputFile = new InputFileVM
{
EntityId = "{templateId}"
},
OutputFile = new OutputFileVM
{
FileName = "My First Report Generated by Task.fpx",
FolderId = "{report folder identifier}"
},
Exports = new List<CreateExportReportTaskVM>
{
new CreateExportReportTaskVM
{
Format = ExportFormat.Pdf,
OutputFile = new OutputFileVM
{
FileName = "pdfFromFpxFromFrx.pdf",
FolderId = "{export folder identifier}"
}
}
}
});
Real object identifiers should be entered in the EntityId and FolderId fields. Otherwise, the task will be aborted with an error.
Note! If you don't specify OutputFile, it will be saved to a temporary folder. If you specify an empty OutputFile - to the root folder. If you specify a folder id - to it.
Next, let's consider creating a report export task with subsequent email sending:
var currentTask = await tasksClient.CreateTaskAsync(new CreateExportTemplateTaskVM
{
SubscriptionId = subscriptionId,
Name = "PDF Email Sending Task",
InputFile = new InputFileVM
{
EntityId = template.Id
},
ReportParameters = new Dictionary<string, string>
{
{ "param1", "val1" },
{ "param2", "val2" },
{ "param3", "val3" }
},
Format = ExportFormat.Pdf,
ExportParameters = new Dictionary<string, string>
{
{ "PrintOptimized", "true" },
{ "JpegQuality", "90" }
},
Transports = new List<CreateTransportTaskBaseVM>
{
new CreateEmailTaskVM
{
Name = "Email Distribution Task",
From = "admin@company.com",
To = new List<string> { "user@company.com","seller@company.com" },
Subject = "Important Report",
IsBodyHtml = false,
Username = "admin@company.com",
Password = "parol123",
Server = "smtp.company.com",
Body = "An important report is attached to this email.",
Port= 587,
EnableSsl = true
}
}
});
Real object identifiers should be entered in the EntityId and FolderId fields. Otherwise, the task will be aborted with an error.
Note! If you don't specify OutputFile, it will be saved to a temporary folder. If you specify an empty OutputFile - to the root folder. If you specify a folder id - to it.
Running Tasks on Schedule
To configure scheduled task execution, you need to specify the following fields:
CronExpressionStartsOnEnds
CronExpression - a string containing a cron expression that defines the task execution schedule.
StartsOn - the date and time from which the scheduled task execution begins (in user's timezone).
Ends - an object containing the date and time until which the task will be executed (On) or the number of repetitions (After).
The On field is specified in user's timezone.
If neither the After nor the On field is specified, the task will run indefinitely.
Example of creating a scheduled task:
var currentTask = await tasksClient.CreateTaskAsync(new CreateExportTemplateTaskVM
{
SubscriptionId = subscriptionId,
Name = "PDF Email Sending Task",
InputFile = new InputFileVM
{
EntityId = template.Id
},
Format = ExportFormat.Pdf,
CronExpression = "*/5 * * * *",
StartsOn = DateTime.Now.AddMinutes(1),
Ends = new CreateTaskEndVM
{
On = DateTime.Now.AddDays(1)
}
});
In this example, the task will run every 5 minutes, starting from the task creation date and will finish execution after one day.
To make the task execute, for example, 10 times, you need to replace the On field with the After field:
CronExpression = "*/5 * * * *",
StartsOn = DateTime.UtcNow,
Ends = new CreateTaskEndVM
{
After = 10
}
Getting Task List
// Get the first 100 tasks from the workspace
var tasks = await tasksClient.GetListAsync(0, 100, subscription.Id);
Editing a Task
After a task is created, you can change some of its parameters.
// Task editing
var editedTask = await tasksClient.UpdateTaskAsync(currentTaskId, new UpdateExportTemplateTaskVM
{
Format = ExportFormat.Docx,
ReportParameters = new Dictionary<string, string>
{
{ "param1", "val1" },
{ "param2", "val2" }
},
TransportIds = new List<string> { "{transportId}" }
});
Note that when updating a task, you need to specify TransportIds - a list of transport task identifiers (Email, FTP, Webhook, S3), not the tasks themselves. The task itself can be created in advance either separately or when creating a transformer task. For more details on their creation, read the section Creating Transport Tasks.
Executing a Task by Specified Identifier
// Run task by identifier
await tasksClient.RunTaskByIdAsync(id);
Deleting Tasks from Storage
// Delete all tasks
foreach(var t in tasks.Tasks)
{
await tasksClient.DeleteTaskAsync(t.Id);
}