func NewPrinterManager(cups *cups.CUPS, gcp *gcp.GoogleCloudPrint, xmpp *xmpp.XMPP, privet *privet.Privet, snmp *snmp.SNMPManager, printerPollInterval string, gcpMaxConcurrentDownload, cupsQueueSize uint, jobFullUsername, ignoreRawPrinters bool, shareScope string) (*PrinterManager, error) { // Get the GCP printer list. gcpPrinters, queuedJobsCount, err := allGCPPrinters(gcp) if err != nil { return nil, err } // Organize the GCP printers into a map. for i := range gcpPrinters { gcpPrinters[i].CUPSJobSemaphore = lib.NewSemaphore(cupsQueueSize) } gcpPrintersByGCPID := lib.NewConcurrentPrinterMap(gcpPrinters) // Construct. pm := PrinterManager{ cups: cups, gcp: gcp, xmpp: xmpp, privet: privet, snmp: snmp, gcpPrintersByGCPID: gcpPrintersByGCPID, downloadSemaphore: lib.NewSemaphore(gcpMaxConcurrentDownload), jobStatsMutex: sync.Mutex{}, jobsDone: 0, jobsError: 0, gcpJobsInFlightMutex: sync.Mutex{}, gcpJobsInFlight: make(map[string]struct{}), cupsQueueSize: cupsQueueSize, jobFullUsername: jobFullUsername, ignoreRawPrinters: ignoreRawPrinters, shareScope: shareScope, quit: make(chan struct{}), } // Sync once before returning, to make sure things are working. // Ignore privet updates this first time because Privet always starts // with zero printers. if err = pm.syncPrinters(true); err != nil { return nil, err } // Initialize Privet printers. if privet != nil { for _, printer := range pm.gcpPrintersByGCPID.GetAll() { getPrinter := func() (lib.Printer, bool) { return pm.gcpPrintersByGCPID.Get(printer.GCPID) } err := privet.AddPrinter(printer, getPrinter) if err != nil { glog.Warningf("Failed to register %s locally: %s", printer.Name, err) } else { glog.Infof("Registered %s locally", printer.Name) } } } ppi, err := time.ParseDuration(printerPollInterval) if err != nil { return nil, err } pm.syncPrintersPeriodically(ppi) if privet == nil { pm.listenNotifications(xmpp.Notifications(), make(chan *lib.Job)) } else { pm.listenNotifications(xmpp.Notifications(), privet.Jobs()) } for gcpID := range queuedJobsCount { go pm.handleNewGCPJobs(gcpID) } return &pm, nil }