Fossil

Check-in [12c487c4]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Change backoffice-nodelay to default off.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 12c487c46f1f36bbbb52c2abd53724e9dc1d9699ad3faba3ab0c9dc61d21b8e8
User & Date: drh 2018-08-07 23:16:48.591
Context
2018-08-07
23:22
Merge changes from the previous two check-ins. ... (check-in: 30068260 user: mistachkin tags: trunk)
23:16
Change backoffice-nodelay to default off. ... (check-in: 12c487c4 user: drh tags: trunk)
23:08
Improve Win32 error handling in the backoffice module and fix a compiler warning. ... (check-in: 3c084879 user: mistachkin tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/backoffice.c.
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "config.h"
#include "backoffice.h"
#include <time.h>
#if defined(_WIN32)
# include <windows.h>
# include <stdio.h>
# include <process.h>
# if defined(__MINGW32__)
#  include <wchar.h>
# endif
# define GETPID (int)GetCurrentProcessId
#else
# include <unistd.h>
# include <sys/types.h>
# include <signal.h>
# define GETPID getpid
#endif







<
<
<







60
61
62
63
64
65
66



67
68
69
70
71
72
73
#include "config.h"
#include "backoffice.h"
#include <time.h>
#if defined(_WIN32)
# include <windows.h>
# include <stdio.h>
# include <process.h>



# define GETPID (int)GetCurrentProcessId
#else
# include <unistd.h>
# include <sys/types.h>
# include <signal.h>
# define GETPID getpid
#endif
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
*/
static void backofficeSigalrmHandler(int x){
  fossil_panic("backoffice timeout (%d seconds)", x);
}
#if defined(_WIN32)
static void *threadHandle = NULL;
static void __stdcall backofficeWin32NoopApcProc(ULONG_PTR pArg){} /* NO-OP */
static void backofficeWin32ThreadCleanup(int bStrict){
  if( threadHandle!=NULL ){
    /* Queue no-op asynchronous procedure call to the sleeping
     * thread.  This will cause it to wake up with a non-zero
     * return value. */
    if( QueueUserAPC(backofficeWin32NoopApcProc, threadHandle, 0) ){
      /* Wait for the thread to wake up and then exit. */
      WaitForSingleObject(threadHandle, INFINITE);
    }else if(bStrict){
      DWORD dwLastError = GetLastError();
      fossil_errorlog(
        "backofficeWin32ThreadCleanup: QueueUserAPC failed, code %lu",
        dwLastError
      );
      if( !TerminateThread(threadHandle, dwLastError) ){
        dwLastError = GetLastError();
        fossil_panic(
          "backofficeWin32ThreadCleanup: TerminateThread failed, code %lu",
          dwLastError
        );
      }
    }
    CloseHandle(threadHandle);
    threadHandle = NULL;
  }else if(bStrict){
    fossil_panic(
      "backofficeWin32ThreadCleanup: no timeout thread handle"
    );
  }
}
static unsigned __stdcall backofficeWin32SigalrmThreadProc(
  void *pArg /* IN: Pointer to integer number of whole seconds. */
){
  int seconds = FOSSIL_PTR_TO_INT(pArg);
  if( SleepEx((DWORD)seconds * 1000, TRUE)==0 ){
    backofficeSigalrmHandler(seconds);
  }
  _endthreadex(0);
  return 0; /* NOT REACHED */
}
#endif
static void backofficeTimeout(int x){
#if defined(_WIN32)
  backofficeWin32ThreadCleanup(0);
  threadHandle = (void*)_beginthreadex(
    0, 0, backofficeWin32SigalrmThreadProc, FOSSIL_INT_TO_PTR(x), 0, 0
  );
#else
  signal(SIGALRM, backofficeSigalrmHandler);
  alarm(x);
#endif







|







<
<
<
<
<
<
<
<
<
<
<
<
<



<
<
<
<















|







278
279
280
281
282
283
284
285
286
287
288
289
290
291
292













293
294
295




296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
*/
static void backofficeSigalrmHandler(int x){
  fossil_panic("backoffice timeout (%d seconds)", x);
}
#if defined(_WIN32)
static void *threadHandle = NULL;
static void __stdcall backofficeWin32NoopApcProc(ULONG_PTR pArg){} /* NO-OP */
static void backofficeWin32ThreadCleanup(){
  if( threadHandle!=NULL ){
    /* Queue no-op asynchronous procedure call to the sleeping
     * thread.  This will cause it to wake up with a non-zero
     * return value. */
    if( QueueUserAPC(backofficeWin32NoopApcProc, threadHandle, 0) ){
      /* Wait for the thread to wake up and then exit. */
      WaitForSingleObject(threadHandle, INFINITE);













    }
    CloseHandle(threadHandle);
    threadHandle = NULL;




  }
}
static unsigned __stdcall backofficeWin32SigalrmThreadProc(
  void *pArg /* IN: Pointer to integer number of whole seconds. */
){
  int seconds = FOSSIL_PTR_TO_INT(pArg);
  if( SleepEx((DWORD)seconds * 1000, TRUE)==0 ){
    backofficeSigalrmHandler(seconds);
  }
  _endthreadex(0);
  return 0; /* NOT REACHED */
}
#endif
static void backofficeTimeout(int x){
#if defined(_WIN32)
  backofficeWin32ThreadCleanup();
  threadHandle = (void*)_beginthreadex(
    0, 0, backofficeWin32SigalrmThreadProc, FOSSIL_INT_TO_PTR(x), 0, 0
  );
#else
  signal(SIGALRM, backofficeSigalrmHandler);
  alarm(x);
#endif
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
      if( g.fAnyTrace ){
        fprintf(stderr, "/***** Begin Backoffice Processing %d *****/\n",
                        GETPID());
      }
      backoffice_work();
      break;
    }
    if( backofficeNoDelay || db_get_boolean("backoffice-nodelay",1) ){
      /* If the no-delay flag is set, exit immediately rather than queuing
      ** up.  Assume that some future request will come along and handle any
      ** necessary backoffice work. */
      db_end_transaction(0);
      break;
    }
    /* This process needs to queue up and wait for the current lease







|







431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
      if( g.fAnyTrace ){
        fprintf(stderr, "/***** Begin Backoffice Processing %d *****/\n",
                        GETPID());
      }
      backoffice_work();
      break;
    }
    if( backofficeNoDelay || db_get_boolean("backoffice-nodelay",0) ){
      /* If the no-delay flag is set, exit immediately rather than queuing
      ** up.  Assume that some future request will come along and handle any
      ** necessary backoffice work. */
      db_end_transaction(0);
      break;
    }
    /* This process needs to queue up and wait for the current lease
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
        }
        db_end_transaction(0);
        break;
      }
    }
  }
#if defined(_WIN32)
  backofficeWin32ThreadCleanup(1);
#endif
  return;
}

/*
** This routine runs to do the backoffice processing.  When adding new
** backoffice processing tasks, add them here.
*/
void backoffice_work(void){
  /* Log the backoffice run for testing purposes.  For production deployments
  ** the "backoffice-logfile" property should be unset and the following code
  ** should be a no-op. */
  char *zLog = db_get("backoffice-logfile",0);
  if( zLog && zLog[0] ){
    FILE *pLog = fossil_fopen(zLog, "a");
    if( pLog ){
      char *zDate = db_text(0, "SELECT datetime('now');");
      fprintf(pLog, "%s (%d) backoffice running\n", zDate, GETPID());
      fclose(pLog);
    }
  }

  /* Here is where the actual work of the backoffice happens */
  email_backoffice(0);
}







|

















|







475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
        }
        db_end_transaction(0);
        break;
      }
    }
  }
#if defined(_WIN32)
  backofficeWin32ThreadCleanup();
#endif
  return;
}

/*
** This routine runs to do the backoffice processing.  When adding new
** backoffice processing tasks, add them here.
*/
void backoffice_work(void){
  /* Log the backoffice run for testing purposes.  For production deployments
  ** the "backoffice-logfile" property should be unset and the following code
  ** should be a no-op. */
  char *zLog = db_get("backoffice-logfile",0);
  if( zLog && zLog[0] ){
    FILE *pLog = fossil_fopen(zLog, "a");
    if( pLog ){
      char *zDate = db_text(0, "SELECT datetime('now');");
      fprintf(pLog, "%s (%d) backoffice running\n", zDate, getpid());
      fclose(pLog);
    }
  }

  /* Here is where the actual work of the backoffice happens */
  email_backoffice(0);
}
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
    wchar_t *ax[5];
    argv[0] = g.nameOfExe;
    argv[1] = "backoffice";
    argv[2] = "-R";
    argv[3] = backofficeDb;
    ax[4] = 0;
    for(i=0; i<=3; i++) ax[i] = fossil_utf8_to_unicode(argv[i]);
    x = _wspawnv(_P_NOWAIT, ax[0], (const wchar_t * const *)ax);
    for(i=0; i<=3; i++) fossil_unicode_free(ax[i]);
    if( g.fAnyTrace ){
      fprintf(stderr, 
        "/***** Subprocess %d creates backoffice child %d *****/\n",
        GETPID(), (int)x);
    }
    if( x>=0 ) return;
  }
#else /* unix */
  {
    pid_t pid = fork();
    if( pid>0 ){
      /* This is the parent in a successful fork().  Return immediately. */
      if( g.fAnyTrace ){
        fprintf(stderr, 
          "/***** Subprocess %d creates backoffice child %d *****/\n",
          GETPID(), (int)pid);
      }
      return;
    }
    if( pid==0 ){
      /* This is the child of a successful fork().  Run backoffice. */
      setsid();
      db_open_repository(backofficeDb);
      backofficeDb = "x";
      backoffice_thread();
      db_close(1);
      if( g.fAnyTrace ){
        fprintf(stderr, "/***** Backoffice Child %d exits *****/\n", GETPID());
      }
      exit(0);
    }
  }
#endif
  /* Fork() failed or is unavailable.  Run backoffice in this process, but
  ** do so with the no-delay setting.







|




|











|











|







539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
    wchar_t *ax[5];
    argv[0] = g.nameOfExe;
    argv[1] = "backoffice";
    argv[2] = "-R";
    argv[3] = backofficeDb;
    ax[4] = 0;
    for(i=0; i<=3; i++) ax[i] = fossil_utf8_to_unicode(argv[i]);
    x = _wspawnv(_P_NOWAIT, ax[0], ax);
    for(i=0; i<=3; i++) fossil_unicode_free(ax[i]);
    if( g.fAnyTrace ){
      fprintf(stderr, 
        "/***** Subprocess %d creates backoffice child %d *****/\n",
        getpid(), (int)x);
    }
    if( x>=0 ) return;
  }
#else /* unix */
  {
    pid_t pid = fork();
    if( pid>0 ){
      /* This is the parent in a successful fork().  Return immediately. */
      if( g.fAnyTrace ){
        fprintf(stderr, 
          "/***** Subprocess %d creates backoffice child %d *****/\n",
          getpid(), (int)pid);
      }
      return;
    }
    if( pid==0 ){
      /* This is the child of a successful fork().  Run backoffice. */
      setsid();
      db_open_repository(backofficeDb);
      backofficeDb = "x";
      backoffice_thread();
      db_close(1);
      if( g.fAnyTrace ){
        fprintf(stderr, "/***** Backoffice Child %d exits *****/\n", getpid());
      }
      exit(0);
    }
  }
#endif
  /* Fork() failed or is unavailable.  Run backoffice in this process, but
  ** do so with the no-delay setting.
Changes to src/db.c.
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
/*
** SETTING: autosync-tries  width=16 default=1
** If autosync is enabled setting this to a value greater
** than zero will cause autosync to try no more than this
** number of attempts if there is a sync failure.
*/
/*
** SETTING: backoffice-nodelay boolean default=on
** If backoffice-nodelay is true, then the backoffice processing
** will never invoke sleep().  If it has nothing useful to do,
** it simply exits.
*/
/*
** SETTING: backoffice-logfile width=40
** If backoffice-logfile is not an empty string and is a valid







|







3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
/*
** SETTING: autosync-tries  width=16 default=1
** If autosync is enabled setting this to a value greater
** than zero will cause autosync to try no more than this
** number of attempts if there is a sync failure.
*/
/*
** SETTING: backoffice-nodelay boolean default=off
** If backoffice-nodelay is true, then the backoffice processing
** will never invoke sleep().  If it has nothing useful to do,
** it simply exits.
*/
/*
** SETTING: backoffice-logfile width=40
** If backoffice-logfile is not an empty string and is a valid