Skip to content

Commit

Permalink
Add remainder of job preservation changes with test suite changes (Is…
Browse files Browse the repository at this point in the history
…sue #189)
  • Loading branch information
michaelrsweet committed May 30, 2022
1 parent 1ed5b33 commit 16abdd7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 77 deletions.
3 changes: 3 additions & 0 deletions pappl/job-process.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,9 @@ finish_job(pappl_job_t *job) // I - Job

_papplSystemAddEventNoLock(printer->system, printer, NULL, PAPPL_EVENT_PRINTER_STATE_CHANGED, NULL);

if (printer->max_preserved_jobs > 0)
_papplPrinterCleanJobsNoLock(printer);

pthread_rwlock_unlock(&printer->rwlock);

_papplSystemConfigChanged(printer->system);
Expand Down
130 changes: 66 additions & 64 deletions pappl/job.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,71 @@ _papplPrinterCheckJobs(
}


//
// '_papplPrinterCleanJobsNoLock()' - Clean completed jobs for a printer.
//

void
_papplPrinterCleanJobsNoLock(
pappl_printer_t *printer) // I - Printer
{
time_t cleantime; // Clean time
pappl_job_t *job; // Current job
int preserved; // Number of preserved jobs


if (cupsArrayGetCount(printer->completed_jobs) == 0 || (printer->max_preserved_jobs == 0 && printer->max_completed_jobs <= 0))
return;

// Enumerate the jobs. Since we have a writer (exclusive) lock, we are the
// only thread enumerating and can use cupsArrayGetFirst/Last...
for (job = (pappl_job_t *)cupsArrayGetFirst(printer->completed_jobs), cleantime = time(NULL) - 60, preserved = 0; job; job = (pappl_job_t *)cupsArrayGetNext(printer->completed_jobs))
{
if (job->completed && job->completed < cleantime && printer->max_completed_jobs > 0 && (int)cupsArrayGetCount(printer->completed_jobs) > printer->max_completed_jobs)
{
cupsArrayRemove(printer->completed_jobs, job);
cupsArrayRemove(printer->all_jobs, job);
}
else if (printer->max_preserved_jobs > 0)
{
if (job->filename)
{
preserved ++;
if (preserved > printer->max_preserved_jobs)
_papplJobRemoveFile(job);
}
}
else
break;
}
}


//
// 'papplPrinterFindJob()' - Find a job.
//
// This function finds a job submitted to a printer using its integer ID value.
//

pappl_job_t * // O - Job or `NULL` if not found
papplPrinterFindJob(
pappl_printer_t *printer, // I - Printer
int job_id) // I - Job ID
{
pappl_job_t key, // Job search key
*job; // Matching job, if any


key.job_id = job_id;

pthread_rwlock_rdlock(&(printer->rwlock));
job = (pappl_job_t *)cupsArrayFind(printer->all_jobs, &key);
pthread_rwlock_unlock(&(printer->rwlock));

return (job);
}


//
// '_papplScannerCheckJobs()' - Check for new jobs to process.
//
Expand Down Expand Up @@ -614,31 +679,6 @@ _papplScannerCheckJobs(
}


//
// 'papplPrinterFindJob()' - Find a job.
//
// This function finds a job submitted to a printer using its integer ID value.
//

pappl_job_t * // O - Job or `NULL` if not found
papplPrinterFindJob(
pappl_printer_t *printer, // I - Printer
int job_id) // I - Job ID
{
pappl_job_t key, // Job search key
*job; // Matching job, if any


key.job_id = job_id;

pthread_rwlock_rdlock(&(printer->rwlock));
job = (pappl_job_t *)cupsArrayFind(printer->all_jobs, &key);
pthread_rwlock_unlock(&(printer->rwlock));

return (job);
}


//
// 'papplSystemCleanJobs()' - Clean out old (completed) jobs.
//
Expand All @@ -657,11 +697,7 @@ papplSystemCleanJobs(
size_t i, // Looping var
count; // Number of printers
pappl_printer_t *printer; // Current printer
pappl_job_t *job; // Current job
time_t cleantime; // Clean time


cleantime = time(NULL) - 60;

pthread_rwlock_rdlock(&system->rwlock);

Expand All @@ -674,42 +710,8 @@ papplSystemCleanJobs(
{
printer = (pappl_printer_t *)cupsArrayGetElement(system->printers, i);

if (cupsArrayGetCount(printer->completed_jobs) == 0 || (printer->max_preserved_jobs == 0 && printer->max_completed_jobs <= 0))
continue;

pthread_rwlock_wrlock(&printer->rwlock);

if (printer->max_completed_jobs > 0)
{
// Enumerate the jobs. Since we have a writer (exclusive) lock, we are the
// only thread enumerating and can use cupsArrayGetFirst/Last...
for (job = (pappl_job_t *)cupsArrayGetFirst(printer->completed_jobs); job; job = (pappl_job_t *)cupsArrayGetNext(printer->completed_jobs))
{
if (job->completed && job->completed < cleantime && (int)cupsArrayGetCount(printer->completed_jobs) > printer->max_completed_jobs)
{
cupsArrayRemove(printer->completed_jobs, job);
cupsArrayRemove(printer->all_jobs, job);
}
else
break;
}
}

if (printer->max_preserved_jobs > 0)
{
int preserved = 0; // Number of preserved jobs

for (job = (pappl_job_t *)cupsArrayGetLast(printer->completed_jobs); job; job = (pappl_job_t *)cupsArrayGetPrev(printer->completed_jobs))
{
if (job->filename)
{
preserved ++;
if (preserved > printer->max_preserved_jobs)
_papplJobRemoveFile(job);
}
}
}

_papplPrinterCleanJobsNoLock(printer);
pthread_rwlock_unlock(&printer->rwlock);
}

Expand Down
2 changes: 1 addition & 1 deletion pappl/printer-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ extern void *_papplPrinterRunRaw(pappl_printer_t *printer) _PAPPL_PRIVATE;
extern void *_papplPrinterRunUSB(pappl_printer_t *printer) _PAPPL_PRIVATE;

extern void _papplPrinterCheckJobs(pappl_printer_t *printer) _PAPPL_PRIVATE;
extern void _papplPrinterCleanJobs(pappl_printer_t *printer) _PAPPL_PRIVATE;
extern void _papplPrinterCleanJobsNoLock(pappl_printer_t *printer) _PAPPL_PRIVATE;
extern void _papplPrinterCopyAttributes(pappl_printer_t *printer, pappl_client_t *client, cups_array_t *ra, const char *format) _PAPPL_PRIVATE;
extern void _papplPrinterCopyState(pappl_printer_t *printer, ipp_tag_t group_tag, ipp_t *ipp, pappl_client_t *client, cups_array_t *ra) _PAPPL_PRIVATE;
extern void _papplPrinterCopyXRI(pappl_printer_t *printer, ipp_t *ipp, pappl_client_t *client) _PAPPL_PRIVATE;
Expand Down
41 changes: 29 additions & 12 deletions pappl/printer-webif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1391,21 +1391,38 @@ _papplPrinterWebReprintJob(
username = "guest";

// Copy the job...
if ((new_job = _papplJobCreate(printer, 0, username, job->format, job->name, job->attrs)) == NULL)
if ((new_job = _papplJobCreate(printer, 0, username, job->format, job->name, job->attrs)) != NULL)
{
status = _PAPPL_LOC("Unable to copy print job.");
}
else
{
// TODO: Copy job->filename
// Submit the job for processing...
_papplJobSubmitFile(new_job, job->filename);
// Copy the job file...
int oldfd, // Old job file
newfd; // New job file
char buffer[8192]; // Copy buffer
ssize_t bytes; // Bytes read...

if ((oldfd = open(job->filename, O_RDONLY | O_BINARY)) >= 0)
{
if ((newfd = papplJobOpenFile(new_job, path, sizeof(path), printer->system->directory, NULL, "w")) >= 0)
{
while ((bytes = read(oldfd, buffer, sizeof(buffer))) > 0)
write(newfd, buffer, (size_t)bytes);

snprintf(path, sizeof(path), "%s/jobs", printer->uriname);
papplClientRespondRedirect(client, HTTP_STATUS_FOUND, path);
cupsFreeOptions(num_form, form);
return;
close(oldfd);
close(newfd);

// Submit the job for processing...
_papplJobSubmitFile(new_job, path);

snprintf(path, sizeof(path), "%s/jobs", printer->uriname);
papplClientRespondRedirect(client, HTTP_STATUS_FOUND, path);
cupsFreeOptions(num_form, form);
return;
}

close(oldfd);
}
}

status = _PAPPL_LOC("Unable to copy print job.");
}
else
{
Expand Down
1 change: 1 addition & 0 deletions testsuite/testpappl.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ main(int argc, // I - Number of command-line arguments
papplPrinterSetGeoLocation(printer, "geo:46.4707,-80.9961");
papplPrinterSetLocation(printer, "Test Lab 42");
papplPrinterSetOrganization(printer, "Lakeside Robotics");
papplPrinterSetMaxPreservedJobs(printer, 3);

if (soptions & PAPPL_SOPTIONS_MULTI_QUEUE)
{
Expand Down

0 comments on commit 16abdd7

Please sign in to comment.