Much of CASHPOCKET moves by date, not by request. A campaign opens when its start date arrives, recruitment closes at a deadline, rewards are paid after a review period, reminders go out three days before a deadline. All of that is scheduled work, and the server that does it runs as two identical processes.
The problem
pm2 runs the server in cluster mode with two instances. Both load the same entry file. If that file starts a scheduler, there are two schedulers, and every job fires twice within the same second.
For most of what the jobs do, twice is not a harmless inefficiency.
- A job that pays rewards would start two payouts for each tester.
- A job that sends a KakaoTalk reminder would send it twice. Alert messages are billed per message and read by real customers.
- A job that settles a campaign’s unused budget would start two refunds to the advertiser.
The reward has a second defence in the data layer: a paid-at claim and an idempotency key stop a repeated payout from moving points. A message has none. Once it is handed to the provider it is sent. The cleanest fix is for the second scheduler not to exist.
One scheduler

pm2 gives every process in a cluster a numeric instance id through the environment. The entry file starts listening for HTTP first, then loads the scheduler module and asks it to initialise. The first thing the initialiser does is read that id. If it is defined and is not zero, it returns without registering anything.
Two further switches follow, in order.
initScheduler(env):
id = env.instanceId # set by pm2 for each process in the cluster
if id is defined and id != "0":
return # not the scheduler instance
if env.CRON_ENABLED == "false":
return # global off switch
if env.APP_ENV == "staging" and env.CRON_ENABLED != "true":
return # staging is off unless switched on
register every job in the Etc/UTC timezone
A staging server boots the same code against its own database. With the scheduler on by default it would run the daily jobs against staging data and, depending on the provider policy, send messages. It therefore needs an explicit switch to run them at all. A second, global switch turns scheduling off everywhere.
Jobs call the API
Most jobs contain one statement: a POST to a route on the server itself, with a job key in a header. The route checks the key and does the work.
That choice has three consequences.
- One implementation. The route that a schedule triggers at midnight is the same one an operator can trigger by hand, with the same checks.
- One authentication rule. A job route checks a key sent in a request header, and tests cover that check.
- Either instance can do the work. The scheduler lives in instance 0, but its HTTP call is balanced across the cluster like any other request. The one-instance rule is about who starts a job, not who executes it.
A few jobs run in the scheduler process itself because they are operations work with no API surface: the nightly database backup to object storage, the removal of files that were soft-deleted more than 30 days ago, and the cleanup of old inquiry attachments.
What runs when
Times are Korea time. The scheduler itself is registered in UTC.
| When | Job | What it moves |
|---|---|---|
| 00:01 | Campaign start | Approved campaigns whose recruitment start has arrived become active |
| 00:02 | Recruitment close | Campaigns past their deadline close recruitment |
| 00:03 | Submission deadlines | Applications whose selection was never confirmed are rejected. Active testers past their personal deadline are closed without a reward |
| 07:50 | Reward payout | Completed applications without a paid-at timestamp are paid |
| 08:00 | Campaign closure | Seven days after the review deadline the campaign closes, and anyone still unpaid is paid |
| 10:00 | Deadline reminder | A reminder three days before the review deadline |
| 12:00 | Automatic selection | Two days after the deadline, pending selections are confirmed, the rest rejected, and unused budget goes back to the advertiser through the ledger |
| 03:05 | Backup | A database dump to object storage |
| 03:10 | File cleanup | Files soft-deleted more than 30 days ago are removed from storage and the database |
| 03:35, 03:40 | Deal settlement | Cashback is paid first, then deal budgets are settled |
| 03:45 | Video budget refund | Unused budget of advertiser videos whose exposure period has ended is returned |
| 09:20 | Ad metrics | Spend and results per ad are synced from the ad platform |
| Every 5 minutes | Scheduled push | Push campaigns whose time has come are sent, and ones stuck mid-send are reclaimed |
| Thursday 17:30 | Withdrawal digest | A weekly summary of pending withdrawals for the people who process them |
Two things about this table matter.
The campaign jobs run a minute apart in lifecycle order: start, close, deadlines. A campaign that should both start and close on the same night passes through the states in sequence.
The two deal jobs run five minutes apart in a fixed order. Cashback to testers goes first and the budget settlement second, so unused budget is never computed from a budget that still owes a payout. The video budget refund follows five minutes later, which keeps the three jobs’ ledger writes from overlapping.
Jobs that can run twice anyway
The one-instance rule removes the systematic duplicate. It does not remove every duplicate: an operator can trigger a route by hand while the schedule is running, and two different jobs can cover the same application. The job bodies are written for that.
- The reward payout claims each application by setting its paid-at timestamp conditionally, then pays with a key built from the campaign and the tester. The 07:50 and 08:00 jobs overlap, and the second finds nothing left to pay for an application the first has claimed.
- The push dispatcher marks a campaign as sending before it sends, so a second run does not pick it up, and reclaims one that has been in that state too long.
- The date-driven status jobs are conditional updates on the current status. Running one twice moves nothing the second time.
Failure modes
| What happens | What the system does | Covered by |
|---|---|---|
| Both instances boot | Instance 1 reads a non-zero id and registers nothing | The check at the top of the initialiser |
| A staging server boots | No jobs unless the switch is set to true | A test with four cases on the environment gate |
| A staging server is pointed at production state | The server refuses to boot | A test on the staging guard |
| A job route is called without the key | The request is rejected | Tests on the key check |
| A reward job runs twice | The paid-at claim and the reward key stop the second payout | Tests on the reward service against the kernel |
| A scheduled push is interrupted mid-send | The next run reclaims it | The dispatcher’s reclaim rule |
Limits
- The rule is positional. It relies on pm2’s instance ids on one machine. Two machines would each have an instance 0, and the design would then need a lock in the database or Redis instead of an id check.
- The instance check itself has no unit test. The environment gate has four cases. The id check does not have one yet.
- A job that fails is not retried by the scheduler. It runs again at its next scheduled time. The daily jobs select everything that is due, not only what became due that day, so a missed night is picked up by the next one.
