Search in sources :

Example 1 with ServiceInstanceEntity

use of com.peterphi.servicemanager.service.db.entity.ServiceInstanceEntity in project stdlib by petergeneric.

the class ServiceManagerLoggingRestServiceImpl method report.

@Override
public void report(final LogReport logs) {
    if (log.isTraceEnabled())
        log.trace("Received " + logs.getLines().length + " log lines to store");
    try {
        final ServiceInstanceEntity serviceInstance = cache.get(logs.getServiceId());
        // Make sure that all store calls are for the same partition (date + instance id)
        // This is technically only a requirement for Azure but it's a guarantee other
        // stores can use effectively (e.g. writing to datestamped log files)
        String partitionKey = null;
        List<LogLineTableEntity> pending = new ArrayList<>();
        for (LogLine line : logs.getLines()) {
            LogLineTableEntity entity = convert(serviceInstance, line);
            if (partitionKey == null) {
                // First entry in a new partition
                partitionKey = entity.getPartitionKey();
            } else if (!partitionKey.equals(entity.getPartitionKey())) {
                // Flush all the lines up til now and then start a new list
                service.store(pending);
                pending = new ArrayList<>();
                partitionKey = entity.getPartitionKey();
            }
            pending.add(entity);
        }
        // Make sure we flush any remaining data to the storage system
        service.store(pending);
    } catch (Throwable t) {
        log.error("Error saving logs", t);
        throw t;
    }
}
Also used : LogLineTableEntity(com.peterphi.servicemanager.service.logging.LogLineTableEntity) ArrayList(java.util.ArrayList) ServiceInstanceEntity(com.peterphi.servicemanager.service.db.entity.ServiceInstanceEntity) LogLine(com.peterphi.std.guice.common.logging.logreport.LogLine)

Example 2 with ServiceInstanceEntity

use of com.peterphi.servicemanager.service.db.entity.ServiceInstanceEntity in project stdlib by petergeneric.

the class ServiceManagerRegistryRestServiceImpl method register.

@Override
@Transactional
public void register(final String instanceId, final String endpoint, final String managementToken, final String codeRevision) {
    ServiceInstanceEntity entity = dao.getById(instanceId);
    final boolean update = (entity != null);
    // Update an existing entity
    if (entity == null) {
        entity = new ServiceInstanceEntity();
        entity.setId(instanceId);
    }
    entity.setEndpoint(endpoint);
    entity.setManagementToken(managementToken);
    entity.setCodeRevision(codeRevision);
    // Proactively update the cache used by the logging service so that it won't have to make a db query
    cache.put(entity);
    if (update)
        dao.update(entity);
    else
        dao.save(entity);
}
Also used : ServiceInstanceEntity(com.peterphi.servicemanager.service.db.entity.ServiceInstanceEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Aggregations

ServiceInstanceEntity (com.peterphi.servicemanager.service.db.entity.ServiceInstanceEntity)2 LogLineTableEntity (com.peterphi.servicemanager.service.logging.LogLineTableEntity)1 LogLine (com.peterphi.std.guice.common.logging.logreport.LogLine)1 Transactional (com.peterphi.std.guice.database.annotation.Transactional)1 ArrayList (java.util.ArrayList)1