Fossil

Check-in [1021afc6]
Login

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

Overview
Comment:Add the --poll option to "fossil backoffice" to allow it to act as a backoffice launch daemon.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1021afc6aa3ea77c9d242b948f306e02c47f5769e76f22cc2b4348ee5e5404b4
User & Date: drh 2019-03-07 17:57:34
Context
2019-03-08
00:42
Update the documentation on the "fossil backoffice" command. check-in: 35c8103e user: drh tags: trunk
2019-03-07
17:57
Add the --poll option to "fossil backoffice" to allow it to act as a backoffice launch daemon. check-in: 1021afc6 user: drh tags: trunk
16:43
Enhancements to the "backoffice" command: Added the --nodelay option and added the ability to specify multiple repositories. check-in: 96fc4848 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/backoffice.c.

78
79
80
81
82
83
84

85
86
87
88
89
90
91
...
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
583
584
585
586
587
588



589
590
591









592
593
594
595
596
597
598
# include <unistd.h>
# include <sys/types.h>
# include <signal.h>
# include <errno.h>
# include <fcntl.h>
# define GETPID getpid
#endif


/*
** The BKOFCE_LEASE_TIME is the amount of time for which a single backoffice
** processing run is valid.  Each backoffice run monopolizes the lease for
** at least this amount of time.  Hopefully all backoffice processing is
** finished much faster than this - usually in less than a second.  But
** regardless of how long each invocation lasts, successive backoffice runs
................................................................................
**
** Usage: backoffice [OPTIONS...] [REPOSITORIES...]
**
** Run backoffice processing on the repositories listed.  If no
** repository is specified, run it on the repository of the local checkout.
**
** This might be done by a cron job or similar to make sure backoffice
** processing happens periodically.


**
** Options:


**
**    --nodelay               Do not queue up or wait for a backoffice job
**                            to complete. If no work is available or if
**                            backoffice has run recently, return immediately.
**                            The --nodelay option is implied if more than
**                            one repository is listed on the command-line.
**




**    --trace                 Enable debugging output on stderr
*/
void backoffice_command(void){




  if( find_option("trace",0,0)!=0 ) g.fAnyTrace = 1;
  if( find_option("nodelay",0,0)!=0 ) backofficeNoDelay = 1;




  /* Silently consume the -R or --repository flag, leaving behind its
  ** argument. This is for legacy compatibility. Older versions of the
  ** backoffice command only ran on a single repository that was specified
  ** using the -R option. */
  (void)find_option("repository","R",0);

  verify_all_options();
  if( g.argc>3 ){
    /* Multiple repositories named on the command-line.  Run each in a

    ** separate sub-process */
    int i;




    for(i=2; i<g.argc; i++){
      Blob cmd;


      blob_init(&cmd, 0, 0);
      blob_append_escaped_arg(&cmd, g.nameOfExe);
      blob_append(&cmd, " backoffice --nodelay", -1);
      if( g.fAnyTrace ){
        blob_append(&cmd, " --trace", -1);
      }
      blob_append_escaped_arg(&cmd, g.argv[i]);
      if( g.fAnyTrace ){
        fossil_print("-- %s\n", blob_str(&cmd));



      }
      fossil_system(blob_str(&cmd));
      blob_reset(&cmd);









    }
  }else{
    if( g.argc==3 ){
      g.zRepositoryOption = g.argv[2];
      g.argc--;
    }
    db_find_and_open_repository(0,0);







>







 







|
>
>

|
>
>







>
>
>
>



>
>
>
>


>
>
>








|
|
>
|

>
>
>
>
|
|
>
>
|
|
|
|
|
|
|
<
<
>
>
>
|
|
|
>
>
>
>
>
>
>
>
>







78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
...
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609


610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# include <unistd.h>
# include <sys/types.h>
# include <signal.h>
# include <errno.h>
# include <fcntl.h>
# define GETPID getpid
#endif
#include <time.h>

/*
** The BKOFCE_LEASE_TIME is the amount of time for which a single backoffice
** processing run is valid.  Each backoffice run monopolizes the lease for
** at least this amount of time.  Hopefully all backoffice processing is
** finished much faster than this - usually in less than a second.  But
** regardless of how long each invocation lasts, successive backoffice runs
................................................................................
**
** Usage: backoffice [OPTIONS...] [REPOSITORIES...]
**
** Run backoffice processing on the repositories listed.  If no
** repository is specified, run it on the repository of the local checkout.
**
** This might be done by a cron job or similar to make sure backoffice
** processing happens periodically.  Or, the --poll option can be used
** to run this command as a daemon that will periodically invoke backoffice
** on collection of repositories.
**
** OPTIONS:
**
**    --debug                 Show what this command is doing.
**
**    --nodelay               Do not queue up or wait for a backoffice job
**                            to complete. If no work is available or if
**                            backoffice has run recently, return immediately.
**                            The --nodelay option is implied if more than
**                            one repository is listed on the command-line.
**
**    --poll N                Repeat backoffice calls for repositories that
**                            change in appoximately N-second intervals.
**                            N less than 1 turns polling off (the default).
**
**    --trace                 Enable debugging output on stderr
*/
void backoffice_command(void){
  int nPoll;
  const char *zPoll;
  int bDebug = 0;
  unsigned int nCmd = 0;
  if( find_option("trace",0,0)!=0 ) g.fAnyTrace = 1;
  if( find_option("nodelay",0,0)!=0 ) backofficeNoDelay = 1;
  zPoll = find_option("poll",0,1);
  nPoll = zPoll ? atoi(zPoll) : 0;
  bDebug = find_option("debug",0,0)!=0;

  /* Silently consume the -R or --repository flag, leaving behind its
  ** argument. This is for legacy compatibility. Older versions of the
  ** backoffice command only ran on a single repository that was specified
  ** using the -R option. */
  (void)find_option("repository","R",0);

  verify_all_options();
  if( g.argc>3 || nPoll>0 ){
    /* Either there are multiple repositories named on the command-line
    ** or we are polling.  In either case, each backoffice should be run
    ** using a separate sub-process */
    int i;
    time_t iNow = 0;
    time_t ix;
    while( 1 /* exit via "break;" */){
      time_t iNext = time(0);
      for(i=2; i<g.argc; i++){
        Blob cmd;
        if( !file_isfile(g.argv[i], ExtFILE) ) continue;
        if( iNow && iNow>file_mtime(g.argv[i],ExtFILE) ) continue;
        blob_init(&cmd, 0, 0);
        blob_append_escaped_arg(&cmd, g.nameOfExe);
        blob_append(&cmd, " backoffice --nodelay", -1);
        if( g.fAnyTrace ){
          blob_append(&cmd, " --trace", -1);
        }
        blob_append_escaped_arg(&cmd, g.argv[i]);


        nCmd++;
        if( bDebug ){
          fossil_print("COMMAND[%u]: %s\n", nCmd, blob_str(&cmd));
        }
        fossil_system(blob_str(&cmd));
        blob_reset(&cmd);
      }
      if( nPoll<1 ) break;
      iNow = iNext;
      ix = time(0);
      if( ix < iNow+nPoll ){
        sqlite3_int64 nMS = (iNow + nPoll - ix)*1000;
        if( bDebug )fossil_print("SLEEP: %lld\n", nMS);
        sqlite3_sleep((int)nMS);
      }
    }
  }else{
    if( g.argc==3 ){
      g.zRepositoryOption = g.argv[2];
      g.argc--;
    }
    db_find_and_open_repository(0,0);