public Task <FileTriggerInfo> CheckForFileAsync(FileWatcherType fileWatcherType, object nextItem = null)
        {
            // TODO: Implement this later for local files based on last modified date/time
            var fileTriggerInfo = new FileTriggerInfo();

            return(Task.FromResult(fileTriggerInfo));
        }
            public override Collection <Attribute> GetAttributes()
            {
                Collection <Attribute> attributes = new Collection <Attribute>();

                string connectionStringSetting = Context.GetMetadataValue <string>("connection");

                if (!string.IsNullOrEmpty(connectionStringSetting))
                {
                    // Register each binding connection with the global config
                    string connectionStringValue = _nameResolver.Resolve(connectionStringSetting);
                    _apiHubConfig.AddConnection(connectionStringSetting, connectionStringValue);
                }

                string path = Context.GetMetadataValue <string>("path");

                if (Context.IsTrigger)
                {
                    FileWatcherType fileWatcherType       = Context.GetMetadataEnumValue <FileWatcherType>("fileWatcherType", FileWatcherType.Created);
                    int             pollIntervalInSeconds = Context.GetMetadataValue <int>("pollIntervalInSeconds");

                    attributes.Add(new ApiHubFileTriggerAttribute(connectionStringSetting, path, fileWatcherType, pollIntervalInSeconds));
                }
                else
                {
                    attributes.Add(new ApiHubFileAttribute(connectionStringSetting, path, Context.Access));
                }

                return(attributes);
            }
        public ApiHubListener(
            ApiHubConfiguration apiHubConfig,
            JobHostConfiguration config,
            IFolderItem folder,
            string functionName,
            ITriggeredFunctionExecutor executor,
            TraceWriter trace,
            ApiHubFileTriggerAttribute attribute)
        {
            this._apiHubConfig            = apiHubConfig;
            this._config                  = config;
            this._folderSource            = folder;
            this._functionName            = functionName;
            this._executor                = executor;
            this._trace                   = trace;
            this._pollIntervalInSeconds   = attribute.PollIntervalInSeconds;
            this._fileWatcherType         = attribute.FileWatcherType;
            this._siteName                = this._config.HostId;
            this._connectionStringSetting = attribute.ConnectionStringSetting;
            _serializer                   = JsonSerializer.Create();

            CloudQueueClient queueClient = CloudStorageAccount.Parse(this._config.StorageConnectionString).CreateCloudQueueClient();

            this._poisonQueue = queueClient.GetQueueReference(PoisonQueueName);
        }
 public ApiHubListener(
     IFolderItem folder,
     ITriggeredFunctionExecutor executor,
     TraceWriter trace,
     int pollIntervalInSeconds,
     FileWatcherType fileWatcherType = FileWatcherType.Created)
 {
     this._folderSource          = folder;
     this._executor              = executor;
     this._trace                 = trace;
     this._pollIntervalInSeconds = pollIntervalInSeconds;
     this._fileWatcherType       = fileWatcherType;
 }
Example #5
0
        public void Run(string path, FileWatcherType watcherType)
        {
            _watcher = new FileSystemWatcher(path);

            if (watcherType == FileWatcherType.Created)
            {
                _watcher.Created += FileCreated;
            }
            else
            {
                _watcher.Changed += FileChanged;
            }

            _watcher.EnableRaisingEvents = true;
        }
        public IFileWatcher CreateFileWatcher(FileWatcherType fileWatcherType, Func <IFileItem, object, Task> callback, object nextItem = null, int pollIntervalInSeconds = CdpConstants.DefaultFileWatcherIntervalInSeconds)
        {
            if (callback == null)
            {
                throw new ArgumentException("Callback can not be null.");
            }

            if (!Directory.Exists(_path))
            {
                Directory.CreateDirectory(_path);
            }

            var poll = new LocalPoll
            {
                _callback = callback,
                _rootPath = this._rootPath
            };

            poll.Run(_path, fileWatcherType);

            return(poll);
        }
 /// <summary>
 /// Attribute used to bind a parameter to a SAAS file
 /// </summary>
 /// <param name="key">App settings key name that have the connections string</param>
 /// <param name="path">Relative path to the file <example>/folder/subfolder/file.txt</example></param>
 /// <param name="fileWatcherType">Type of the file watcher.</param>
 /// <param name="pollIntervalInSeconds">The poll interval in seconds.</param>
 public ApiHubFileTriggerAttribute(string key, string path, FileWatcherType fileWatcherType = FileWatcherType.Created, int pollIntervalInSeconds = 0)
     : base(key, path)
 {
     this.PollIntervalInSeconds = pollIntervalInSeconds;
     this.FileWatcherType       = fileWatcherType;
 }
Example #8
0
        public IFileWatcher CreateFileWatcher(FileWatcherType fileWatcherType, Func <IFileItem, object, Task> callback, object nextItem = null, int pollIntervalInSeconds = CdpConstants.DefaultFileWatcherIntervalInSeconds)
        {
            Uri pollUri = null;

            if (callback == null)
            {
                throw new ArgumentException("Callback can not be null.");
            }

            var poll = new Poll
            {
                _pollIntervalInSeconds = pollIntervalInSeconds,
                _cdpHelper             = _cdpHelper,
                _callback = callback
            };

            if (nextItem == null)
            {
                if (string.IsNullOrEmpty(_handleId))
                {
                    _handleId = GetHandleIdFromPathAsync(_cdpHelper.MakeUri(CdpConstants.TopMostFolderRoot), _path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries), 0, null).GetAwaiter().GetResult();

                    if (string.IsNullOrEmpty(_handleId))
                    {
                        _cdpHelper.Logger.Error("Unable to get a reference to path: " + _path);
                        return(null);
                    }
                }

                if (fileWatcherType == FileWatcherType.Created)
                {
                    // FTP and SFTP connectors do not currently implement onnewfile trigger but instead onupdatefile trigger handles both file creates and updates.
                    // This is a workaround to enable file create triggers for FTP and SFTP connections.
                    // TODO: This needs to be removed if/when FTP and SFTP implement separate triggers for file create and updates.
                    if (!IsFtpOrSftpApi())
                    {
                        pollUri = _cdpHelper.MakeUri(CdpConstants.OnNewFileTemplate, _handleId);
                    }
                    else
                    {
                        pollUri = _cdpHelper.MakeUri(CdpConstants.OnUpdateFileTemplate, _handleId);
                    }
                }
                else if (fileWatcherType == FileWatcherType.Updated)
                {
                    pollUri = _cdpHelper.MakeUri(CdpConstants.OnUpdateFileTemplate, _handleId);
                }
            }
            else
            {
                pollUri = nextItem as Uri;

                if (pollUri == null)
                {
                    throw new ArgumentException("Invalid type", "nextItem");
                }
            }

            if (!string.IsNullOrEmpty(_handleId) || nextItem != null)
            {
                Task tIgnore = poll.Run(pollUri);

                _cdpHelper.Logger.Info(string.Format("Started monitoring folder: {0}, pollUri: {1}", _path, pollUri.AbsoluteUri));

                poll._runTask = tIgnore;

                return(poll);
            }
            else
            {
                _cdpHelper.Logger.Error("Can't create a trigger on a folder which does not exist: " + _path);

                return(null);
            }
        }
Example #9
0
        public async Task <FileTriggerInfo> CheckForFileAsync(FileWatcherType fileWatcherType, object nextItem = null)
        {
            Uri pollUri = null;

            if (nextItem == null)
            {
                if (string.IsNullOrEmpty(_handleId))
                {
                    _handleId = await GetHandleIdFromPathAsync(_cdpHelper.MakeUri(CdpConstants.TopMostFolderRoot), _path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries), 0, null);
                }

                if (fileWatcherType == FileWatcherType.Created)
                {
                    // TODO: This needs to be removed if/when FTP and SFTP implement separate triggers for file create and updates.
                    if (!IsFtpOrSftpApi())
                    {
                        pollUri = _cdpHelper.MakeUri(CdpConstants.OnNewFileTemplate, _handleId);
                    }
                    else
                    {
                        pollUri = _cdpHelper.MakeUri(CdpConstants.OnUpdateFileTemplate, _handleId);
                    }
                }
                else if (fileWatcherType == FileWatcherType.Updated)
                {
                    pollUri = _cdpHelper.MakeUri(CdpConstants.OnUpdateFileTemplate, _handleId);
                }
            }
            else
            {
                pollUri = nextItem as Uri;

                if (pollUri == null)
                {
                    throw new ArgumentException("Invalid type", "nextItem");
                }
            }

            // Only the header is required and there is no need to read the entire file content which the connector returns.
            HttpResponseMessage response = await _cdpHelper.SendAsync(HttpMethod.Get, pollUri, HttpCompletionOption.ResponseHeadersRead);

            Uri       nextUri          = response.Headers.Location; // poll next
            TimeSpan  delay            = new TimeSpan(0, 0, CdpConstants.DefaultFileWatcherIntervalInSeconds);
            IFileItem fileItem         = null;
            bool      shouldResetState = false;

            if (response.StatusCode == HttpStatusCode.OK)
            {
                string fileId   = response.GetHeader("x-ms-file-id");
                string fullpath = response.GetHeader("x-ms-file-path");

                // Chop off leading
                if (fullpath[0] == '/')
                {
                    fullpath = fullpath.Substring(1);
                }

                // Got a new file
                fileItem = new FileItem
                {
                    _path     = fullpath,
                    _handleId = fileId,
                };
            }
            else if (response.StatusCode == HttpStatusCode.Accepted)
            {
                var rt = response.Headers.RetryAfter;

                if (rt.Delta.HasValue)
                {
                    delay = rt.Delta.Value;
                }

                var resetHeader = response.GetHeader("x-ms-trigger-reset");

                if (resetHeader == "1")
                {
                    shouldResetState = true;
                }
            }
            else
            {
                _cdpHelper.Logger.Error("Unable to check for file, http status code: " + response.StatusCode.ToString());
            }

            return(new FileTriggerInfo
            {
                FileItem = fileItem,
                NextUri = nextUri,
                WatcherType = fileWatcherType,
                RetryAfter = delay,
                ShouldResetState = shouldResetState
            });
        }