コード例 #1
0
        // LOG ALL ERRORS INTO GLOBAL ERROR HANDLER
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                cacheCancellationToken = new CancellationToken();
                // Log
                // Http Client // Do a manual call if this fails
            }

            CancellationToken newToken;

            while (!cancellationToken.IsCancellationRequested)
            {
                // Put on a seperate thread
                // Read Database
                if (_init_called == false)
                {
                    _            = initialise_cache();
                    _init_called = !_init_called;
                }

                Time time = new Time();

                // Time Check
                if (time.ReturnTime().Hours >= 17 && time.ReturnTime().Hours <= 8)
                {
                    if (_cacheFull)
                    {
                        if (_sessionEnded == false) // && getState sessionEnded=true
                        {
                            // Save last data to database
                            for (int key = 0; key < MAX; key++)
                            {
                                String JSON = Cache.Get(key).Serialize();
                                _ = saveEODdata(JSON);
                            }

                            // Stop the stream
                            _ = serviceWorker.StopAsync(cancellationToken);

                            newToken          = new CancellationToken();
                            cancellationToken = newToken;

                            Console.WriteLine("STOP STREAM  ");

                            _sessionEnded = true;
                            _init_work    = false;
                        }
                    }
                }

                if (_sessionEnded)
                {
                    _sessionEnded = false;
                }

                if (_init_work == false)
                {
                    // Start the Service Worker
                    await _stockHandler.Clients.All.lockStream(serviceWorker.API_REQUESTS, false);

                    _ = serviceWorker.StartAsync(cancellationToken);
                    Console.WriteLine("START STREAM");
                    _init_work = !_init_work;
                }

                if (_cacheFull)
                {
                    // Data is kept up to date

                    /*   for (int key = 0; key < MAX; key++)
                     * {
                     *     String JSON = Cache.Get(key).Serialize();
                     *     _ = saveEODdata(JSON);
                     * }*/
                    for (int key = 0; key < MAX; key++)
                    {
                        String JSON = Cache.Get(key).Serialize();
                        await _stockHandler.Clients.All.requestData(key, JSON);
                    }
                }
                //  }

                await Task.Delay(1000);
            }
        }