A scheduled task is a script that the DocView Worker runs on a timetable. It is not a step in a batch's workflow: it runs on its own, usually after export. For example:
- every night at 02:00, zip the documents exported since the last run and e-mail the zip to the accounts team;
- every hour, send the new export files to a customer by SFTP;
- on the first day of the month, write a summary file of last month's exported batches;
- every 15 minutes, call a web service with the batches exported since the last run.
You find scheduled tasks under Administration › System › Scheduled tasks. You need the permission Scheduled tasks (Admin.ScheduledTasks). C# tasks also need the permission C# scripts (Admin.CSharpScripts).
Create a task
- Click New task. A working example is already in the editor: it zips the PDF files exported since the last run and e-mails them.
- Give the task a Name and, if you like, a Description.
- Choose the language: JavaScript or C# (see JavaScript or C#?).
- Write the script. Check tells you whether it parses (JavaScript) or compiles (C#) without running it. Example puts the example back.
- Set the Schedule (below). Next runs shows the next five times it runs.
- Fill in the Parameters: one
name=valueper line, for examplefolder=D:\Exportandto=accounts@example.com. The script reads them withdv.parameter('folder'). - Set the Time limit. A run that takes longer is stopped and recorded as failed.
- Switch the task On when you want it to run on its schedule, and click Save.
Saving checks the script and the schedule; a task that does not parse or compile is not saved.
Schedules
| Runs | Set |
|---|---|
| Every few minutes | Every N minutes, on the clock (every 15 minutes runs at :00, :15, :30 and :45). |
| Every hour | At a minute past each hour. |
| Every day | At a time of day. Tick days to run only on those, for example Monday to Friday. |
| On days of the week | At a time of day on the days you tick. |
| Once a month | On a day of the month at a time of day. Months without that day use their last day. |
| Advanced: cron expression | Any timetable a cron expression can describe. |
Times of day are in the task's Time zone; summer time is taken into account.
A cron expression has five fields: minute hour day-of-month month day-of-week. For example:
| Expression | Runs |
|---|---|
0 2 * * * |
Every day at 02:00 |
0 2 * * 1-5 |
02:00 on weekdays |
*/30 8-18 * * * |
Every 30 minutes from 08:00 to 18:30 |
0 6 1 * * |
06:00 on the first day of the month |
0 0 22 * * 5 |
(six fields: seconds first) 22:00 every Friday |
Run now and the run history
Run now asks the DocView Worker to run the task straight away, even when it is switched off. The DocView Worker picks it up within half a minute; the page follows the run and opens its log when it finishes.
Every run is listed under Runs: when it started, how long it took, the result, who started it (Schedule or your name) and which DocView Worker ran it. Log shows what the script wrote with dv.log(). Runs are kept for 90 days. When a timed run fails, the person who last saved the task is told in the notification centre.
On the list of tasks, the switch turns a task on or off, and Run now runs it.
Only one DocView Worker runs each run, even when several DocView Workers are installed.
JavaScript or C#?
| JavaScript | C# | |
|---|---|---|
| Who may write it | Scheduled tasks permission | Also the C# scripts permission |
| What it can do | Only the DocView helpers (dv) below |
The helpers (dv) and the whole of .NET |
| Files | Only in its work folder, the folders of folder export destinations, and the folders in ScheduledTasks:AllowedFolders |
Anywhere the DocView Worker's account can reach |
| Runs | In a sandbox, with no access to .NET, the network or files except through dv |
In the DocView Worker, with the DocView Worker's rights, like C# export scripts |
C# tasks do not run when C# scripts are switched off on the installation (Export:CSharpScripts:Enabled = false).
The helpers: dv
Both languages have the same helpers, as dv. JavaScript uses lower case first letters (dv.listFiles), C# capitals (dv.ListFiles). Relative paths are inside the run's own work folder, which is removed when the run ends.
| Helper | What it does |
|---|---|
dv.parameter(name, fallback) |
A parameter's value, or the fallback |
dv.parameters |
All parameters |
dv.now, dv.lastRunAt |
When this run started; when the task last ran (null the first time) |
dv.workFolder, dv.workPath(name) |
The run's work folder; a path in it |
dv.log(text), dv.warn(text) |
Write to the run's log |
dv.fail(text) |
Stop the run as failed with this message |
dv.exportedBatches(since) |
The batches exported since a time (default: the last run, or the last 24 hours the first time): id, name, Batch Setup, when, documents, pages and the export destinations they went to |
dv.listFiles(folder, pattern, modifiedSince, subfolders) |
The files in a folder, e.g. '*.pdf', optionally changed since a time and in sub-folders: path, name, size, modified |
dv.zip(files, zipPath, baseFolder) |
Zip files into a zip; paths inside are relative to baseFolder. Returns the zip's path |
dv.zipFolder(folder, zipPath, pattern, modifiedSince) |
Zip a folder's files, keeping sub-folders |
dv.copy, dv.move, dv.delete, dv.exists, dv.createFolder |
Work with files and folders |
dv.writeText(path, text), dv.readText(path) |
Write or read a text file |
dv.sendEmail({ to, subject, body, attachments }) |
E-mail through the SMTP server set up in Settings › E-mail; to can have several addresses separated by commas |
dv.uploadSftp(settings, files) |
Upload files by SFTP with the same settings as an SFTP export destination: host, port, username, path, and passwordSecret or privateKeySecret, hostKeySha256 |
dv.httpGet(url), dv.httpPost(url, body, contentType) |
Call a web service; returns the response text |
Keep passwords out of scripts and parameters: store them under Settings › Secrets and pass the secret's name (for example passwordSecret: 'customer-sftp').
In the JavaScript editor, press Ctrl+Space to see the helpers.
Example: zip yesterday's exports and send them by SFTP
const folder = dv.parameter('folder');
const files = dv.listFiles(folder, '*.pdf', dv.lastRunAt, true);
if (files.length === 0) {
dv.log('Nothing new.');
return;
}
const zip = dv.zip(files, 'Exports ' + dv.now.toISOString().substring(0, 10) + '.zip', folder);
dv.uploadSftp({ host: 'sftp.customer.com', username: 'docview', passwordSecret: 'customer-sftp', path: '/incoming' }, [zip]);
dv.log('Sent ' + files.length + ' file(s).');
The same in C#:
var folder = dv.Parameter("folder")!;
var files = dv.ListFiles(folder, "*.pdf", dv.LastRunAt, subfolders: true);
if (files.Length == 0) { dv.Log("Nothing new."); return; }
var zip = dv.Zip(files.Select(f => f.Path), dv.WorkPath($"Exports {dv.Now:yyyy-MM-dd}.zip"), folder);
dv.UploadSftp(new { host = "sftp.customer.com", username = "docview", passwordSecret = "customer-sftp", path = "/incoming" }, new[] { zip });
dv.Log($"Sent {files.Length} file(s).");
In C#, throw new ScheduledTaskException("message") fails the run with that message, and await can be used.
Allow more folders (JavaScript)
JavaScript tasks may use their work folder and the folders of the organisation's folder export destinations. To allow other folders, list them in the DocView Worker's configuration:
"ScheduledTasks": {
"AllowedFolders": [ "D:\\Archive", "\\\\fileserver\\handover" ]
}
Organisations
Each organisation has its own scheduled tasks; a task sees only its organisation's batches and secrets. See Organisations and departments.