Search code examples
firebasegoogle-cloud-firestorecrongoogle-cloud-functionsscheduled-tasks

How does pub/sub in firebase cloud function work?


I'm building a web application about working schedule management with firebase. The flow is :

-Creation of template of shift schedule:

template = {
    schedule : [
      {
        day : 'Monday',
        work_time : '10am-6pm'
      },
      {
        day : 'Tuesday',
        work_time : '8am-3pm'
      }
    ],
    from_date : '2021/01/25',
    to_date : '2021/05/25'
  }

-Creation of shift on user: Add template to user's shift or edit template

so basically, i need to pass the template data of user to subscribe to cloud scheduler to add to user's shift everyday within the date range.

shift = {
   user_id : 'user_id',
   day : 'Monday',
   work_date : '2021/01/25',
   work_time : '10am-6pm',
  }
 // on the next day auto create
 shift = {
   user_id : 'user_id',
   day : 'Tuesday',
   work_date : '2021/01/26',
   work_time : '8am-3pm'
  }

-I need to unsubscribe the scheduler when editing template on user's shift.

-In the template creation, there's a button to stop creating shift on all users who have that template.

Do i make any sense? Please help with my use case. Is it possible?


Solution

  • It's quite common in some use cases to add template/initial data to a database every midnight (or anytime depending on use case) so that can be updated or listed somewhere.

    I need to unsubscribe the scheduler when editing template on each user's shift.

    I also need to unsubscribe all related user's scheduler when click stop creating button on template creation.

    There's no direct way to do anything like that. When a user starts editing their shift you would have to store that in a database first. In your scheduled function you can check (from database) if a task is being edited. If yes, then skip it and proceed with others.

    Also what do you mean by "all related user's scheduler"? Ideally you will have a single cloud function that will process all users and not a dedicated pub/sub topic for every users. You just follow the same way as in first case, if a user has clicked that stop button then store that in database and don't process that user whenever your function runs.