public JobHostContext(IFunctionIndexLookup functionLookup,
     IFunctionExecutor executor,
     IListener listener,
     TraceWriter trace,
     IAsyncCollector<FunctionInstanceLogEntry> fastLogger = null)
 {
     _functionLookup = functionLookup;
     _executor = executor;
     _listener = listener;
     _trace = trace;
     _fastLogger = fastLogger;
 }
Esempio n. 2
0
        // Do the full runtime intitialization. This includes static initialization.
        // This mainly means:
        // - indexing the functions
        // - spinning up the listeners (so connecting to the services)
        public static async Task <JobHostContext> CreateJobHostContextAsync(
            this JobHostConfiguration config,
            ServiceProviderWrapper services, // Results from first phase
            JobHost host,
            CancellationToken shutdownToken,
            CancellationToken cancellationToken)
        {
            FunctionExecutor                           functionExecutor       = services.GetService <FunctionExecutor>();
            IFunctionIndexProvider                     functionIndexProvider  = services.GetService <IFunctionIndexProvider>();
            ITriggerBindingProvider                    triggerBindingProvider = services.GetService <ITriggerBindingProvider>();
            IBindingProvider                           bindingProvider        = services.GetService <IBindingProvider>();
            SingletonManager                           singletonManager       = services.GetService <SingletonManager>();
            IJobActivator                              activator              = services.GetService <IJobActivator>();
            IHostIdProvider                            hostIdProvider         = services.GetService <IHostIdProvider>();
            INameResolver                              nameResolver           = services.GetService <INameResolver>();
            IExtensionRegistry                         extensions             = services.GetExtensions();
            IStorageAccountProvider                    storageAccountProvider = services.GetService <IStorageAccountProvider>();
            ILoggerFactory                             loggerFactory          = services.GetService <ILoggerFactory>();
            IFunctionResultAggregatorFactory           aggregatorFactory      = services.GetService <IFunctionResultAggregatorFactory>();
            IAsyncCollector <FunctionInstanceLogEntry> functionEventCollector = null;
            SharedQueueHandler                         hostSharedQueue        = services.GetService <SharedQueueHandler>();

            // Create the aggregator if all the pieces are configured
            IAsyncCollector <FunctionInstanceLogEntry> aggregator = null;

            if (loggerFactory != null && aggregatorFactory != null && config.Aggregator.IsEnabled)
            {
                aggregator = aggregatorFactory.Create(config.Aggregator.BatchSize, config.Aggregator.FlushTimeout, loggerFactory);
            }

            IQueueConfiguration queueConfiguration = services.GetService <IQueueConfiguration>();
            var blobsConfiguration = config.Blobs;

            IAsyncCollector <FunctionInstanceLogEntry> registeredFunctionEventCollector = services.GetService <IAsyncCollector <FunctionInstanceLogEntry> >();

            if (registeredFunctionEventCollector != null && aggregator != null)
            {
                // If there are both an aggregator and a registered FunctionEventCollector, wrap them in a composite
                functionEventCollector = new CompositeFunctionEventCollector(new[] { registeredFunctionEventCollector, aggregator });
            }
            else
            {
                // Otherwise, take whichever one is null (or use null if both are)
                functionEventCollector = aggregator ?? registeredFunctionEventCollector;
            }

            IWebJobsExceptionHandler exceptionHandler = services.GetService <IWebJobsExceptionHandler>();

            if (exceptionHandler != null)
            {
                exceptionHandler.Initialize(host);
            }

            bool hasFastTableHook   = services.GetService <IAsyncCollector <FunctionInstanceLogEntry> >() != null;
            bool noDashboardStorage = config.DashboardConnectionString == null;

            // Only testing will override these interfaces.
            IHostInstanceLoggerProvider     hostInstanceLoggerProvider     = services.GetService <IHostInstanceLoggerProvider>();
            IFunctionInstanceLoggerProvider functionInstanceLoggerProvider = services.GetService <IFunctionInstanceLoggerProvider>();
            IFunctionOutputLoggerProvider   functionOutputLoggerProvider   = services.GetService <IFunctionOutputLoggerProvider>();

            if (hostInstanceLoggerProvider == null && functionInstanceLoggerProvider == null && functionOutputLoggerProvider == null)
            {
                if (hasFastTableHook && noDashboardStorage)
                {
                    var loggerProvider = new FastTableLoggerProvider(loggerFactory);
                    hostInstanceLoggerProvider     = loggerProvider;
                    functionInstanceLoggerProvider = loggerProvider;
                    functionOutputLoggerProvider   = loggerProvider;
                }
                else
                {
                    var loggerProvider = new DefaultLoggerProvider(storageAccountProvider, loggerFactory);
                    hostInstanceLoggerProvider     = loggerProvider;
                    functionInstanceLoggerProvider = loggerProvider;
                    functionOutputLoggerProvider   = loggerProvider;
                }
            }

            using (CancellationTokenSource combinedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, shutdownToken))
            {
                CancellationToken combinedCancellationToken = combinedCancellationSource.Token;

                await WriteSiteExtensionManifestAsync(combinedCancellationToken);

                IStorageAccount dashboardAccount = await storageAccountProvider.GetDashboardAccountAsync(combinedCancellationToken);

                IHostInstanceLogger hostInstanceLogger = await hostInstanceLoggerProvider.GetAsync(combinedCancellationToken);

                IFunctionInstanceLogger functionInstanceLogger = await functionInstanceLoggerProvider.GetAsync(combinedCancellationToken);

                IFunctionOutputLogger functionOutputLogger = await functionOutputLoggerProvider.GetAsync(combinedCancellationToken);

                loggerFactory.AddProvider(new FunctionOutputLoggerProvider());

                if (functionExecutor == null)
                {
                    var extensionRegistry     = config.GetService <IExtensionRegistry>();
                    var globalFunctionFilters = extensionRegistry.GetFunctionFilters();

                    functionExecutor = new FunctionExecutor(functionInstanceLogger, functionOutputLogger, exceptionHandler, functionEventCollector, loggerFactory, globalFunctionFilters);
                    services.AddService(functionExecutor);
                }

                if (functionIndexProvider == null)
                {
                    var defaultTimeout = config.FunctionTimeout?.ToAttribute();
                    functionIndexProvider = new FunctionIndexProvider(
                        services.GetService <ITypeLocator>(),
                        triggerBindingProvider,
                        bindingProvider,
                        activator,
                        functionExecutor,
                        extensions,
                        singletonManager,
                        loggerFactory,
                        hostSharedQueue,
                        defaultTimeout);

                    // Important to set this so that the func we passed to DynamicHostIdProvider can pick it up.
                    services.AddService <IFunctionIndexProvider>(functionIndexProvider);
                }

                IFunctionIndex functions = await functionIndexProvider.GetAsync(combinedCancellationToken);

                IListenerFactory functionsListenerFactory = new HostListenerFactory(functions.ReadAll(), singletonManager, activator, nameResolver, loggerFactory);

                IFunctionExecutor hostCallExecutor;
                IListener         listener;
                HostOutputMessage hostOutputMessage;

                string hostId = await hostIdProvider.GetHostIdAsync(cancellationToken);

                if (string.Compare(config.HostId, hostId, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    // if this isn't a static host ID, provide the HostId on the config
                    // so it is accessible
                    config.HostId = hostId;
                }

                if (dashboardAccount == null)
                {
                    hostCallExecutor = new ShutdownFunctionExecutor(shutdownToken, functionExecutor);

                    IListener factoryListener  = new ListenerFactoryListener(functionsListenerFactory, hostSharedQueue);
                    IListener shutdownListener = new ShutdownListener(shutdownToken, factoryListener);
                    listener = shutdownListener;

                    hostOutputMessage = new DataOnlyHostOutputMessage();
                }
                else
                {
                    string sharedQueueName = HostQueueNames.GetHostQueueName(hostId);
                    IStorageQueueClient dashboardQueueClient       = dashboardAccount.CreateQueueClient();
                    IStorageQueue       sharedQueue                = dashboardQueueClient.GetQueueReference(sharedQueueName);
                    IListenerFactory    sharedQueueListenerFactory = new HostMessageListenerFactory(sharedQueue,
                                                                                                    queueConfiguration, exceptionHandler, loggerFactory, functions,
                                                                                                    functionInstanceLogger, functionExecutor);

                    Guid             hostInstanceId               = Guid.NewGuid();
                    string           instanceQueueName            = HostQueueNames.GetHostQueueName(hostInstanceId.ToString("N"));
                    IStorageQueue    instanceQueue                = dashboardQueueClient.GetQueueReference(instanceQueueName);
                    IListenerFactory instanceQueueListenerFactory = new HostMessageListenerFactory(instanceQueue,
                                                                                                   queueConfiguration, exceptionHandler, loggerFactory, functions,
                                                                                                   functionInstanceLogger, functionExecutor);

                    HeartbeatDescriptor heartbeatDescriptor = new HeartbeatDescriptor
                    {
                        SharedContainerName = HostContainerNames.Hosts,
                        SharedDirectoryName = HostDirectoryNames.Heartbeats + "/" + hostId,
                        InstanceBlobName    = hostInstanceId.ToString("N"),
                        ExpirationInSeconds = (int)HeartbeatIntervals.ExpirationInterval.TotalSeconds
                    };

                    IStorageBlockBlob blob = dashboardAccount.CreateBlobClient()
                                             .GetContainerReference(heartbeatDescriptor.SharedContainerName)
                                             .GetBlockBlobReference(heartbeatDescriptor.SharedDirectoryName + "/" + heartbeatDescriptor.InstanceBlobName);
                    IRecurrentCommand heartbeatCommand = new UpdateHostHeartbeatCommand(new HeartbeatCommand(blob));

                    IEnumerable <MethodInfo> indexedMethods = functions.ReadAllMethods();
                    Assembly hostAssembly = GetHostAssembly(indexedMethods);
                    string   displayName  = hostAssembly != null?AssemblyNameCache.GetName(hostAssembly).Name : "Unknown";

                    hostOutputMessage = new DataOnlyHostOutputMessage
                    {
                        HostInstanceId      = hostInstanceId,
                        HostDisplayName     = displayName,
                        SharedQueueName     = sharedQueueName,
                        InstanceQueueName   = instanceQueueName,
                        Heartbeat           = heartbeatDescriptor,
                        WebJobRunIdentifier = WebJobRunIdentifier.Current
                    };

                    hostCallExecutor = CreateHostCallExecutor(instanceQueueListenerFactory, heartbeatCommand,
                                                              exceptionHandler, shutdownToken, functionExecutor);
                    IListenerFactory hostListenerFactory = new CompositeListenerFactory(functionsListenerFactory,
                                                                                        sharedQueueListenerFactory, instanceQueueListenerFactory);
                    listener = CreateHostListener(hostListenerFactory, hostSharedQueue, heartbeatCommand, exceptionHandler, shutdownToken);

                    // Publish this to Azure logging account so that a web dashboard can see it.
                    await LogHostStartedAsync(functions, hostOutputMessage, hostInstanceLogger, combinedCancellationToken);
                }

                functionExecutor.HostOutputMessage = hostOutputMessage;

                IEnumerable <FunctionDescriptor> descriptors = functions.ReadAllDescriptors();
                int descriptorsCount = descriptors.Count();

                ILogger startupLogger = loggerFactory?.CreateLogger(LogCategories.Startup);

                if (config.UsingDevelopmentSettings)
                {
                    string msg = "Development settings applied";
                    startupLogger?.LogDebug(msg);
                }

                if (descriptorsCount == 0)
                {
                    string msg = string.Format("No job functions found. Try making your job classes and methods public. {0}",
                                               Constants.ExtensionInitializationMessage);

                    startupLogger?.LogWarning(msg);
                }
                else
                {
                    StringBuilder functionsTrace = new StringBuilder();
                    functionsTrace.AppendLine("Found the following functions:");

                    foreach (FunctionDescriptor descriptor in descriptors)
                    {
                        functionsTrace.AppendLine(descriptor.FullName);
                    }
                    string msg = functionsTrace.ToString();
                    startupLogger?.LogInformation(msg);
                }

                return(new JobHostContext(
                           functions,
                           hostCallExecutor,
                           listener,
                           functionEventCollector,
                           loggerFactory));
            }
        }
Esempio n. 3
0
        public static async void Run(
            [EventHubTrigger("conversationhub", Connection = "EventhubConnectionString")] EventData[] events,
            [EventHub("conversationhub", Connection = "EventhubConnectionString")] IAsyncCollector <string> conversationhub,
            [ServiceBus("jobqueue", Connection = "ServiceBusConnectionString", EntityType = EntityType.Queue)] IAsyncCollector <string> jobQueue,
            ILogger log)
        {
            try
            {
                List <string> conversations = new List <string>();

                // TODO: get list of conversations from "eventHub"
                foreach (EventData eventData in events)
                {
                    string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                    conversations.Add(messageBody);
                    //await Task.Yield();
                }

                log.LogInformation($"Total conversations: {conversations.Count}");

                // TODO: batch job donwload
                PureCloudClient purecloudClient = new PureCloudClient();
                await purecloudClient.GetAccessToken();

                BatchDownloadJobSubmissionResult job = await purecloudClient.BatchRecordingDownloadByConversation(conversations);

                // TODO: add in "jobQueue"
                await jobQueue.AddAsync(job.Id);
            }
            catch (Exception ex)
            {
                TelemetryClient telemetry = new TelemetryClient();
                telemetry.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                telemetry.TrackException(ex);

                log.LogInformation($"Exception during execution: {ex.Message}");

                do
                {
                    try
                    {
                        await Task.Delay(Convert.ToInt32(Environment.GetEnvironmentVariable("deplaytime")));

                        //List<Task> taskList = new List<Task>(); // to mutch performatic kkkk :P
                        foreach (EventData eventData in events)
                        {
                            string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                            await conversationhub.AddAsync(messageBody);

                            log.LogInformation($"Readded conversation: {messageBody} to conversationhub");
                        }
                        //await Task.WhenAll(taskList); // to mutch performatic kkkk :P

                        break;
                    }
                    catch (Exception exEx)
                    {
                        telemetry.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                        telemetry.TrackException(exEx);

                        log.LogInformation($"Exception during execution: {exEx.Message}");
                    }
                } while (true);
            }
        }
Esempio n. 4
0
        public static void Run([ServiceBusTrigger("orders", Connection = "AzureWebJobsServiceBus")] Orders order, [SignalR(HubName = "notifications")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {order.ProductName}");

            //using (var ctx = new EventMessagingDemoContext())
            //{
            //    ctx.Orders.Add(order);
            //    ctx.SaveChanges();
            //}

            Task.Run(async() =>
            {
                var endpoint  = Environment.GetEnvironmentVariable("DocDbEndpoint");
                var masterKey = Environment.GetEnvironmentVariable("DocDbMasterKey");
                using (var client = new DocumentClient(new Uri(endpoint), masterKey))
                {
                    Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Creating Database <<<<<<<<<<<<<<<<<<<");
                    // Create new database Object
                    //Id defines name of the database
                    var databaseDefinition = new Database {
                        Id = "EventMessagingDemo"
                    };
                    var database = await client.CreateDatabaseIfNotExistsAsync(databaseDefinition);
                    Console.WriteLine("Database EventMessagingDemo created successfully");

                    //Create new database collection
                    Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Creating Collection <<<<<<<<<<<<<<<<<<<");
                    var collectionDefinition = new DocumentCollection {
                        Id = "orders"
                    };
                    var collection = await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("EventMessagingDemo"),
                                                                                           collectionDefinition);
                    Console.WriteLine("Collection orders created successfully");

                    //Insert new Document
                    Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Creating Document <<<<<<<<<<<<<<<<<<<");

                    Orders doc1Definition      = new Orders();
                    doc1Definition.ProductName = order.ProductName;
                    doc1Definition.Quantity    = order.Quantity;
                    doc1Definition.Status      = order.Status;
                    //dynamic doc1Definition = new
                    //{
                    //    title = "Star War IV ",
                    //    rank = 600,
                    //    category = "Sci-fi"
                    //};
                    var document1 = await client.CreateDocumentAsync(
                        UriFactory.CreateDocumentCollectionUri("EventMessagingDemo", "orders"),
                        doc1Definition);

                    //Console.WriteLine("\r\n>>>>>>>>>>>> Querying Document <<<<<<<<<<<<<<<<<<<<");
                    //var response = client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri("testDb", "testDocumentCollection"),
                    //    "select * from c").ToList();
                    //var document = response.First();
                    //Console.WriteLine($"Id:{document.id}");
                    //Console.WriteLine($"Title:{document.title}");
                    //Console.WriteLine($"Rank:{document.rank}");
                    //Console.WriteLine($"category:{document.category}");

                    //Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Deleteing Collection <<<<<<<<<<<<<<<<<<<");
                    //await client.DeleteDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri("testDb", "testDocumentCollection"));

                    //Console.ReadKey();
                }
            }).Wait();

            signalRMessages.AddAsync(
                new SignalRMessage
            {
                Target    = "productOrdered",
                Arguments = new[] { order }
            });
        }
        public static async Task <IActionResult> TokBlitzRaceGameOver(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = Constants.Version + "/tokblitzracegameover")] HttpRequest req,
            [SignalR(HubName = "multiplayer")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log)
        {
            //Read query info
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    info        = JsonConvert.DeserializeObject <TokBlitzRace>(requestBody);

            bool forfeit = false;

            if (info.ForfeitingPlayer != null)
            {
                info.Loser    = info.ForfeitingPlayer;
                info.LoserId  = info.ForfeiterId;
                info.Winner   = (info.ForfeitingPlayer.PlayerNumber == 1) ? info.Players[0]: info.Players[1];
                info.WinnerId = info.Winner.ConnectionId;

                forfeit = true;
            }
            else
            {
                if (info.FinishedFirst.PlayerNumber == 1)
                {
                    info.Player1Points += Constants.FINISHED_FIRST_BONUS;
                    if (info.Player1PointsPerRound != null)
                    {
                        info.Player1PointsPerRound.Add(Constants.FINISHED_FIRST_BONUS);
                    }
                }
                else if (info.FinishedFirst.PlayerNumber == 2)
                {
                    info.Player2Points += Constants.FINISHED_FIRST_BONUS;
                    if (info.Player2PointsPerRound != null)
                    {
                        info.Player2PointsPerRound.Add(Constants.FINISHED_FIRST_BONUS);
                    }
                }

                info.Winner   = (info.Player1Points > info.Player2Points) ? info.Players[0] : info.Players[1];
                info.WinnerId = info.Winner.ConnectionId;

                info.Loser   = (info.Player1Points > info.Player2Points) ? info.Players[1] : info.Players[0];
                info.LoserId = info.Loser.ConnectionId;
                if (info.Player1Points == info.Player2Points)
                {
                    info.Winner = new GamePlayer()
                    {
                        PlayerNumber = 0, UserId = "Tie", UserName = "******", UserPhoto = "Tie"
                    }
                }
                ;
            }

            // update the points and others

            tokblitzTeamClass getwinner = new tokblitzTeamClass();

            getwinner.Id          = info.Winner.ConnectionId;
            getwinner.GamesPlayed = 1;
            getwinner.Wins        = 1;
            getwinner.TeamPoints  = 2;


            tokblitzTeamClass getloser = new tokblitzTeamClass();

            getloser.Id          = info.Loser.ConnectionId;
            getloser.GamesPlayed = 1;
            getloser.Losses      = 1;
            getloser.TeamPoints  = (forfeit == true) ? 0 : 1;

            tokblitzTeamClass getwinnerOld = await Api <tokblitzTeamClass> .GetItemAsync(getwinner.Id, Constants.PkRequest(getwinner.Id));

            tokblitzTeamClass getwinnerTosend = TeamClassUpdater <tokblitzTeamClass> .TeamUpdater(getwinner, getwinnerOld);

            tokblitzTeamClass getLoserOld = await Api <tokblitzTeamClass> .GetItemAsync(getloser.Id, Constants.PkRequest(getloser.Id));

            tokblitzTeamClass getLoserTosend = TeamClassUpdater <tokblitzTeamClass> .TeamUpdater(getloser, getLoserOld);


            await Api <tokblitzTeamClass> .UpdateItemAsyncSaveGames(getwinner.Id, getwinnerTosend, Constants.PkRequest(getwinner.Id));

            await Api <tokblitzTeamClass> .UpdateItemAsyncSaveGames(getloser.Id, getLoserTosend, Constants.PkRequest(getloser.Id));


            // creates a rematch record after the game
            RematchRecord rematchRecord = new RematchRecord();
            Guid          newId;

            newId            = Guid.NewGuid();
            rematchRecord.Id = newId.ToString();
            rematchRecord.Pk = newId.ToString();
            rematchRecord.Player1ConnectionId = info.Players[0].ConnectionId.ToString();
            rematchRecord.Player2ConnectionId = info.Players[1].ConnectionId.ToString();
            rematchRecord.Player1Id           = info.Players[0].UserId.ToString();
            rematchRecord.Player2Id           = info.Players[1].UserId.ToString();

            await signalRMessages.AddAsync(
                new SignalRMessage
            {
                Target    = "tokblitzracegameover",
                Arguments = new[] { info }
            });


            await StatsFunctions.IncrementDayCounter(1);


            await Api <TokBlitzRace> .CreateItemAsync(info, Constants.PkRequest(info.Id));

            await Api <RematchRecord> .CreateItemAsync(rematchRecord, Constants.PkRequest(rematchRecord.Id));

            await signalRMessages.AddAsync(
                new SignalRMessage
            {
                Target    = "rematch_record",
                Arguments = new[] { rematchRecord }
            });



            return(new OkObjectResult(info));
        }
        public static async Task Run([TimerTrigger("%WebAppSSLManager-Trigger%"
#if DEBUG
                                                   , RunOnStartup = true
#endif
                                                   )] TimerInfo myTimer,
                                     [SendGrid(ApiKey = "SendGridKey")] IAsyncCollector <SendGridMessage> messageCollector,
                                     ILogger logger)
        {
            _logger = logger;
            logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            Settings.Init(logger);

            MailHelper.Init(logger, messageCollector);
            await MailHelper.SendEmailForActivityStartedAsync();

            AzureHelper.Init(logger);

            var errors        = new List <(string hostname, string errorMessage)>();
            var appProperties = await BuildAppPropertiesListAsync();

            int certsCreated = 0;

            if (appProperties != null && appProperties.Any())
            {
                await CertificatesHelper.InitAsync(logger, Settings.UseStaging?CertificateMode.Staging : CertificateMode.Production);

                foreach (var appProperty in appProperties)
                {
                    AzureHelper.InitAppProperty(appProperty);
                    CertificatesHelper.InitAppProperty(appProperty);

                    try
                    {
                        //Request certificate and install it if all is ok
                        if (await AzureHelper.NeedsNewCertificateAsync() && await CertificatesHelper.GetCertificateAsync())
                        {
                            await AzureHelper.AddCertificateAsync();

                            certsCreated++;
                        }
                    }
                    catch (Exception ex)
                    {
                        var message = $"Unable to complete the processing for {appProperty.Hostname}";
                        logger.LogError(ex, message);
                        await MailHelper.SendEmailForErrorAsync(ex, message);

                        errors.Add((hostname: appProperty.Hostname, errorMessage: ex.Message));
                    }

                    if (Settings.BatchSize > 0 && certsCreated >= Settings.BatchSize)
                    {
                        logger.LogInformation($"Maximum number of certificates ({Settings.BatchSize}) generated this run - exiting.");
                        break;
                    }
                }
            }

            AzureHelper.Dispose();
            await MailHelper.SendEmailForActivityCompletedAsync(errors);
        }
 public static Task <string> SendMessage(
     [HttpTrigger(AuthorizationLevel.Anonymous, "get")] object message,
     [SignalR(HubName = "chat")] IAsyncCollector <SignalRMessage> signalRMessages)
 {
     return(Task.Run(() => { return System.Guid.NewGuid().ToString().Substring(0, 5); }));
 }
Esempio n. 8
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, [Queue("vehicle")] IAsyncCollector <Vehicle> vehicleQueue,
            ILogger log)
        {
            log.LogInformation("Vehical request sent.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    vehicle     = JsonConvert.DeserializeObject <Vehicle>(requestBody);
            await vehicleQueue.AddAsync(vehicle);

            double FinalPrice = (vehicle.ListPrice * 0.915);

            Math.Round(FinalPrice, 2);

            log.LogInformation($"{vehicle.BuyerLast}, {vehicle.BuyerFirst} got a {vehicle.year} {vehicle.Make} {vehicle.Model} with a list price of ${vehicle.ListPrice}. This is a {vehicle.vehicleType} with a final price of ${FinalPrice}. Buyer Email is {vehicle.Email}");

            string responseMessage = ($"Buyer {vehicle.BuyerFirst} {vehicle.BuyerLast} ({vehicle.Email}) purchased a {vehicle.year} {vehicle.Make} {vehicle.Model} {vehicle.vehicleType} with a list price of ${vehicle.ListPrice}. with discount applied, purchase price is ${FinalPrice}.");

            return(new OkObjectResult(responseMessage));
        }
Esempio n. 9
0
        private async Task StartConsumer(ISourceBlock <EventData> producer, IAsyncCollector <IMeasurement> collector, Func <Exception, EventData, Task <bool> > errorConsumer)
        {
            // Collect non operation canceled exceptions as they occur to ensure the entire data stream is processed
            var exceptions = new ConcurrentBag <Exception>();
            var cts        = new CancellationTokenSource();
            var consumer   = new ActionBlock <EventData>(
                async evt =>
            {
                try
                {
                    _log.LogMetric(
                        IomtMetrics.DeviceEventProcessingLatency(),
                        (DateTime.UtcNow - evt.SystemProperties.EnqueuedTimeUtc).TotalSeconds);

                    _log.LogMetric(
                        IomtMetrics.DeviceEventProcessingLatencyMs(),
                        (DateTime.UtcNow - evt.SystemProperties.EnqueuedTimeUtc).TotalMilliseconds);

                    var token = _converter.Convert(evt);

                    foreach (var measurement in _contentTemplate.GetMeasurements(token))
                    {
                        measurement.IngestionTimeUtc = evt.SystemProperties.EnqueuedTimeUtc;
                        await collector.AddAsync(measurement).ConfigureAwait(false);

                        _log.LogMetric(
                            IomtMetrics.NormalizedEvent(),
                            1);
                    }
                }
                catch (OperationCanceledException)
                {
                    cts.Cancel();
                    throw;
                }
#pragma warning disable CA1031
                catch (Exception ex)
                {
                    if (await errorConsumer(ex, evt).ConfigureAwait(false))
                    {
                        exceptions.Add(ex);
                    }
                }
#pragma warning restore CA1031
            },
                new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = _maxParallelism, SingleProducerConstrained = true, CancellationToken = cts.Token
            });

            _ = producer.LinkTo(consumer, new DataflowLinkOptions {
                PropagateCompletion = true
            });

            await consumer.Completion
            .ContinueWith(
                task =>
            {
                if (!exceptions.IsEmpty)
                {
                    throw new AggregateException(exceptions);
                }
            },
                cts.Token,
                AsyncContinueOnSuccess,
                TaskScheduler.Current)
            .ConfigureAwait(false);
        }
Esempio n. 10
0
        public static async Task Run([ActivityTrigger] RecognitionOrder recognitionOrder, [Queue("modeltraining", Connection = "StorageConnectionString")] IAsyncCollector <RecognitionOrder> queue)
        {
            await queue.AddAsync(recognitionOrder);

            await queue.FlushAsync();
        }
Esempio n. 11
0
        public static async Task Run([TimerTrigger("0 */15 * * * *")] TimerInfo myTimer,
                                     [Blob("spitball/AzureSearch/tutor-version.txt")] CloudBlockBlob blob,
                                     [AzureSearchSync(TutorSearchWrite.IndexName)] IAsyncCollector <AzureSearchSyncOutput> indexInstance,
                                     [Inject] IQueryBus queryBus,
                                     ILogger log,
                                     CancellationToken token)
        {
            var query = new TutorSyncAzureSearchQuery(0);

            if (await blob.ExistsAsync())
            {
                var str = await blob.DownloadTextAsync();

                query = JsonConvert.DeserializeObject <TutorSyncAzureSearchQuery>(str);
            }


            var nextQuery = new TutorSyncAzureSearchQuery(query.Version);

            bool updateOccur;

            do
            {
                updateOccur = false;
                var result = await queryBus.QueryAsync(query, token);

                foreach (var update in result.Update)
                {
                    log.LogInformation($"Sync {update}");
                    updateOccur = true;
                    var courses = update.Courses?.Where(w => !string.IsNullOrWhiteSpace(w)).Distinct().ToArray() ??
                                  new string[0];
                    var subjects = update.Subjects?.Where(w => !string.IsNullOrWhiteSpace(w)).ToArray() ?? new string[0];
                    await indexInstance.AddAsync(new AzureSearchSyncOutput()
                    {
                        Item = new Tutor
                        {
                            Country = update.Country.ToUpperInvariant(),
                            Id      = update.UserId.ToString(),
                            Name    = update.Name,
                            Courses = courses.ToArray(),
                            Rate    = update.Rate,

                            InsertDate = DateTime.UtcNow,
                            Prefix     = courses.Union(subjects).Union(new[] { update.Name })
                                         .Distinct(StringComparer.OrdinalIgnoreCase).ToArray(),
                            ReviewCount   = update.ReviewsCount,
                            Subjects      = subjects.ToArray(),
                            OverAllRating = update.OverAllRating,
                            Data          = new TutorCardDto()
                            {
                                UserId         = update.UserId,
                                Name           = update.Name,
                                Courses        = courses.Take(3),
                                Subjects       = subjects.OrderBy(o => o).Take(3),
                                ReviewsCount   = update.ReviewsCount,
                                Rate           = (float)update.Rate,
                                University     = update.University,
                                Lessons        = Math.Max(update.LessonsCount, update.ReviewsCount),
                                Bio            = update.Bio,
                                Price          = update.Price,
                                Country        = update.Country,
                                Image          = update.Image,
                                NeedSerializer = true,
                                DiscountPrice  = update.SubsidizedPrice
                            }
                        },
                        Insert = true
                    }, token);
                }

                foreach (var delete in result.Delete)
                {
                    log.LogInformation($"delete tutor id {delete}");
                    updateOccur = true;
                    await indexInstance.AddAsync(new AzureSearchSyncOutput()
                    {
                        Item = new Tutor()
                        {
                            Id = delete
                        },
                        Insert = false
                    }, token);
                }

                query.Page++;
                nextQuery.Version = Math.Max(nextQuery.Version, result.Version);
                await indexInstance.FlushAsync(token);
            } while (updateOccur);

            if (query.Page > 0)
            {
                var jsonStr = JsonConvert.SerializeObject(nextQuery);
                await blob.UploadTextAsync(jsonStr);
            }

            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        }
Esempio n. 12
0
        public static async Task Run([ServiceBusTrigger("%InvoiceQueueName%", Connection = "InvoicingQueueConnectionString")] string myQueueItem, [ServiceBus("%InvoiceResponseQueueName%", Connection = "InvoicingQueueConnectionString")] IAsyncCollector <InvoiceResponse> output, ILogger log, ExecutionContext context)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();
            var response = await ProcessInvoice(myQueueItem, log, config);

            await output.AddAsync(response);
        }
Esempio n. 13
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "ratings")] HttpRequestMessage req,
                                                           [DocumentDB(
                                                                databaseName: "OpenHack",
                                                                collectionName: "Ratings",
                                                                ConnectionStringSetting = "CosmosDBConnection")]
                                                           IAsyncCollector <Rating> ratingsOut,
                                                           TraceWriter log)
        {
            NewRating newRating;

            try
            {
                dynamic body = await req.Content.ReadAsStringAsync();

                newRating = JsonConvert.DeserializeObject <NewRating>(body as string);
            }
            catch
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "wrong payload"));
            }

            //try
            //{
            //    var json = await client.GetStringAsync($"https://hacker1.azurewebsites.net/api/users/{newRating.userId}");
            //    var user = JsonConvert.DeserializeObject<User>(json);
            //}
            //catch
            //{
            //    return req.CreateErrorResponse(HttpStatusCode.BadRequest, "invalid userId");
            //}

            //try
            //{
            //    var json = await client.GetStringAsync($"https://hacker1.azurewebsites.net/api/products/{newRating.productId}");
            //    var user = JsonConvert.DeserializeObject<Product>(json);
            //}
            //catch
            //{
            //    return req.CreateErrorResponse(HttpStatusCode.BadRequest, "invalid productId");
            //}

            if (newRating.rating <= 5 && newRating.rating >= 0)
            {
                var rating = new Rating
                {
                    id           = Guid.NewGuid().ToString(),
                    rating       = newRating.rating,
                    locationName = newRating.locationName,
                    productId    = newRating.productId,
                    timestamp    = DateTime.UtcNow.ToString("u"),
                    userId       = newRating.userId,
                    userNotes    = newRating.userNotes
                };

                ITextAnalyticsClient client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials())
                {
                    Endpoint = "https://southcentralus.api.cognitive.microsoft.com"
                };

                var result = await client.SentimentAsync(
                    new MultiLanguageBatchInput(
                        new List <MultiLanguageInput>
                {
                    new MultiLanguageInput("en", "0", rating.userNotes)
                }));

                rating.sentimentScore = result.Documents.First().Score;

                await ratingsOut.AddAsync(rating);

                telemetryClient.TrackEvent("Rating", new Dictionary <string, string>()
                {
                    { "id", rating.id },
                    { "productId", rating.productId },
                    { "userNotes", rating.userNotes }
                }, new Dictionary <string, double>
                {
                    { "sentimentScore", rating.sentimentScore ?? 0d }
                });

                return(req.CreateResponse(HttpStatusCode.OK, rating));
            }

            return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "invalid rating"));
        }
        public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, [CosmosDB(databaseName: "Twitter", collectionName: "Tweets", ConnectionStringSetting = "CosmosDBConnection")] IAsyncCollector <Tweet> twitterItemsOut, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            try
            {
                client = new TwitterClient(System.Environment.GetEnvironmentVariable("consumerKey"), System.Environment.GetEnvironmentVariable("consumerSecret"), System.Environment.GetEnvironmentVariable("accessKey"));
            }
            catch (Exception ex)
            {
                log.LogError(ex, ex.Message);
                throw;
            }

            string userName = System.Environment.GetEnvironmentVariable("TwitterUser");

            var tweets = await GetUserTimelineTweets(userName, log).ConfigureAwait(false);

            foreach (var tweet in tweets)
            {
                Tweet item = new Tweet()
                {
                    username  = userName,
                    content   = tweet.FullText,
                    id        = Convert.ToString(tweet.Id),
                    createdAt = tweet.CreatedAt,
                    isRetweet = tweet.IsRetweet
                };

                await twitterItemsOut.AddAsync(item).ConfigureAwait(false);
            }

            log.LogInformation($"Added {tweets.Length} entries to the database");
        }
Esempio n. 15
0
 public static async Task AddToQueue(
     [ActivityTrigger] string message,
     [Queue(Global.QUEUE)] IAsyncCollector <string> console)
 {
     await console.AddAsync(message);
 }
Esempio n. 16
0
    public async Task <ICommandResult> HandleAsync(DeploymentScopeCreateCommand command, IAsyncCollector <ICommand> commandQueue, IDurableOrchestrationContext orchestrationContext, ILogger log)
    {
        if (command is null)
        {
            throw new ArgumentNullException(nameof(command));
        }

        if (commandQueue is null)
        {
            throw new ArgumentNullException(nameof(commandQueue));
        }

        var commandResult = command.CreateResult();

        try
        {
            commandResult.Result = await deploymentScopeRepository
                                   .AddAsync(command.Payload)
                                   .ConfigureAwait(false);

            if (adapterProvider.GetAdapter(commandResult.Result.Type) is IAdapterIdentity adapterIdentity)
            {
                var servicePrincipal = await adapterIdentity
                                       .GetServiceIdentityAsync(commandResult.Result)
                                       .ConfigureAwait(false);

                var servicePrincipalUser = await userRepository
                                           .GetAsync(commandResult.Result.Organization, servicePrincipal.Id.ToString())
                                           .ConfigureAwait(false);

                if (servicePrincipalUser is null)
                {
                    servicePrincipalUser ??= new User
                    {
                        Id               = servicePrincipal.Id.ToString(),
                        Role             = OrganizationUserRole.Adapter,
                        UserType         = Model.Data.UserType.Service,
                        Organization     = commandResult.Result.Organization,
                        OrganizationName = commandResult.Result.OrganizationName
                    };

                    await commandQueue
                    .AddAsync(new OrganizationUserCreateCommand(command.User, servicePrincipalUser))
                    .ConfigureAwait(false);
                }
            }

            commandResult.RuntimeStatus = CommandRuntimeStatus.Completed;
        }
        catch (Exception exc)
        {
            commandResult.Errors.Add(exc);
        }

        return(commandResult);
    }
Esempio n. 17
0
            public async Task Func2([SignalR(HubName = DefaultHubName, ConnectionStringSetting = AttrConnStrConfigKey)] IAsyncCollector <SignalRMessage> signalRMessages)
            {
                await SimulateSendingMessage(signalRMessages);

                Assert.NotNull(((ServiceManagerStore)StaticServiceHubContextStore.ServiceManagerStore).GetByConfigurationKey(AttrConnStrConfigKey));
            }
Esempio n. 18
0
        public async Task Run <T1, T2>(
            string[] messages,
            IBinder blobFaultBinder,
            Binder queueFaultBinder,
            IBinder incomingBatchBinder,
            IAsyncCollector <string> outputEvents,
            ILogger log,
            ExecutionContext context)
        {
            var batchId = Guid.NewGuid().ToString();

            bool logIncoming = Utils.GetEnvironmentVariable("logIncomingBatches").ToLower() == "true";

            var           azMonMsgs  = (AzMonMessages)Activator.CreateInstance(typeof(T1), log);
            List <string> decomposed = null;

            try
            {
                decomposed = azMonMsgs.DecomposeIncomingBatch(messages);

                if (logIncoming)
                {
                    try
                    {
                        var blobWriter = await incomingBatchBinder.BindAsync <CloudBlockBlob>(
                            new BlobAttribute($"transmission-incoming/{batchId}", FileAccess.ReadWrite));

                        await blobWriter.UploadTextAsync(String.Join(",", messages));
                    }
                    catch (Exception exIncomingBlob)
                    {
                        log.LogError($"Failed to log the incoming transmission blob: {batchId}. {exIncomingBlob.Message}");
                        throw exIncomingBlob;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            log.LogInformation($"Decomposition count messages: {decomposed.Count}");

            if (decomposed.Count > 0)
            {
                var splunkMsgs = (SplunkEventMessages)Activator.CreateInstance(typeof(T2), outputEvents, log, context);
                try
                {
                    splunkMsgs.Ingest(decomposed.ToArray());
                    await splunkMsgs.Emit();
                }
                catch (Exception exEmit)
                {
                    try
                    {
                        var blobWriter = await blobFaultBinder.BindAsync <CloudBlockBlob>(
                            new BlobAttribute($"transmission-faults/{batchId}", FileAccess.ReadWrite));

                        string json = await Task <string> .Factory.StartNew(() => JsonConvert.SerializeObject(splunkMsgs.SplunkMessages));

                        await blobWriter.UploadTextAsync(json);
                    }
                    catch (Exception exFaultBlob)
                    {
                        log.LogError($"Failed to write the fault blob: {batchId}. {exFaultBlob.Message}");
                        throw exFaultBlob;
                    }

                    try
                    {
                        var qMsg = new TransmissionFaultMessage {
                            id = batchId, type = typeof(T2).ToString()
                        };
                        string qMsgJson = JsonConvert.SerializeObject(qMsg);

                        var queueWriter = await queueFaultBinder.BindAsync <CloudQueue>(
                            new QueueAttribute("transmission-faults"));

                        await queueWriter.AddMessageAsync(new CloudQueueMessage(qMsgJson));
                    }
                    catch (Exception exFaultQueue)
                    {
                        log.LogError($"Failed to write the fault queue: {batchId}. {exFaultQueue.Message}");
                        throw exFaultQueue;
                    }

                    log.LogError($"Error emitting messages to output binding: {exEmit.Message}. The messages were held in the fault processor queue for handling once the error is resolved.");
                    throw exEmit;
                }
            }

            log.LogInformation($"C# Event Hub trigger function processed a batch of messages: {messages.Length}");
        }
Esempio n. 19
0
    public async Task <ICommandResult> HandleAsync(DeploymentScopeDeleteCommand command, IAsyncCollector <ICommand> commandQueue, IDurableOrchestrationContext orchestrationContext, ILogger log)
    {
        if (command is null)
        {
            throw new ArgumentNullException(nameof(command));
        }

        if (commandQueue is null)
        {
            throw new ArgumentNullException(nameof(commandQueue));
        }

        var commandResult = command.CreateResult();

        try
        {
            commandResult.Result = await deploymentScopeRepository
                                   .RemoveAsync(command.Payload)
                                   .ConfigureAwait(false);

            commandResult.RuntimeStatus = CommandRuntimeStatus.Completed;
        }
        catch (Exception exc)
        {
            commandResult.Errors.Add(exc);
        }

        return(commandResult);
    }
Esempio n. 20
0
        public static async Task <ClaimDocument> Run(
            [HttpTrigger(
                 WebHookType = "genericJson")] HttpRequestMessage req,
            [DocumentDB(
                 databaseName: "ContosoInc",
                 collectionName: "Claims",
                 ConnectionStringSetting = "CosmosDBConnection")] IAsyncCollector <ClaimDocument> document,
            TraceWriter log)
        {
            try
            {
                log.Info($"Webhook was triggered!");

                string jsonContent = await req.Content.ReadAsStringAsync();

                dynamic data = JsonConvert.DeserializeObject(jsonContent);

                string imageUrl = data.imageUrl;
                string text     = data.text;

                #region normalize claim
                string normalizedText = Regex.Replace(text, @"\. *([a-z0-9])", ". $1", RegexOptions.IgnoreCase);
                #endregion

                #region Get claim summary
                string summary;

                if (normalizedText.Split('.').Count() > 3)
                {
                    using (var client = new HttpClient())
                    {
                        var body = new
                        {
                            txt = normalizedText,
                        };
                        var content  = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
                        var response = await client.PostAsync(TextSummaryEndpoint, content);

                        if (!response.IsSuccessStatusCode)
                        {
                            throw new Exception($"Failed to get claim summary. Status code: {response.StatusCode}. Reason phrase: {response.ReasonPhrase}");
                        }

                        var responseString = await response.Content.ReadAsStringAsync();

                        string[] summaries = JsonConvert.DeserializeObject <string[]>(responseString);
                        summary = summaries[0];
                    }
                }
                else
                {
                    summary = normalizedText;
                }
                #endregion

                #region Get claim sentiment
                double sentiment;

                using (var client = new HttpClient())
                {
                    var body    = new TextAnalytics.Request(1, normalizedText);
                    var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", TextAnalyticsApiKey);
                    var response = await client.PostAsync(TextAnalyticsEndpoint, content);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new Exception($"Failed to get claim sentiment. Status code: {response.StatusCode}. Reason phrase: {response.ReasonPhrase}");
                    }

                    string responseString = await response.Content.ReadAsStringAsync();

                    var textAnalyticsResponse = JsonConvert.DeserializeObject <TextAnalytics.Response>(responseString);

                    sentiment = textAnalyticsResponse.documents.Where(x => x.id == 1).FirstOrDefault().score;
                }
                #endregion

                #region Get image tags
                string[] tags;

                using (var client = new HttpClient())
                {
                    var body    = new ComputerVision.Request(imageUrl);
                    var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ComputerVisionApiKey);
                    var response = await client.PostAsync(ComputerVisionEndpoint, content);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new Exception($"Failed to get image tags. Status code: {response.StatusCode}. Reason phrase: {response.ReasonPhrase}");
                    }

                    string responseString = await response.Content.ReadAsStringAsync();

                    var computerVisionResponse = JsonConvert.DeserializeObject <ComputerVision.Response>(responseString);

                    tags = computerVisionResponse.tags.Where(x => x.confidence > ComputerVisionConfidence).Select(x => x.name).ToArray();
                }
                #endregion

                #region Get image category
                double autoProbability;
                double homeProbability;

                using (var client = new HttpClient())
                {
                    var body    = new ComputerVision.Request(imageUrl);
                    var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
                    client.DefaultRequestHeaders.Add("Prediction-Key", CustomVisionApiKey);
                    var response = await client.PostAsync(CustomVisionEndpoint, content);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new Exception($"Failed to classify image. Status code: {response.StatusCode}. Reason phrase: {response.ReasonPhrase}");
                    }

                    string responseString = await response.Content.ReadAsStringAsync();

                    var customVisionResponse = JsonConvert.DeserializeObject <CustomVision.Response>(responseString);

                    autoProbability = customVisionResponse.predictions.Where(x => string.Equals(x.tagName, "auto", StringComparison.OrdinalIgnoreCase)).First().probability;
                    homeProbability = customVisionResponse.predictions.Where(x => string.Equals(x.tagName, "home", StringComparison.OrdinalIgnoreCase)).First().probability;
                }
                #endregion

                #region Store in DB
                ClaimDocument claimDocument = new ClaimDocument()
                {
                    Guid            = Guid.NewGuid().ToString(),
                    ImageUrl        = imageUrl,
                    Claim           = normalizedText,
                    Summary         = summary,
                    Sentiment       = sentiment,
                    AutoProbability = autoProbability,
                    HomeProbability = homeProbability,
                    Tags            = tags,
                };

                await document.AddAsync(claimDocument);

                #endregion

                #region Update indexer
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("api-key", SearchApiKey);
                    var response = await client.PostAsync(RunIndexerEndpoint, null);

                    if (response.StatusCode != HttpStatusCode.Accepted)
                    {
                        throw new Exception($"Failed to run indexer. Status code: {response.StatusCode}. Reason phrase: {response.ReasonPhrase}");
                    }
                }
                #endregion

                return(claimDocument);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Esempio n. 21
0
 public WadSplunkEventMessages(IAsyncCollector <string> outputEvents, ILogger log, ExecutionContext context) : base(outputEvents, log, context)
 {
 }
Esempio n. 22
0
        public static SendFlightDataSignalR2_SupportOutput SendFlightDataSignalR2_Support(int index, int count,
                                                                                          ExecutionContext context,
                                                                                          //[ActivityTrigger]object input,
                                                                                          //[CosmosDBTrigger("demo", "flights", ConnectionStringSetting = "AzureWebJobsCosmosDBConnectionString")]
                                                                                          IAsyncCollector <SignalRMessage> signalRMessages,
                                                                                          ILogger logger, bool allplanes = false)
        {
            var list = ReadFlightData(context);

            var dictionarytime = new Dictionary <long, List <FlightData> >();

            foreach (var item in list)
            {
                if (dictionarytime.ContainsKey(item.time))
                {
                    dictionarytime[item.time].Add(item);
                }
                else
                {
                    dictionarytime.Add(item.time, new List <FlightData>());
                    dictionarytime[item.time].Add(item);
                }
            }

            var dictionaryplanes = new Dictionary <string, List <FlightData> >();

            foreach (var item in list)
            {
                if (dictionaryplanes.ContainsKey(item.icao24))
                {
                    dictionaryplanes[item.icao24].Add(item);
                }
                else
                {
                    dictionaryplanes.Add(item.icao24, new List <FlightData>());
                    dictionaryplanes[item.icao24].Add(item);
                }
            }

            var orderedset    = dictionaryplanes.OrderByDescending(item => item.Value.Count);
            var limitedplanes = orderedset.Take(20).Select(i => i.Key).ToHashSet();
            var timeitem      = dictionarytime.Skip(index).Take(1).First();

            var resultlist = new List <FlightData>();

            foreach (var flight in timeitem.Value)
            {
                if (limitedplanes.Contains(flight.icao24))
                {
                    resultlist.Add(flight);
                    if (!allplanes)
                    {
                        signalRMessages.AddAsync(new SignalRMessage
                        {
                            Target    = "flightEvent",
                            Arguments = new[] { flight }
                        });
                    }
                }
            }
            if (allplanes)
            {
                signalRMessages.AddAsync(new SignalRMessage
                {
                    Target    = "flightEvent",
                    Arguments = new[] { resultlist }
                });
            }



            return(new SendFlightDataSignalR2_SupportOutput {
                time = timeitem.Key,
                Next = (index + 15) % dictionarytime.Count,
                Count = resultlist.Count,
                Result = resultlist
            });
        } // method
Esempio n. 23
0
        public static async Task <IActionResult> MultiplayerSetup(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = Constants.Version + "/multiplayersetup")] HttpRequest req,
            [SignalR(HubName = "multiplayer")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log)
        {
            //Read query info
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    player      = JsonConvert.DeserializeObject <GamePlayer>(requestBody);

            //Get list of players
            Constants.CollectionId = "Games";
            var lobby = await Api <TokGamesRoomBasic> .GetItemAsync("lobby", Constants.PkRequest("lobby"));

            lobby.WaitingPlayers.RemoveAll(x => x.UserId == player.UserId);

            if (lobby.WaitingPlayers.Count == 0)
            {   //Wait for other player
                lobby.WaitingPlayers.Add(player);


                await Api <TokGamesRoomBasic> .UpdateItemAsyncSaveGames("lobby", lobby, Constants.PkRequest("lobby"));

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    Target    = "waiting",
                    Arguments = new[] { player }
                });

                // await StatsFunctions.IncrementDayCounter(1);
            }
            else
            {
                //Match up with a waiting player
                var player1 = player;
                var player2 = lobby.WaitingPlayers.FirstOrDefault();

                TokBlitzRace gameInfo = new TokBlitzRace();
                player1.PlayerNumber = 1;
                player2.PlayerNumber = 2;
                gameInfo.Players.Add(player1);
                gameInfo.Players.Add(player2);
                gameInfo.RoomId = Guid.NewGuid().ToString();

                //Get game toks
                gameInfo.Toks = GetGameTokIds();

                //Remove once successful
                lobby.WaitingPlayers.RemoveAll(x => x.UserId == player2.UserId);
                await Api <TokGamesRoomBasic> .UpdateItemAsyncSaveGames("lobby", lobby, Constants.PkRequest("lobby"));

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    Target = "newgame",

                    Arguments = new[] { gameInfo }
                });

                // await StatsFunctions.IncrementDayCounter(1);
            }

            return(new OkResult());
        }
        public static async Task Run([TimerTrigger("0 8 0 * * *")] TimerInfo myTimer,
                                     [CosmosDB(
                                          databaseName: "Shared-Free",
                                          collectionName: "V1-pmdboard",
                                          ConnectionStringSetting = "CosmosDBConnection",
                                          Id = "internal-Multiple-Mystery-Dungeon",
                                          PartitionKey = "internal-Multiple-Mystery-Dungeon"
                                          )] V1GameMetadata runInfo,
                                     [CosmosDB(
                                          databaseName: "Shared-Free",
                                          collectionName: "V1-pmdboard",
                                          ConnectionStringSetting = "CosmosDBConnection"
                                          )] IAsyncCollector <V1CombinedRuns> entries, ILogger log)
        {
            log.LogInformation($"PMD Series updater function started execution at: {DateTime.Now}");

            var gameInfo = new V1CombinedRuns
            {
                id         = "gameinfo-PMD-Series",
                Title      = "Multiple Mystery Dungeon Games",
                Categories = new List <Category>()
            };

            foreach (var category in runInfo.Categories)
            {
                var categoryInfo     = category.Split('-');
                var internalCategory = new Category
                {
                    Name = categoryInfo[1],
                    Runs = new List <InternalRun>()
                };

                string url      = $"https://speedrun.com/api/v1/leaderboards/{runInfo.GameID}/category/{categoryInfo[0]}?top=1&embed=players";
                var    response = await FunctionHttpClient.httpClient.GetAsync(url);

                var resStream = await response.Content.ReadAsStreamAsync();

                Response result = await JsonSerializer.DeserializeAsync <Response>(resStream);

                if (response.IsSuccessStatusCode)
                {
                    if (result.ResponseBody.Players.PlayerList.Count > 0 && result.ResponseBody.RunList.Count > 0)
                    {
                        string playerName;
                        if (string.Equals(result.ResponseBody.Players.PlayerList[0].Role, "guest", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var jsonString = await response.Content.ReadAsStringAsync();

                            playerName = Utils.GetGuestUser(jsonString);
                        }
                        else
                        {
                            playerName = !string.IsNullOrEmpty(result.ResponseBody.Players.PlayerList[0].Names.Name) ?
                                         result.ResponseBody.Players.PlayerList[0].Names.Name :
                                         result.ResponseBody.Players.PlayerList[0].Names.NameJP;
                        }

                        var runTime = result.ResponseBody.RunList[0].Run.Times.PrimaryTime;
                        var runDate = result.ResponseBody.RunList[0].Run.RunDate;
                        var srcID   = result.ResponseBody.RunList[0].Run.Id;

                        var internalRow = new InternalRun
                        {
                            Language = "ENG",
                            Version  = string.Empty,
                            Runner   = playerName,
                            RunDate  = runDate,
                            RunTime  = runTime,
                            SRCLink  = srcID
                        };
                        internalCategory.Runs.Add(internalRow);
                    }
                }
                else
                {
                    log.LogError($"Request to {url} failed. Error code: {response.StatusCode}");
                }
                gameInfo.Categories.Add(internalCategory);
            }
            await entries.AddAsync(gameInfo);
        }
Esempio n. 25
0
        public static async Task <IActionResult> TokblitzRaceRematch(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = Constants.Version + "/tokblitzracerematch/" + "{id}/" + "{answer}")] HttpRequest req, string id, bool answer,
            [SignalR(HubName = "multiplayer")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log)
        {
            //Read query info
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            // creates a new gameplyer from rquest body
            var Record = JsonConvert.DeserializeObject <GamePlayer>(requestBody);

            //Get the record rematch and put in the FetchedRecord variable
            Constants.CollectionId = "Games";
            var FetchedRecord = await Api <RematchRecord> .GetItemAsync(id, Constants.PkRequest(id));

            // checks if the userid of the one asking for rematch is the same with the userId player1 in the rematch record
            if (Record.UserId == FetchedRecord.Player1Id)
            {
                if (answer == true)
                {
                    // if true,  update the rematch record set the value of player1rematch to "true"
                    FetchedRecord.Player1Rematch = true;
                    // if yes,  update the rematch record set the value of player1rematch to "true"
                    FetchedRecord.Player1HasMadeDecision = true;
                    await Api <RematchRecord> .UpdateItemAsyncSaveGames(FetchedRecord.Id, FetchedRecord, Constants.PkRequest(FetchedRecord.Id));
                }
                else
                {
                    // if false,  update the rematch record set the value of player1rematch to "false"
                    FetchedRecord.Player1Rematch = false;
                    // if false,  update the rematch record set the value of player1rematch to "true"
                    FetchedRecord.Player1HasMadeDecision = true;
                    await Api <RematchRecord> .UpdateItemAsyncSaveGames(FetchedRecord.Id, FetchedRecord, Constants.PkRequest(FetchedRecord.Id));
                }
            }
            else if (Record.UserId == FetchedRecord.Player2Id)
            {
                if (answer == true)
                {
                    // if true,  update the rematch record set the value of player1rematch to "true"
                    FetchedRecord.Player2Rematch = true;
                    // if yes,  update the rematch record set the value of player1rematch to "true"
                    FetchedRecord.Player2HasMadeDecision = true;
                    await Api <RematchRecord> .UpdateItemAsyncSaveGames(FetchedRecord.Id, FetchedRecord, Constants.PkRequest(FetchedRecord.Id));
                }
                else
                {
                    // if false,  update the rematch record set the value of player1rematch to "false"
                    FetchedRecord.Player2Rematch = false;
                    // if false,  update the rematch record set the value of player1rematch to "true"
                    FetchedRecord.Player2HasMadeDecision = true;
                    await Api <RematchRecord> .UpdateItemAsyncSaveGames(FetchedRecord.Id, FetchedRecord, Constants.PkRequest(FetchedRecord.Id));
                }
            }


            var UpdatedFetchedRecord = await Api <RematchRecord> .GetItemAsync(id, Constants.PkRequest(id));


            // if both have decided
            if (UpdatedFetchedRecord.Player1HasMadeDecision == true && UpdatedFetchedRecord.Player2HasMadeDecision == true)
            {
                // if both yes
                if (UpdatedFetchedRecord.Player1Rematch == true && UpdatedFetchedRecord.Player2Rematch == true)
                {
                    TokBlitzRace gameInfo = new TokBlitzRace();
                    var          play1    = UpdatedFetchedRecord.Players[0];
                    play1.PlayerNumber  = 1;
                    Record.PlayerNumber = 2;
                    //var play2 = UpdatedFetchedRecord.Players[1];
                    gameInfo.Players.Add(play1);
                    gameInfo.Players.Add(Record);
                    gameInfo.RoomId = Guid.NewGuid().ToString();

                    UpdatedFetchedRecord.Player1Rematch         = false;
                    UpdatedFetchedRecord.Player2Rematch         = false;
                    UpdatedFetchedRecord.Player2HasMadeDecision = false;
                    UpdatedFetchedRecord.Player1HasMadeDecision = false;


                    await Api <RematchRecord> .UpdateItemAsyncSaveGames(UpdatedFetchedRecord.Id, UpdatedFetchedRecord, Constants.PkRequest(UpdatedFetchedRecord.Id));


                    //Get game toks
                    gameInfo.Toks = GetGameTokIds();
                    await signalRMessages.AddAsync(
                        new SignalRMessage
                    {
                        Target    = "new_game_rematch",
                        Arguments = new[] { gameInfo }
                    });


                    // if both have decided and player 1 is false and player 2 is true
                }
                else if (UpdatedFetchedRecord.Player1Rematch == false && UpdatedFetchedRecord.Player2Rematch == true)
                {
                    var getter = UpdatedFetchedRecord.Player2ConnectionId;
                    await signalRMessages.AddAsync(
                        new SignalRMessage
                    {
                        Target    = "new_game_rematch_denied",
                        Arguments = new[] { getter }
                    });


                    // if both have decided and player 2 is false and player 1 is true
                }
                else if (UpdatedFetchedRecord.Player2Rematch == false && UpdatedFetchedRecord.Player1Rematch == true)
                {
                    var getter = UpdatedFetchedRecord.Player1ConnectionId;
                    await signalRMessages.AddAsync(
                        new SignalRMessage
                    {
                        Target    = "new_game_rematch_denied",
                        Arguments = new[] { getter }
                    });
                }
            }
            // if only one player has decided
            else
            {
                UpdatedFetchedRecord.Players.Add(Record);

                var updated = await Api <RematchRecord> .UpdateItemAsyncSaveGames(UpdatedFetchedRecord.Id, UpdatedFetchedRecord, Constants.PkRequest(UpdatedFetchedRecord.Id));

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    Target    = "waiting_for_rematch",
                    Arguments = new[] { "Please Wait for your opponent" }
                });

                return(new OkObjectResult(updated));
            }


            return(new OkResult());
        }
Esempio n. 26
0
        public static async Task ProcessTripExternalizations2SignalR([EventGridTrigger] EventGridEvent eventGridEvent,
                                                                     [SignalR(HubName = "trips")] IAsyncCollector <SignalRMessage> signalRMessages,
                                                                     ILogger log)
        {
            log.LogInformation($"ProcessTripExternalizations2SignalR triggered....EventGridEvent" +
                               $"\n\tId:{eventGridEvent.Id}" +
                               $"\n\tTopic:{eventGridEvent.Topic}" +
                               $"\n\tSubject:{eventGridEvent.Subject}" +
                               $"\n\tType:{eventGridEvent.EventType}" +
                               $"\n\tData:{eventGridEvent.Data}");

            try
            {
                TripItem trip = JsonConvert.DeserializeObject <TripItem>(eventGridEvent.Data.ToString());
                if (trip == null)
                {
                    throw new Exception("Trip is null!");
                }

                log.LogInformation($"ProcessTripExternalizations2SignalR trip code {trip.Code}");

                // Convert the `event subject` to a method to be called on clients
                var clientMethod = "tripUpdated";
                if (eventGridEvent.Subject == Constants.EVG_SUBJECT_TRIP_DRIVERS_NOTIFIED)
                {
                    clientMethod = "tripDriversNotified";
                }
                else if (eventGridEvent.Subject == Constants.EVG_SUBJECT_TRIP_DRIVER_PICKED)
                {
                    clientMethod = "tripDriverPicked";
                }
                else if (eventGridEvent.Subject == Constants.EVG_SUBJECT_TRIP_STARTING)
                {
                    clientMethod = "tripStarting";
                }
                else if (eventGridEvent.Subject == Constants.EVG_SUBJECT_TRIP_RUNNING)
                {
                    clientMethod = "tripRunning";
                }
                else if (eventGridEvent.Subject == Constants.EVG_SUBJECT_TRIP_COMPLETED)
                {
                    clientMethod = "tripCompleted";
                }
                else if (eventGridEvent.Subject == Constants.EVG_SUBJECT_TRIP_ABORTED)
                {
                    clientMethod = "tripAborted";
                }

                log.LogInformation($"ProcessTripExternalizations2SignalR firing SignalR `{clientMethod}` client method!");
                await signalRMessages.AddAsync(new SignalRMessage
                {
                    UserId    = trip.Passenger.Code,
                    Target    = clientMethod,
                    Arguments = new object[] { trip }
                });
            }
            catch (Exception e)
            {
                var error = $"ProcessTripExternalizations2SignalR failed: {e.Message}";
                log.LogError(error);
                throw e;
            }
        }
Esempio n. 27
0
        public static async Task <JobHostContext> CreateAndLogHostStartedAsync(
            JobHost host,
            IStorageAccountProvider storageAccountProvider,
            IQueueConfiguration queueConfiguration,
            ITypeLocator typeLocator,
            IJobActivator activator,
            INameResolver nameResolver,
            IConsoleProvider consoleProvider,
            JobHostConfiguration config,
            CancellationToken shutdownToken,
            CancellationToken cancellationToken,
            IWebJobsExceptionHandler exceptionHandler,
            IHostIdProvider hostIdProvider                                 = null,
            FunctionExecutor functionExecutor                              = null,
            IFunctionIndexProvider functionIndexProvider                   = null,
            IBindingProvider bindingProvider                               = null,
            IHostInstanceLoggerProvider hostInstanceLoggerProvider         = null,
            IFunctionInstanceLoggerProvider functionInstanceLoggerProvider = null,
            IFunctionOutputLoggerProvider functionOutputLoggerProvider     = null,
            SingletonManager singletonManager                              = null,
            IAsyncCollector <FunctionInstanceLogEntry> fastLogger          = null)
        {
            if (hostIdProvider == null)
            {
                hostIdProvider = new DynamicHostIdProvider(storageAccountProvider, () => functionIndexProvider);
            }

            IExtensionTypeLocator extensionTypeLocator = new ExtensionTypeLocator(typeLocator);

            ContextAccessor <IMessageEnqueuedWatcher> messageEnqueuedWatcherAccessor = new ContextAccessor <IMessageEnqueuedWatcher>();
            ContextAccessor <IBlobWrittenWatcher>     blobWrittenWatcherAccessor     = new ContextAccessor <IBlobWrittenWatcher>();
            ISharedContextProvider sharedContextProvider = new SharedContextProvider();

            // Create a wrapper TraceWriter that delegates to both the user
            // TraceWriters specified via config (if present), as well as to Console
            TraceWriter trace = new ConsoleTraceWriter(config.Tracing, consoleProvider.Out);

            // Register system services with the service container
            config.AddService <INameResolver>(nameResolver);

            if (exceptionHandler != null)
            {
                exceptionHandler.Initialize(host);
            }

            ExtensionConfigContext context = new ExtensionConfigContext
            {
                Config = config,
                Trace  = trace,
                Host   = host
            };

            InvokeExtensionConfigProviders(context);

            if (singletonManager == null)
            {
                singletonManager = new SingletonManager(storageAccountProvider, exceptionHandler, config.Singleton, trace, hostIdProvider, config.NameResolver);
            }

            IExtensionRegistry      extensions             = config.GetExtensions();
            ITriggerBindingProvider triggerBindingProvider = DefaultTriggerBindingProvider.Create(nameResolver,
                                                                                                  storageAccountProvider, extensionTypeLocator, hostIdProvider, queueConfiguration, exceptionHandler,
                                                                                                  messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, sharedContextProvider, extensions, singletonManager, trace);

            if (bindingProvider == null)
            {
                bindingProvider = DefaultBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, extensions);
            }

            bool hasFastTableHook   = config.GetService <IAsyncCollector <FunctionInstanceLogEntry> >() != null;
            bool noDashboardStorage = config.DashboardConnectionString == null;

            if (hasFastTableHook && noDashboardStorage)
            {
                var loggerProvider = new FastTableLoggerProvider(trace);
                hostInstanceLoggerProvider     = loggerProvider;
                functionInstanceLoggerProvider = loggerProvider;
                functionOutputLoggerProvider   = loggerProvider;
            }
            else
            {
                var loggerProvider = new DefaultLoggerProvider(storageAccountProvider, trace);
                hostInstanceLoggerProvider     = loggerProvider;
                functionInstanceLoggerProvider = loggerProvider;
                functionOutputLoggerProvider   = loggerProvider;
            }

            using (CancellationTokenSource combinedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, shutdownToken))
            {
                CancellationToken combinedCancellationToken = combinedCancellationSource.Token;

                await WriteSiteExtensionManifestAsync(combinedCancellationToken);

                IStorageAccount dashboardAccount = await storageAccountProvider.GetDashboardAccountAsync(combinedCancellationToken);

                IHostInstanceLogger hostInstanceLogger = await hostInstanceLoggerProvider.GetAsync(combinedCancellationToken);

                IFunctionInstanceLogger functionInstanceLogger = await functionInstanceLoggerProvider.GetAsync(combinedCancellationToken);

                IFunctionOutputLogger functionOutputLogger = await functionOutputLoggerProvider.GetAsync(combinedCancellationToken);

                if (functionExecutor == null)
                {
                    functionExecutor = new FunctionExecutor(functionInstanceLogger, functionOutputLogger, exceptionHandler, trace, fastLogger);
                }

                if (functionIndexProvider == null)
                {
                    functionIndexProvider = new FunctionIndexProvider(typeLocator, triggerBindingProvider, bindingProvider, activator, functionExecutor, extensions, singletonManager, trace);
                }

                IFunctionIndex functions = await functionIndexProvider.GetAsync(combinedCancellationToken);

                IListenerFactory functionsListenerFactory = new HostListenerFactory(functions.ReadAll(), singletonManager, activator, nameResolver, trace);

                IFunctionExecutor hostCallExecutor;
                IListener         listener;
                HostOutputMessage hostOutputMessage;

                string hostId = await hostIdProvider.GetHostIdAsync(cancellationToken);

                if (string.Compare(config.HostId, hostId, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    // if this isn't a static host ID, provide the HostId on the config
                    // so it is accessible
                    config.HostId = hostId;
                }

                if (dashboardAccount != null)
                {
                    string sharedQueueName = HostQueueNames.GetHostQueueName(hostId);
                    IStorageQueueClient dashboardQueueClient       = dashboardAccount.CreateQueueClient();
                    IStorageQueue       sharedQueue                = dashboardQueueClient.GetQueueReference(sharedQueueName);
                    IListenerFactory    sharedQueueListenerFactory = new HostMessageListenerFactory(sharedQueue,
                                                                                                    queueConfiguration, exceptionHandler, trace, functions,
                                                                                                    functionInstanceLogger, functionExecutor);

                    Guid             hostInstanceId               = Guid.NewGuid();
                    string           instanceQueueName            = HostQueueNames.GetHostQueueName(hostInstanceId.ToString("N"));
                    IStorageQueue    instanceQueue                = dashboardQueueClient.GetQueueReference(instanceQueueName);
                    IListenerFactory instanceQueueListenerFactory = new HostMessageListenerFactory(instanceQueue,
                                                                                                   queueConfiguration, exceptionHandler, trace, functions,
                                                                                                   functionInstanceLogger, functionExecutor);

                    HeartbeatDescriptor heartbeatDescriptor = new HeartbeatDescriptor
                    {
                        SharedContainerName = HostContainerNames.Hosts,
                        SharedDirectoryName = HostDirectoryNames.Heartbeats + "/" + hostId,
                        InstanceBlobName    = hostInstanceId.ToString("N"),
                        ExpirationInSeconds = (int)HeartbeatIntervals.ExpirationInterval.TotalSeconds
                    };

                    IStorageBlockBlob blob = dashboardAccount.CreateBlobClient()
                                             .GetContainerReference(heartbeatDescriptor.SharedContainerName)
                                             .GetBlockBlobReference(heartbeatDescriptor.SharedDirectoryName + "/" + heartbeatDescriptor.InstanceBlobName);
                    IRecurrentCommand heartbeatCommand = new UpdateHostHeartbeatCommand(new HeartbeatCommand(blob));

                    IEnumerable <MethodInfo> indexedMethods = functions.ReadAllMethods();
                    Assembly hostAssembly = GetHostAssembly(indexedMethods);
                    string   displayName  = hostAssembly != null?hostAssembly.GetName().Name : "Unknown";

                    hostOutputMessage = new DataOnlyHostOutputMessage
                    {
                        HostInstanceId      = hostInstanceId,
                        HostDisplayName     = displayName,
                        SharedQueueName     = sharedQueueName,
                        InstanceQueueName   = instanceQueueName,
                        Heartbeat           = heartbeatDescriptor,
                        WebJobRunIdentifier = WebJobRunIdentifier.Current
                    };

                    hostCallExecutor = CreateHostCallExecutor(instanceQueueListenerFactory, heartbeatCommand,
                                                              exceptionHandler, shutdownToken, functionExecutor);
                    IListenerFactory hostListenerFactory = new CompositeListenerFactory(functionsListenerFactory,
                                                                                        sharedQueueListenerFactory, instanceQueueListenerFactory);
                    listener = CreateHostListener(hostListenerFactory, heartbeatCommand, exceptionHandler, shutdownToken);

                    // Publish this to Azure logging account so that a web dashboard can see it.
                    await LogHostStartedAsync(functions, hostOutputMessage, hostInstanceLogger, combinedCancellationToken);
                }
                else
                {
                    hostCallExecutor = new ShutdownFunctionExecutor(shutdownToken, functionExecutor);

                    IListener factoryListener  = new ListenerFactoryListener(functionsListenerFactory);
                    IListener shutdownListener = new ShutdownListener(shutdownToken, factoryListener);
                    listener = shutdownListener;

                    hostOutputMessage = new DataOnlyHostOutputMessage();
                }

                functionExecutor.HostOutputMessage = hostOutputMessage;

                IEnumerable <FunctionDescriptor> descriptors = functions.ReadAllDescriptors();
                int descriptorsCount = descriptors.Count();

                if (config.UsingDevelopmentSettings)
                {
                    trace.Verbose(string.Format("Development settings applied"));
                }

                if (descriptorsCount == 0)
                {
                    trace.Warning(string.Format("No job functions found. Try making your job classes and methods public. {0}",
                                                Constants.ExtensionInitializationMessage), TraceSource.Indexing);
                }
                else
                {
                    StringBuilder functionsTrace = new StringBuilder();
                    functionsTrace.AppendLine("Found the following functions:");

                    foreach (FunctionDescriptor descriptor in descriptors)
                    {
                        functionsTrace.AppendLine(descriptor.FullName);
                    }

                    trace.Info(functionsTrace.ToString(), TraceSource.Indexing);
                }

                return(new JobHostContext(functions, hostCallExecutor, listener, trace, fastLogger));
            }
        }
Esempio n. 28
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            [CosmosDB(
                 databaseName: "<database name>",
                 collectionName: "<collection name>",
                 ConnectionStringSetting = "CosmosDBConnection")] IAsyncCollector <GeneratedResult> cosmosItem,
            ILogger log)
        {
            log.LogInformation("Received request to generate short URL");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    input       = JsonConvert.DeserializeObject <urlBody>(requestBody);

            //if no customize alias
            if (string.IsNullOrEmpty(input.alias))
            {
                bool            Exist    = true;
                GeneratedResult response = new GeneratedResult();
                //check for duplication for random generated alias
                while (Exist)
                {
                    string shorten = RandomGenerator();
                    response = new GeneratedResult
                    {
                        url           = input.url,
                        shorturl      = shorten,
                        dateGenerated = DateTime.UtcNow.ToShortDateString()
                    };
                    Exist = await ShortenExistAsync(shorten);
                }

                //create item in cosmos DB
                await cosmosItem.AddAsync(response);

                //return json object
                return(new OkObjectResult(JsonConvert.SerializeObject(response)));
            }
            else
            {
                //check if custom alias existed
                bool Exist = await ShortenExistAsync(input.alias);

                if (Exist)
                {
                    //if existed, simply response "existed"
                    return(new OkObjectResult("existed"));
                }
                else
                {
                    //otherwise create a new item in cosmos DB with custom alias
                    GeneratedResult response = new GeneratedResult()
                    {
                        url           = input.url,
                        shorturl      = input.alias,
                        dateGenerated = DateTime.UtcNow.ToShortDateString()
                    };

                    await cosmosItem.AddAsync(response);

                    return(new OkObjectResult("created"));
                }
            }
        }
 public static void BindToAsyncCollectorOfJObject(
     [ApiHubTable("AzureWebJobsSql", TableName = "SampleTable")]
     IAsyncCollector <JObject> collector)
 {
     // Add entities to the collector.
 }
Esempio n. 30
0
            public async Task Func([SignalR(HubName = DefaultHubName)] IAsyncCollector <SignalRMessage> signalRMessages)
            {
                await SimulateSendingMessage(signalRMessages);

                Assert.NotNull(((ServiceManagerStore)StaticServiceHubContextStore.ServiceManagerStore).GetByConfigurationKey(Constants.AzureSignalRConnectionStringName));
            }
Esempio n. 31
0
        public static async Task<JobHostContext> CreateAndLogHostStartedAsync(
            JobHost host,
            IStorageAccountProvider storageAccountProvider,
            IQueueConfiguration queueConfiguration,
            ITypeLocator typeLocator,
            IJobActivator activator,
            INameResolver nameResolver,
            IConsoleProvider consoleProvider,
            JobHostConfiguration config,
            CancellationToken shutdownToken,
            CancellationToken cancellationToken,
            IHostIdProvider hostIdProvider = null,
            FunctionExecutor functionExecutor = null,
            IFunctionIndexProvider functionIndexProvider = null,
            IBindingProvider bindingProvider = null,
            IHostInstanceLoggerProvider hostInstanceLoggerProvider = null,
            IFunctionInstanceLoggerProvider functionInstanceLoggerProvider = null,
            IFunctionOutputLoggerProvider functionOutputLoggerProvider = null,
            IBackgroundExceptionDispatcher backgroundExceptionDispatcher = null,
            SingletonManager singletonManager = null,
            IAsyncCollector<FunctionInstanceLogEntry> fastLogger = null)
        {
            if (hostIdProvider == null)
            {
                hostIdProvider = new DynamicHostIdProvider(storageAccountProvider, () => functionIndexProvider);
            }

            IExtensionTypeLocator extensionTypeLocator = new ExtensionTypeLocator(typeLocator);
            if (backgroundExceptionDispatcher == null)
            {
                backgroundExceptionDispatcher = BackgroundExceptionDispatcher.Instance;
            }
            ContextAccessor<IMessageEnqueuedWatcher> messageEnqueuedWatcherAccessor = new ContextAccessor<IMessageEnqueuedWatcher>();
            ContextAccessor<IBlobWrittenWatcher> blobWrittenWatcherAccessor = new ContextAccessor<IBlobWrittenWatcher>();
            ISharedContextProvider sharedContextProvider = new SharedContextProvider();

            // Create a wrapper TraceWriter that delegates to both the user 
            // TraceWriters specified via config (if present), as well as to Console
            TraceWriter trace = new ConsoleTraceWriter(config.Tracing, consoleProvider.Out);

            // Register system services with the service container
            config.AddService<INameResolver>(nameResolver);

            ExtensionConfigContext context = new ExtensionConfigContext
            {
                Config = config,
                Trace = trace,
                Host = host
            };
            InvokeExtensionConfigProviders(context);

            if (singletonManager == null)
            {
                singletonManager = new SingletonManager(storageAccountProvider, backgroundExceptionDispatcher, config.Singleton, trace, hostIdProvider, config.NameResolver);
            }

            IExtensionRegistry extensions = config.GetExtensions();
            ITriggerBindingProvider triggerBindingProvider = DefaultTriggerBindingProvider.Create(nameResolver,
                storageAccountProvider, extensionTypeLocator, hostIdProvider, queueConfiguration, backgroundExceptionDispatcher,
                messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, sharedContextProvider, extensions, singletonManager, trace);

            if (bindingProvider == null)
            {
                bindingProvider = DefaultBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, extensions);
            }
                        
            bool hasFastTableHook = config.GetService<IAsyncCollector<FunctionInstanceLogEntry>>() != null;
            bool noDashboardStorage = config.DashboardConnectionString == null;
            if (hasFastTableHook && noDashboardStorage)
            {
                var loggerProvider = new FastTableLoggerProvider(trace);
                hostInstanceLoggerProvider = loggerProvider;
                functionInstanceLoggerProvider = loggerProvider;
                functionOutputLoggerProvider = loggerProvider;
            }
            else
            {
                var loggerProvider = new DefaultLoggerProvider(storageAccountProvider, trace);
                hostInstanceLoggerProvider = loggerProvider;
                functionInstanceLoggerProvider = loggerProvider;
                functionOutputLoggerProvider = loggerProvider;
            }

            using (CancellationTokenSource combinedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, shutdownToken))
            {
                CancellationToken combinedCancellationToken = combinedCancellationSource.Token;

                await WriteSiteExtensionManifestAsync(combinedCancellationToken);

                IStorageAccount dashboardAccount = await storageAccountProvider.GetDashboardAccountAsync(combinedCancellationToken);

                IHostInstanceLogger hostInstanceLogger = await hostInstanceLoggerProvider.GetAsync(combinedCancellationToken);                
                IFunctionInstanceLogger functionInstanceLogger = await functionInstanceLoggerProvider.GetAsync(combinedCancellationToken);                
                IFunctionOutputLogger functionOutputLogger = await functionOutputLoggerProvider.GetAsync(combinedCancellationToken);
                
                if (functionExecutor == null)
                {
                    functionExecutor = new FunctionExecutor(functionInstanceLogger, functionOutputLogger, backgroundExceptionDispatcher, trace, config.FunctionTimeout, fastLogger);
                }

                if (functionIndexProvider == null)
                {
                    functionIndexProvider = new FunctionIndexProvider(typeLocator, triggerBindingProvider, bindingProvider, activator, functionExecutor, extensions, singletonManager, trace);
                }

                IFunctionIndex functions = await functionIndexProvider.GetAsync(combinedCancellationToken);
                IListenerFactory functionsListenerFactory = new HostListenerFactory(functions.ReadAll(), singletonManager, activator, nameResolver, trace);

                IFunctionExecutor hostCallExecutor;
                IListener listener;
                HostOutputMessage hostOutputMessage;

                string hostId = await hostIdProvider.GetHostIdAsync(cancellationToken);
                if (string.Compare(config.HostId, hostId, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    // if this isn't a static host ID, provide the HostId on the config
                    // so it is accessible
                    config.HostId = hostId;
                }

                if (dashboardAccount != null)
                {
                    string sharedQueueName = HostQueueNames.GetHostQueueName(hostId);
                    IStorageQueueClient dashboardQueueClient = dashboardAccount.CreateQueueClient();
                    IStorageQueue sharedQueue = dashboardQueueClient.GetQueueReference(sharedQueueName);
                    IListenerFactory sharedQueueListenerFactory = new HostMessageListenerFactory(sharedQueue,
                        queueConfiguration, backgroundExceptionDispatcher, trace, functions,
                        functionInstanceLogger, functionExecutor);

                    Guid hostInstanceId = Guid.NewGuid();
                    string instanceQueueName = HostQueueNames.GetHostQueueName(hostInstanceId.ToString("N"));
                    IStorageQueue instanceQueue = dashboardQueueClient.GetQueueReference(instanceQueueName);
                    IListenerFactory instanceQueueListenerFactory = new HostMessageListenerFactory(instanceQueue,
                        queueConfiguration, backgroundExceptionDispatcher, trace, functions,
                        functionInstanceLogger, functionExecutor);

                    HeartbeatDescriptor heartbeatDescriptor = new HeartbeatDescriptor
                    {
                        SharedContainerName = HostContainerNames.Hosts,
                        SharedDirectoryName = HostDirectoryNames.Heartbeats + "/" + hostId,
                        InstanceBlobName = hostInstanceId.ToString("N"),
                        ExpirationInSeconds = (int)HeartbeatIntervals.ExpirationInterval.TotalSeconds
                    };

                    IStorageBlockBlob blob = dashboardAccount.CreateBlobClient()
                        .GetContainerReference(heartbeatDescriptor.SharedContainerName)
                        .GetBlockBlobReference(heartbeatDescriptor.SharedDirectoryName + "/" + heartbeatDescriptor.InstanceBlobName);
                    IRecurrentCommand heartbeatCommand = new UpdateHostHeartbeatCommand(new HeartbeatCommand(blob));

                    IEnumerable<MethodInfo> indexedMethods = functions.ReadAllMethods();
                    Assembly hostAssembly = GetHostAssembly(indexedMethods);
                    string displayName = hostAssembly != null ? hostAssembly.GetName().Name : "Unknown";

                    hostOutputMessage = new DataOnlyHostOutputMessage
                    {
                        HostInstanceId = hostInstanceId,
                        HostDisplayName = displayName,
                        SharedQueueName = sharedQueueName,
                        InstanceQueueName = instanceQueueName,
                        Heartbeat = heartbeatDescriptor,
                        WebJobRunIdentifier = WebJobRunIdentifier.Current
                    };

                    hostCallExecutor = CreateHostCallExecutor(instanceQueueListenerFactory, heartbeatCommand,
                        backgroundExceptionDispatcher, shutdownToken, functionExecutor);
                    IListenerFactory hostListenerFactory = new CompositeListenerFactory(functionsListenerFactory,
                        sharedQueueListenerFactory, instanceQueueListenerFactory);
                    listener = CreateHostListener(hostListenerFactory, heartbeatCommand, backgroundExceptionDispatcher, shutdownToken);

                    // Publish this to Azure logging account so that a web dashboard can see it. 
                    await LogHostStartedAsync(functions, hostOutputMessage, hostInstanceLogger, combinedCancellationToken);
                }
                else
                {
                    hostCallExecutor = new ShutdownFunctionExecutor(shutdownToken, functionExecutor);

                    IListener factoryListener = new ListenerFactoryListener(functionsListenerFactory);
                    IListener shutdownListener = new ShutdownListener(shutdownToken, factoryListener);
                    listener = shutdownListener;

                    hostOutputMessage = new DataOnlyHostOutputMessage();
                }

                functionExecutor.HostOutputMessage = hostOutputMessage;

                IEnumerable<FunctionDescriptor> descriptors = functions.ReadAllDescriptors();
                int descriptorsCount = descriptors.Count();

                if (config.UsingDevelopmentSettings)
                {
                    trace.Verbose(string.Format("Development settings applied"));
                }

                if (descriptorsCount == 0)
                {
                    trace.Warning(string.Format("No job functions found. Try making your job classes and methods public. {0}", 
                        Constants.ExtensionInitializationMessage), TraceSource.Indexing);
                }
                else
                {
                    StringBuilder functionsTrace = new StringBuilder();
                    functionsTrace.AppendLine("Found the following functions:");
                    
                    foreach (FunctionDescriptor descriptor in descriptors)
                    {
                        functionsTrace.AppendLine(descriptor.FullName);
                    }

                    trace.Info(functionsTrace.ToString(), TraceSource.Indexing);
                }

                return new JobHostContext(functions, hostCallExecutor, listener, trace, fastLogger);
            }
        }
Esempio n. 32
0
        public static async Task SendMessage([HttpTrigger(AuthorizationLevel.Anonymous, "post")] object location, [SignalR(HubName = "location")] IAsyncCollector <SignalRMessage> signalRMessages)
        {
            try
            {
                var message = new SignalRMessage
                {
                    Target    = "newLocation",
                    Arguments = new[] { location }
                };


                await signalRMessages.AddAsync(message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error {ex}\n{ex.StackTrace}");
                Debug.WriteLine($"Error {ex}\n{ex.StackTrace}");
            }
        }