Differences
This shows you the differences between the selected revisions of the page.
2014-09-11 | 2014-09-11 | ||
introducing webjobs article (martin) | in-page anchored links without page name (martin) | ||
Line 2: | Line 2: | ||
If you need to export/backup data from your Microsoft Azure WebSite to a remote server or import data from the remote server to your WebSite, you can use WinSCP from Azure WebJob. | If you need to export/backup data from your Microsoft Azure WebSite to a remote server or import data from the remote server to your WebSite, you can use WinSCP from Azure WebJob. | ||
- | The Azure WebJob is a script or an application run on Azure WebSite server that has a read/write access to your web site. The job can be executed on demand or schedule. To implement file transfers for the WebJob, you can either run WinSCP in [[scripting|a scripting mode]] from a batch file; use [[library|WinSCP .NET assembly]] from a [[guide_microsoft_azure_webjob_sftp#powershell|PowerShell script]] or develop [[guide_microsoft_azure_webjob_sftp#consoleapp|a (console) application]] that uses the WinSCP .NET assembly. | + | The Azure WebJob is a script or an application run on Azure WebSite server that has a read/write access to your web site. The job can be executed on demand or schedule. To implement file transfers for the WebJob, you can either run WinSCP in [[scripting|a scripting mode]] from a batch file; use [[library|WinSCP .NET assembly]] from a [[#powershell|PowerShell script]] or develop [[#consoleapp|a (console) application]] that uses the WinSCP .NET assembly. |
You can also use the WebJob without having an actual web site as a cloud-hosted scheduled task. | You can also use the WebJob without having an actual web site as a cloud-hosted scheduled task. | ||
Line 11: | Line 11: | ||
WebJob is a package that contains your job executable (e.g. a batch file or a console application) along with all supporting files (script, WinSCP executable, WinSCP .NET assembly, etc). Azure platform automatically tries to detect what executable is the main one. To ease this, you should name the main executable ''run.*'', e.g. ''run.bat'' for a batch file. | WebJob is a package that contains your job executable (e.g. a batch file or a console application) along with all supporting files (script, WinSCP executable, WinSCP .NET assembly, etc). Azure platform automatically tries to detect what executable is the main one. To ease this, you should name the main executable ''run.*'', e.g. ''run.bat'' for a batch file. | ||
- | See [[guide_microsoft_azure_webjob_sftp#powershell|implementation sections]] below for examples of the package contents. | + | See [[#powershell|implementation sections]] below for examples of the package contents. |
===== [[deploying]] Deploying WebJob ===== | ===== [[deploying]] Deploying WebJob ===== | ||
Line 24: | Line 24: | ||
After your WebJob is uploaded, it is extracted to ''/site/wwwroot/App_Data/jobs/type/name'', where ''type'' is ''triggered'' for //Scheduled// and //On demand// jobs and ''continuous'' for //Continuous// jobs. From now on, you can [[guide_microsoft_azure#website|manage/update the job files using FTPS]]. | After your WebJob is uploaded, it is extracted to ''/site/wwwroot/App_Data/jobs/type/name'', where ''type'' is ''triggered'' for //Scheduled// and //On demand// jobs and ''continuous'' for //Continuous// jobs. From now on, you can [[guide_microsoft_azure#website|manage/update the job files using FTPS]]. | ||
- | //You can also deploy the job using Visual Studio, see [[guide_microsoft_azure_webjob_sftp#consoleapp|C# Console Application]] section below.// | + | //You can also deploy the job using Visual Studio, see [[#consoleapp|C# Console Application]] section below.// |
===== [[environment]] WebJob Environment ===== | ===== [[environment]] WebJob Environment ===== | ||
Line 40: | Line 40: | ||
| ''WEBSITE_SITE_NAME'' | Web site name | | | | ''WEBSITE_SITE_NAME'' | Web site name | | | ||
- | When the WebJob is executed, its files are cloned to a temporary folder (see ''WEBJOBS_PATH'') and the job is run there. So it makes no sense to store any data to job's working directory, as that is not persistent and is not accessible remotely. Use ''WEBROOT_PATH'' to locate either job's master directory (''%WEBROOT_PATH%\App_Data\jobs\%WEBJOBS_TYPE%\%WEBJOBS_NAME%'') or ''WEBJOBS_DATA_PATH'' to locate job's data directory. See [[guide_microsoft_azure_webjob_sftp#powershell|implementation sections]] below for examples. | + | When the WebJob is executed, its files are cloned to a temporary folder (see ''WEBJOBS_PATH'') and the job is run there. So it makes no sense to store any data to job's working directory, as that is not persistent and is not accessible remotely. Use ''WEBROOT_PATH'' to locate either job's master directory (''%WEBROOT_PATH%\App_Data\jobs\%WEBJOBS_TYPE%\%WEBJOBS_NAME%'') or ''WEBJOBS_DATA_PATH'' to locate job's data directory. See [[#powershell|implementation sections]] below for examples. |
===== [[log]] Copying Session Log to Job Log ===== | ===== [[log]] Copying Session Log to Job Log ===== | ||
Line 46: | Line 46: | ||
The standard output of the job is redirected to a log file which you can display on Azure Management Portal. | The standard output of the job is redirected to a log file which you can display on Azure Management Portal. | ||
- | It is useful to have WinSCP session log file included into the job's log. For that as the last step of the job, print the WinSCP session log to the standard output. See [[guide_microsoft_azure_webjob_sftp#powershell|implementation sections]] below for examples. | + | It is useful to have WinSCP session log file included into the job's log. For that as the last step of the job, print the WinSCP session log to the standard output. See [[#powershell|implementation sections]] below for examples. |
To display the job log, go to //WebJobs// tab of your web site page on Azure Management Portal and click on a link in //Logs// column of the job. | To display the job log, go to //WebJobs// tab of your web site page on Azure Management Portal and click on a link in //Logs// column of the job. | ||
Line 53: | Line 53: | ||
==== Using Scripting ==== | ==== Using Scripting ==== | ||
- | //If your transfer/synchronization task is simple, use WinSCP scripting. For complex tasks, see below for solutions using WinSCP .NET assembly from a [[guide_microsoft_azure_webjob_sftp#powershell|PowerShell script]] or [[guide_microsoft_azure_webjob_sftp#consoleapp|console application]].// | + | //If your transfer/synchronization task is simple, use WinSCP scripting. For complex tasks, see below for solutions using WinSCP .NET assembly from a [[#powershell|PowerShell script]] or [[#consoleapp|console application]].// |
//Start with learning how to [[guide_automation|automate file transfers (or synchronization) to FTP server or SFTP server]].// | //Start with learning how to [[guide_automation|automate file transfers (or synchronization) to FTP server or SFTP server]].// | ||
Line 59: | Line 59: | ||
Comparing with generic WinSCP script examples, your Azure transfer job should: | Comparing with generic WinSCP script examples, your Azure transfer job should: | ||
- | * have a wrapper batch file named ''run.bat'' so that Azure correctly identifies it as [[guide_microsoft_azure_webjob_sftp#structure|the main executable]]; | + | * have a wrapper batch file named ''run.bat'' so that Azure correctly identifies it as [[#structure|the main executable]]; |
* the wrapper batch file should propagate WinSCP [[scripting#checking_results|exit code]], so that Azure correctly identifies failures (any non-zero exit code is a failure to Azure, the same for WinSCP); | * the wrapper batch file should propagate WinSCP [[scripting#checking_results|exit code]], so that Azure correctly identifies failures (any non-zero exit code is a failure to Azure, the same for WinSCP); | ||
* enable session logging to a unique job run directory (''%WEBJOBS_DATA_PATH%\%WEBJOBS_RUN_ID%''); | * enable session logging to a unique job run directory (''%WEBJOBS_DATA_PATH%\%WEBJOBS_RUN_ID%''); | ||
- | * locate source or destination paths of the transfer using WebJob [[guide_microsoft_azure_webjob_sftp#environment|environment variables]]; | + | * locate source or destination paths of the transfer using WebJob [[#environment|environment variables]]; |
- | * [[guide_microsoft_azure_webjob_sftp#log|print the session log to the standard output]], so that it is available from Azure Management Portal. | + | * [[#log|print the session log to the standard output]], so that it is available from Azure Management Portal. |
An example script (''script.txt'') that backs up the WebSite to a remote SFTP server: | An example script (''script.txt'') that backs up the WebSite to a remote SFTP server: | ||
Line 101: | Line 101: | ||
</code> | </code> | ||
- | A complete WebJob package (to be [[guide_microsoft_azure_webjob_sftp#deploying|deployed to Azure WebSite]]) consists of following files: | + | A complete WebJob package (to be [[#deploying|deployed to Azure WebSite]]) consists of following files: |
* ''winscp.com'' [[executables|executable]]; | * ''winscp.com'' [[executables|executable]]; | ||
Line 116: | Line 116: | ||
Comparing with generic WinSCP PowerShell examples, your Azure transfer job should: | Comparing with generic WinSCP PowerShell examples, your Azure transfer job should: | ||
- | * have a wrapper batch file named ''run.bat'' so that Azure correctly identifies it as [[guide_microsoft_azure_webjob_sftp#structure|the main executable]] (Azure WebJob cannot run PowerShell scripts directly); | + | * have a wrapper batch file named ''run.bat'' so that Azure correctly identifies it as [[#structure|the main executable]] (Azure WebJob cannot run PowerShell scripts directly); |
* the PowerShell script should propagate any error from WinSCP code to an exit code, so that Azure correctly identifies failures (any non-zero exit code is a failure to Azure); | * the PowerShell script should propagate any error from WinSCP code to an exit code, so that Azure correctly identifies failures (any non-zero exit code is a failure to Azure); | ||
* enable session logging to a unique job run directory (''%WEBJOBS_DATA_PATH%\%WEBJOBS_RUN_ID%''); | * enable session logging to a unique job run directory (''%WEBJOBS_DATA_PATH%\%WEBJOBS_RUN_ID%''); | ||
- | * locate source or destination paths of the transfer using WebJob [[guide_microsoft_azure_webjob_sftp#environment|environment variables]]; | + | * locate source or destination paths of the transfer using WebJob [[#environment|environment variables]]; |
- | * [[guide_microsoft_azure_webjob_sftp#log|print the session log to the standard output]], so that it is available from Azure Management Portal; | + | * [[#log|print the session log to the standard output]], so that it is available from Azure Management Portal; |
* not use ''[[http://technet.microsoft.com/en-us/library/hh849877.aspx|Write-Host]]'' cmdlet as there is no console to write to in the Azure WebJob, use ''[[http://technet.microsoft.com/en-us/library/hh849921.aspx|Write-Output]]'' instead. | * not use ''[[http://technet.microsoft.com/en-us/library/hh849877.aspx|Write-Host]]'' cmdlet as there is no console to write to in the Azure WebJob, use ''[[http://technet.microsoft.com/en-us/library/hh849921.aspx|Write-Output]]'' instead. | ||
Line 187: | Line 187: | ||
</code> | </code> | ||
- | A complete WebJob package (to be [[guide_microsoft_azure_webjob_sftp#deploying|deployed to Azure WebSite]]) consists of following files: | + | A complete WebJob package (to be [[#deploying|deployed to Azure WebSite]]) consists of following files: |
* ''winscp.exe'' [[executables|executable]]; | * ''winscp.exe'' [[executables|executable]]; | ||
Line 202: | Line 202: | ||
Comparing with generic WinSCP .NET assembly C# examples, your Azure transfer application should: | Comparing with generic WinSCP .NET assembly C# examples, your Azure transfer application should: | ||
- | * have a wrapper batch file named ''run.bat'' so that Azure correctly identifies it as [[guide_microsoft_azure_webjob_sftp#structure|the main executable]]((There will be two ''.exe'' files, your application and ''winscp.exe'', so Azure won't know which one to pick up, unless you name your application ''run.exe''.)); | + | * have a wrapper batch file named ''run.bat'' so that Azure correctly identifies it as [[#structure|the main executable]]((There will be two ''.exe'' files, your application and ''winscp.exe'', so Azure won't know which one to pick up, unless you name your application ''run.exe''.)); |
* propagate any error from WinSCP .NET library to its exit code, so that Azure correctly identifies failures (any non-zero exit code is a failure to Azure); | * propagate any error from WinSCP .NET library to its exit code, so that Azure correctly identifies failures (any non-zero exit code is a failure to Azure); | ||
* enable session logging to a unique job run directory (''%WEBJOBS_DATA_PATH%\%WEBJOBS_RUN_ID%''); | * enable session logging to a unique job run directory (''%WEBJOBS_DATA_PATH%\%WEBJOBS_RUN_ID%''); | ||
- | * locate source or destination paths of the transfer using WebJob [[guide_microsoft_azure_webjob_sftp#environment|environment variables]]; | + | * locate source or destination paths of the transfer using WebJob [[#environment|environment variables]]; |
- | * [[guide_microsoft_azure_webjob_sftp#log|print the session log to the standard output]], so that it is available from Azure Management Portal or provide other means for accessing the log. | + | * [[#log|print the session log to the standard output]], so that it is available from Azure Management Portal or provide other means for accessing the log. |
An example C# code that backs up the WebSite to a remote SFTP server: | An example C# code that backs up the WebSite to a remote SFTP server: | ||
Line 288: | Line 288: | ||
</code> | </code> | ||
- | A complete WebJob package (to be [[guide_microsoft_azure_webjob_sftp#deploying|deployed to Azure WebSite]]) consists of following files: | + | A complete WebJob package (to be [[#deploying|deployed to Azure WebSite]]) consists of following files: |
* your application executable (e.g. ''ConsoleApplication1.exe''); | * your application executable (e.g. ''ConsoleApplication1.exe''); | ||
Line 298: | Line 298: | ||
You can develop the WebJob (console) application as any other, even using //Visual Studio Express for Windows Desktop//. If you are using //Visual Studio Express for Web//, you can use //Microsoft Azure WebJob// project template. It's basically the same template as a //Console Application// in the Desktop edition.((Web edition does not have //Console Application// template.)) | You can develop the WebJob (console) application as any other, even using //Visual Studio Express for Windows Desktop//. If you are using //Visual Studio Express for Web//, you can use //Microsoft Azure WebJob// project template. It's basically the same template as a //Console Application// in the Desktop edition.((Web edition does not have //Console Application// template.)) | ||
- | With //Visual Studio Express for Web// (2013 edition with Update 3)((For older versions you need Azure SDK.)) you can ease [[guide_microsoft_azure_webjob_sftp#deploying|the deployment]] using command //Publish as Azure WebJob//, which is available in the project context menu in //Solution Explorer//. It opens //[[http://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-deploy-webjobs/#configure|Add Azure WebJob]]// dialog that allows you to name your job and setup how it is run (including scheduling). Make sure you add all additional files needed for the job (i.e. ''winscp.exe'', ''run.bat'' and private key, as shown above) to the project with //Build Action// set to //Content// to have them deployed (see also [[library_install#vs|Using WinSCP .NET assembly from Visual Studio]]). When you submit the dialog, a publish process starts on the background in //Web Publish Activity// pane. Next time you publish, after making changes to the project, only modified files are uploaded. See also [[http://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-deploy-webjobs/|How to Deploy Azure WebJobs to Azure Websites]]. | + | With //Visual Studio Express for Web// (2013 edition with Update 3)((For older versions you need Azure SDK.)) you can ease [[#deploying|the deployment]] using command //Publish as Azure WebJob//, which is available in the project context menu in //Solution Explorer//. It opens //[[http://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-deploy-webjobs/#configure|Add Azure WebJob]]// dialog that allows you to name your job and setup how it is run (including scheduling). Make sure you add all additional files needed for the job (i.e. ''winscp.exe'', ''run.bat'' and private key, as shown above) to the project with //Build Action// set to //Content// to have them deployed (see also [[library_install#vs|Using WinSCP .NET assembly from Visual Studio]]). When you submit the dialog, a publish process starts on the background in //Web Publish Activity// pane. Next time you publish, after making changes to the project, only modified files are uploaded. See also [[http://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-deploy-webjobs/|How to Deploy Azure WebJobs to Azure Websites]]. |
===== Further Reading ===== | ===== Further Reading ===== |