Пример #1
0
        private async Task HandleGetInstalledPrintersAsync(RequestInstalledPrintersRefreshRequest request)
        {
            var db           = _redis.GetDatabase();
            var connectionId = await db.HashGetAsync(RedisConstants.ConnectedClients, request.SpoolerId);

            if (connectionId.HasValue)
            {
                var spoolerClient = _printingHubContext.Clients.Client(connectionId);
                await spoolerClient.RequestInstalledPrinters();
            }
        }
Пример #2
0
            public override async Task <ActionResult <Spooler> > HandleAsync(Query request, CancellationToken cancellationToken)
            {
                var projectId = User.GetProjectId();

                var query   = _db.Spoolers.Where(x => x.Zone.ProjectId == projectId && x.Id == request.Id);
                var spooler = await _mapper.ProjectTo <Spooler>(query).SingleOrDefaultAsync();

                if (spooler is null)
                {
                    return(NotFound());
                }

                var db = _redis.GetDatabase();

                if (Request.GetTypedHeaders().CacheControl?.NoCache == true)
                {
                    // Force update of installed printers
                    var subscriber = _redis.GetSubscriber();

                    var refreshedSubscription = await subscriber.SubscribeAsync(RedisConstants.Channels.InstalledPrintersRefreshed(spooler.Id));

                    var queue        = RedisConstants.Queues.RequestInstalledPrintersRefreshQueue(spooler.Id);
                    var queueRequest = new RequestInstalledPrintersRefreshRequest()
                    {
                        SpoolerId = spooler.Id
                    };
                    await db.ListRightPushAsync(queue, JsonSerializer.Serialize(queueRequest));

                    await subscriber.PublishAsync(RedisConstants.Channels.JobCreated, queue);

                    try
                    {
                        using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
                        cts.CancelAfter(5_000);

                        await refreshedSubscription.ReadAsync(cts.Token);
                    }
                    catch (OperationCanceledException)
                    {
                    }
                    finally
                    {
                        await refreshedSubscription.UnsubscribeAsync();
                    }
                }

                await QueryHelpers.GetPrintersAsync(spooler, db);

                return(spooler);
            }