@@ -12,7 +12,6 @@ import { redux } from "@cocalc/frontend/app-framework";
12
12
import { appBasePath } from "@cocalc/frontend/customize/app-base-path" ;
13
13
import { dialogs } from "@cocalc/frontend/i18n" ;
14
14
import { getIntl } from "@cocalc/frontend/i18n/get-intl" ;
15
- import { allow_project_to_run } from "@cocalc/frontend/project/client-side-throttle" ;
16
15
import { ensure_project_running } from "@cocalc/frontend/project/project-start-warning" ;
17
16
import { API } from "@cocalc/frontend/project/websocket/api" ;
18
17
import { connection_to_project } from "@cocalc/frontend/project/websocket/connect" ;
@@ -44,9 +43,10 @@ import { readFile, type ReadFileOptions } from "@cocalc/conat/files/read";
44
43
import { type ProjectApi } from "@cocalc/conat/project/api" ;
45
44
import { type CopyOptions } from "@cocalc/conat/files/fs" ;
46
45
46
+ const TOUCH_THROTTLE = 30_000 ;
47
+
47
48
export class ProjectClient {
48
49
private client : WebappClient ;
49
- private touch_throttle : { [ project_id : string ] : number } = { } ;
50
50
51
51
constructor ( client : WebappClient ) {
52
52
this . client = client ;
@@ -393,63 +393,40 @@ export class ProjectClient {
393
393
} ,
394
394
) ;
395
395
396
- touch_project = async (
397
- // project_id where activity occured
398
- project_id : string ,
399
- // optional global id of a compute server (in the given project), in which case we also mark
400
- // that compute server as active, which keeps it running in case it has idle timeout configured.
401
- compute_server_id ?: number ,
402
- ) : Promise < void > => {
403
- if ( ! is_valid_uuid_string ( project_id ) ) {
404
- console . warn ( "WARNING -- touch_project takes a project_id, but got " , {
405
- project_id,
406
- } ) ;
407
- }
408
- if ( compute_server_id ) {
409
- // this is throttled, etc. and is independent of everything below.
410
- touchComputeServer ( {
411
- project_id,
412
- compute_server_id,
413
- client : this . client ,
414
- } ) ;
415
- // that said, we do still touch the project, since if a user is actively
416
- // using a compute server, the project should also be considered active.
417
- }
418
-
419
- const state = redux . getStore ( "projects" ) ?. get_state ( project_id ) ;
420
- if ( ! ( state == null && redux . getStore ( "account" ) ?. get ( "is_admin" ) ) ) {
421
- // not trying to view project as admin so do some checks
422
- if ( ! ( await allow_project_to_run ( project_id ) ) ) return ;
423
- if ( ! this . client . is_signed_in ( ) ) {
424
- // silently ignore if not signed in
396
+ touch_project = throttle (
397
+ async (
398
+ // project_id where activity occured
399
+ project_id : string ,
400
+ // optional global id of a compute server (in the given project), in which case we also mark
401
+ // that compute server as active, which keeps it running in case it has idle timeout configured.
402
+ compute_server_id ?: number | null ,
403
+ ) : Promise < void > => {
404
+ console . log ( "touch_project" , { project_id, compute_server_id } ) ;
405
+ if ( ! is_valid_uuid_string ( project_id ) ) {
406
+ console . warn ( "WARNING -- touch_project takes a project_id, but got " , {
407
+ project_id,
408
+ } ) ;
425
409
return ;
426
410
}
427
- if ( state != "running" ) {
428
- // not running so don't touch (user must explicitly start first)
429
- return ;
411
+ if ( compute_server_id ) {
412
+ // this is independent of everything below.
413
+ touchComputeServer ( {
414
+ project_id,
415
+ compute_server_id,
416
+ client : this . client ,
417
+ } ) ;
418
+ // that said, we do still touch the project, since if a user is actively
419
+ // using a compute server, the project should also be considered active.
430
420
}
431
- }
432
421
433
- // Throttle -- so if this function is called with the same project_id
434
- // twice in 3s, it's ignored (to avoid unnecessary network traffic).
435
- // Do not make the timeout long, since that can mess up
436
- // getting the hub-websocket to connect to the project.
437
- const last = this . touch_throttle [ project_id ] ;
438
- if ( last != null && Date . now ( ) - last <= 3000 ) {
439
- return ;
440
- }
441
- this . touch_throttle [ project_id ] = Date . now ( ) ;
442
- try {
443
- await this . client . conat_client . hub . db . touch ( { project_id } ) ;
444
- } catch ( err ) {
445
- // silently ignore; this happens, e.g., if you touch too frequently,
446
- // and shouldn't be fatal and break other things.
447
- // NOTE: this is a bit ugly for now -- basically the
448
- // hub returns an error regarding actually touching
449
- // the project (updating the db), but it still *does*
450
- // ensure there is a TCP connection to the project.
451
- }
452
- } ;
422
+ try {
423
+ await this . client . conat_client . hub . db . touch ( { project_id } ) ;
424
+ } catch ( err ) {
425
+ console . log ( "WARNING: issue touching project" , err ) ;
426
+ }
427
+ } ,
428
+ TOUCH_THROTTLE ,
429
+ ) ;
453
430
454
431
// Print sagews to pdf
455
432
// The printed version of the file will be created in the same directory
0 commit comments