1

I am new to azure web functions and I can't seem to find any documentation about multiple triggers in the same project. I have created my solution and already created a nice TimerTrigger, which works fine.

This trigger downloads files from an ftp directory and then uploads the files to our azure storage account. The code looks like this:

[DependencyInjectionConfig(typeof(DependencyInjectionConfig))]
public class FtpTrigger
{
    [FunctionName("EOrderTimerTrigger")]
    public static async Task Run(
        [TimerTrigger("*/15 * * * * *")]TimerInfo myTimer, 
        TraceWriter log,
        [Inject]IConfig config,
        [Inject]SmartFileClient smartFileClient,
        [Inject]SettingsHandler settingsHandler,
        [Inject]StorageHandler storageHandler)
    {
        if (myTimer.IsPastDue)
            log.Info("Timer is running late!");

        log.Info($"Listing directories from { config.FtpUrl }");

        var accounts = await smartFileClient.ListDirectories(config.FtpPath);

        if (!accounts.Any())
        {
            log.Warning($"There are no files waiting to be processed for any account");
            return;
        }

        foreach (var account in accounts)
        {
            try
            {
                log.Info($"Retrieving settings for { account }");
                var url = $"{config.ApiBaseUrl}/{config.ApiSettingsPath}";
                var settings = await settingsHandler.GetAsync(url, account);

                log.Info($"Find all order files for { account }");
                var fileNames = await smartFileClient.ListFiles($"{config.FtpPath}/{account}", settings.OrderFileSuffix.Replace(".", ""));

                if (!fileNames.Any())
                {
                    log.Warning($"No files to process for { account }");
                    continue;
                }

                log.Info($"Get a list of files awaiting to be processed for { account }");
                var awaiting = await storageHandler.ListAsync(config.StorageProcessingContainer, account);

                foreach(var fileName in fileNames)
                {
                    log.Info($"Finding any files awaiting to be processed in the storage account for { account }");
                    var friendlyName = Regex.Replace(fileName, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled); ;
                    var match = awaiting.Any(m => m.Equals(friendlyName));
                    if (match)
                    {
                        log.Warning($"File ({fileName}) already awaiting to be processed for { account }");
                        continue;
                    }

                    log.Info($"Download { fileName } from the ftp directory for { account }");
                    var bytes = await smartFileClient.DownloadFile($"{config.FtpPath}/{account}", fileName);

                    log.Info($"Upload { fileName } to the Azure Storage account for { account }");
                    await storageHandler.UploadAsync(friendlyName, bytes, config.StorageProcessingContainer, account);

                    log.Info($"Delete { fileName } from the ftp directory for { account }");
                    if (!await smartFileClient.DeleteFile($"{config.FtpPath}/{account}", fileName))
                        log.Error($"Failed to delete { fileName } from the ftp directory for { account }");
                }

            } catch (Exception ex)
            {
                log.Error($"{ ex.Message }");
            }
        }

        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

Now I want to add a second trigger. This trigger is going to be a BlobTrigger. I added it to my project and ran it and it never fired, even when files were created. So I realised I must be doing something wrong.

Can someone tell me how to have multiple triggers in a project? And if it can't be done; what is the alternative?

r3plica
  • 13,017
  • 23
  • 128
  • 290

1 Answers1

0

Actually, you could add multiple trigger in a same azure function project.

I added it to my project and ran it and it never fired, even when files were created.

You could follow this article.

[FunctionName("Function2")]
        public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "blobconnect")]Stream myBlob, string name, TraceWriter log)
        {
            log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
        }

Note: The string {name} in the blob trigger path samples-workitems/{name} creates a binding expression that you can use in function code to access the file name of the triggering blob. For more information, see Blob name patterns later in this article.

You only need to modify samples-workitems to your container name. And set the blob connection in local.hosting.json file.

Community
  • 1
  • 1
Joey Cai
  • 18,968
  • 1
  • 20
  • 30